home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / older_versions / ncftp-3.2.2-src.tar.bz2 / ncftp-3.2.2-src.tar / ncftp-3.2.2 / libncftp / u_gethome.c < prev    next >
C/C++ Source or Header  |  2008-07-13  |  3KB  |  126 lines

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