homedir.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id: homedir.c,v 1.4 2004/01/29 13:54:08 bagder Exp $
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifdef WIN32
  28. #include <windows.h>
  29. #endif
  30. #ifdef HAVE_PWD_H
  31. #include <pwd.h>
  32. #endif
  33. #ifdef HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36. #ifdef VMS
  37. #include <unixlib.h>
  38. #endif
  39. #include "homedir.h"
  40. #ifdef CURLDEBUG
  41. #include "../lib/memdebug.h"
  42. #endif
  43. static
  44. char *GetEnv(const char *variable, char do_expand)
  45. {
  46. char *env = NULL;
  47. #ifdef WIN32
  48. char buf1[1024], buf2[1024];
  49. DWORD rc;
  50. /* Don't use getenv(); it doesn't find variable added after program was
  51. * started. Don't accept truncated results (i.e. rc >= sizeof(buf1)). */
  52. rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
  53. if (rc > 0 && rc < sizeof(buf1)) {
  54. env = buf1;
  55. variable = buf1;
  56. }
  57. if (do_expand && strchr(variable,'%')) {
  58. /* buf2 == variable if not expanded */
  59. rc = ExpandEnvironmentStrings (variable, buf2, sizeof(buf2));
  60. if (rc > 0 && rc < sizeof(buf2) &&
  61. !strchr(buf2,'%')) /* no vars still unexpanded */
  62. env = buf2;
  63. }
  64. #else
  65. (void)do_expand;
  66. #ifdef VMS
  67. env = getenv(variable);
  68. if (env && strcmp("HOME",variable) == 0) {
  69. env = decc$translate_vms(env);
  70. }
  71. #else
  72. /* no length control */
  73. env = getenv(variable);
  74. #endif
  75. #endif
  76. return (env && env[0])?strdup(env):NULL;
  77. }
  78. /* return the home directory of the current user as an allocated string */
  79. char *homedir(void)
  80. {
  81. char *home = GetEnv("HOME", FALSE);
  82. if(home)
  83. return home;
  84. #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  85. {
  86. struct passwd *pw = getpwuid(geteuid());
  87. if (pw) {
  88. #ifdef VMS
  89. home = decc$translate_vms(pw->pw_dir);
  90. #else
  91. home = pw->pw_dir;
  92. #endif
  93. if (home && home[0])
  94. home = strdup(home);
  95. }
  96. }
  97. #endif /* PWD-stuff */
  98. #ifdef WIN32
  99. home = GetEnv("APPDATA", TRUE);
  100. if(!home)
  101. home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
  102. on Win-2K/XP */
  103. #endif /* WIN32 */
  104. return home;
  105. }