PageRenderTime 69ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/ncftp-3.2.5/libncftp/u_gethome.c

#
C | 125 lines | 101 code | 17 blank | 7 comment | 49 complexity | e8b536cc4cbabe4bf384f596cae1cbe5 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /* u_gethome.c
  2. *
  3. * Copyright (c) 1996-2006 Mike Gleason, NcFTP Software.
  4. * All rights reserved.
  5. *
  6. */
  7. #include "syshdrs.h"
  8. #ifdef PRAGMA_HDRSTOP
  9. # pragma hdrstop
  10. #endif
  11. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  12. #define _CRT_SECURE_NO_WARNINGS 1
  13. extern void GetSpecialDir(char *dst, size_t size, int whichDir);
  14. #endif
  15. void
  16. GetHomeDir(char *const dst, const size_t size)
  17. {
  18. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  19. const char *homedrive, *homepath;
  20. homepath = getenv("USERPROFILE"); /* Windows XP */
  21. if (homepath != NULL) {
  22. (void) Strncpy(dst, homepath, size);
  23. return;
  24. }
  25. homedrive = getenv("HOMEDRIVE");
  26. homepath = getenv("HOMEPATH");
  27. if ((homedrive != NULL) && (homepath != NULL)) {
  28. (void) Strncpy(dst, homedrive, size);
  29. (void) Strncat(dst, homepath, size);
  30. return;
  31. }
  32. GetSpecialDir(dst, size, CSIDL_PERSONAL /* "My Documents" */);
  33. if (dst[0] != '\0')
  34. return;
  35. dst[0] = '\0';
  36. if (GetWindowsDirectory(dst, size - 1) < 1)
  37. (void) Strncpy(dst, ".", size);
  38. else if (dst[1] == ':') {
  39. dst[2] = '\\';
  40. dst[3] = '\0';
  41. }
  42. #else
  43. struct passwd pw;
  44. char pwbuf[256];
  45. if (GetMyPwEnt(&pw, pwbuf, sizeof(pwbuf)) == 0) {
  46. (void) Strncpy(dst, pw.pw_dir, size);
  47. } else {
  48. (void) Strncpy(dst, ".", size);
  49. }
  50. #endif
  51. } /* GetHomeDir */
  52. void
  53. GetTmpDir(char *const dst, const size_t size)
  54. {
  55. static const char *envvars[] = {"TMPDIR", "TMP", "TEMP", NULL};
  56. const char *tdir;
  57. int i;
  58. struct Stat st;
  59. memset(dst, 0, size);
  60. for (i = 0; envvars[i] != NULL; i++) {
  61. tdir = getenv(envvars[i]);
  62. if ((tdir == NULL) || (tdir[0] == '\0'))
  63. continue;
  64. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  65. #else
  66. if (tdir[0] != '/')
  67. continue;
  68. #endif
  69. if ((Stat(tdir, &st) >= 0) && (S_ISDIR(st.st_mode))) {
  70. (void) Strncpy(dst, tdir, size);
  71. return;
  72. }
  73. }
  74. /* No suitable environment variable found. */
  75. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  76. # ifdef CSIDL_WINDOWS
  77. memset(dst, 0, size);
  78. GetSpecialDir(dst, size, CSIDL_WINDOWS /* "C:\WINDOWS" */);
  79. if (dst[0] != '\0') {
  80. (void) Strncat(dst, "\\TEMP", size);
  81. if ((Stat(dst, &st) >= 0) && (S_ISDIR(st.st_mode))) {
  82. return;
  83. }
  84. }
  85. # else
  86. (void) Strncpy(dst, "C:\\WINDOWS\\TEMP", size);
  87. if ((Stat(dst, &st) >= 0) && (S_ISDIR(st.st_mode)))
  88. return;
  89. (void) Strncpy(dst, "C:\\WINNT\\TEMP", size);
  90. if ((Stat(dst, &st) >= 0) && (S_ISDIR(st.st_mode)))
  91. return;
  92. # endif
  93. # ifdef CSIDL_INTERNET_CACHE
  94. memset(dst, 0, size);
  95. GetSpecialDir(dst, size, CSIDL_INTERNET_CACHE /* "Temporary Internet Files" */);
  96. if ((dst[0] != '\0') && (Stat(dst, &st) >= 0) && (S_ISDIR(st.st_mode)))
  97. return;
  98. # endif
  99. (void) Strncpy(dst, "\\TEMP", size);
  100. if ((Stat(dst, &st) >= 0) && (S_ISDIR(st.st_mode)))
  101. return;
  102. #else
  103. (void) Strncpy(dst, "/tmp", size);
  104. if ((Stat(dst, &st) >= 0) && (S_ISDIR(st.st_mode)))
  105. return;
  106. #endif
  107. memset(dst, 0, size); /* return empty string */
  108. } /* GetTmpDir */