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_misc.c < prev    next >
C/C++ Source or Header  |  2008-09-03  |  8KB  |  385 lines

  1. /* u_misc.c
  2.  *
  3.  * Copyright (c) 1996-2005 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. static int wsaInit = 0;
  14. #if defined(WIN32) || defined(_WINDOWS)
  15. WSADATA wsaData;
  16. #endif
  17.  
  18.  
  19.  
  20. void
  21. InitWinsock(void)
  22. {
  23. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  24.     if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
  25. #    ifdef _CONSOLE
  26.         fprintf(stderr, "could not initialize winsock\n");
  27. #    endif
  28.         exit(1);
  29.     }
  30. #endif
  31.     wsaInit++;
  32. }    /* InitWinsock */
  33.  
  34.  
  35.  
  36. void
  37. DisposeWinsock(void)
  38. {
  39. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  40.     if (wsaInit > 0) { WSACleanup(); wsaInit--; }
  41. #else
  42.     wsaInit--;
  43. #endif
  44. }    /* DisposeWinsock */
  45.  
  46.  
  47.  
  48.  
  49. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  50.  
  51.  
  52. int gettimeofday(struct timeval *const tp, void *junk)
  53. {
  54. #if (_WIN32_WINNT < 0x0500)
  55.     /* Older systems don't have all the necessary time functions. */
  56.  
  57.     SYSTEMTIME systemTime;
  58.  
  59.     if (tp == NULL)
  60.         return (-1);
  61.  
  62.     GetSystemTime(&systemTime);
  63.  
  64.     /* Create an arbitrary second counter;
  65.      * Note that this particular one creates
  66.      * a problem at the end of the month.
  67.      */
  68.     tp->tv_sec =
  69.         systemTime.wSecond +
  70.         systemTime.wMinute * 60 +
  71.         systemTime.wHour * 3600 +
  72.         systemTime.wDay * 86400;
  73.  
  74.     tp->tv_usec = systemTime.wMilliseconds * 1000;
  75.  
  76.     return 0;
  77. #else    /* Windows 2000 or better */
  78.     FILETIME systemTime, unixEpochFileTime;
  79.     ULARGE_INTEGER sysTimeAsFileTime;
  80.     SYSTEMTIME unixEpochSysTime;
  81.     static int initialized = 0;
  82.     static unsigned __int64 unixEpochQuad = 0;
  83.     unsigned __int64 now_time64_t;
  84.     time_t now_time_t;
  85.  
  86.     if (tp == NULL)
  87.         return (-1);
  88.  
  89.     tp->tv_sec = 0;
  90.     tp->tv_usec = 0;
  91.  
  92.     GetSystemTimeAsFileTime(&systemTime);
  93.     sysTimeAsFileTime.QuadPart = 0;
  94.     sysTimeAsFileTime.LowPart = systemTime.dwLowDateTime;
  95.     sysTimeAsFileTime.HighPart = systemTime.dwHighDateTime;
  96.  
  97.     if (initialized == 0) {
  98.         memset(&unixEpochSysTime, 0, sizeof(unixEpochSysTime));
  99.         memset(&unixEpochFileTime, 0, sizeof(unixEpochFileTime));
  100.         unixEpochSysTime.wYear = 1970;
  101.         unixEpochSysTime.wMonth = 1;
  102.         unixEpochSysTime.wDay = 1;
  103.         if (! SystemTimeToFileTime(&unixEpochSysTime, &unixEpochFileTime))
  104.             return (-1);
  105.         unixEpochQuad = (unsigned __int64) unixEpochFileTime.dwLowDateTime + ((unsigned __int64) unixEpochFileTime.dwHighDateTime << 32);
  106.         if (sysTimeAsFileTime.QuadPart < unixEpochQuad)
  107.             return (-1);
  108.         initialized = 1;
  109.     }
  110.  
  111.     /* Compute number of 100-ns (0.1 us) intervals since Jan 1, 1970. */
  112.     now_time64_t = sysTimeAsFileTime.QuadPart - unixEpochQuad;
  113.     tp->tv_usec = (unsigned long) ((now_time64_t / (unsigned __int64) 10) % (unsigned __int64) 1000000);
  114.     now_time64_t /= 10000000;
  115.     now_time_t = (time_t) (now_time64_t & 0xFFFFFFFF);
  116.     tp->tv_sec = (long) now_time_t;
  117.  
  118.     return 0;
  119. #endif
  120. }    /* gettimeofday */
  121.  
  122.  
  123.  
  124.  
  125. void WinSleep(unsigned int seconds)
  126. {
  127.     DWORD now, deadline;
  128.     DWORD milliseconds = seconds * 1000;
  129.  
  130.     if (milliseconds > 0) {
  131.         now = GetTickCount();
  132.         deadline = now + milliseconds;
  133.         if (now < deadline) {
  134.             /* Typical case */
  135.             do {
  136.                 milliseconds = deadline - now;
  137.                 Sleep(milliseconds);
  138.                 now = GetTickCount();
  139.             } while (now < deadline);
  140.         } else {
  141.             /* Overflow case */
  142.             deadline = now - 1;
  143.             milliseconds -= (0xFFFFFFFF - now);
  144.             do {
  145.                 Sleep(0xFFFFFFFF - now);
  146.                 now = GetTickCount();
  147.             } while (now > deadline);
  148.             /* Counter has now wrapped around */
  149.             deadline = now + milliseconds;
  150.             do {
  151.                 milliseconds = deadline - now;
  152.                 Sleep(milliseconds);
  153.                 now = GetTickCount();
  154.             } while (now < deadline);
  155.         }
  156.     }
  157. }    /* WinSleep */
  158. #endif
  159.  
  160.  
  161.  
  162. #if defined(WIN32) || defined(_WINDOWS) || defined(__CYGWIN__)
  163. char *
  164. StrFindLocalPathDelim(const char *src) /* TODO: optimize */
  165. {
  166.     const char *first;
  167.     int c;
  168.  
  169.     first = NULL;
  170.     for (;;) {
  171.         c = *src++;
  172.         if (c == '\0')
  173.             break;
  174.         if (IsLocalPathDelim(c)) {
  175.             first = src - 1;
  176.             break;
  177.         }
  178.     }
  179.  
  180.     return ((char *) first);
  181. }    /* StrFindLocalPathDelim */
  182.  
  183.  
  184.  
  185. char *
  186. StrRFindLocalPathDelim(const char *src)    /* TODO: optimize */
  187. {
  188.     const char *last;
  189.     int c;
  190.  
  191.     last = NULL;
  192.     for (;;) {
  193.         c = *src++;
  194.         if (c == '\0')
  195.             break;
  196.         if (IsLocalPathDelim(c))
  197.             last = src - 1;
  198.     }
  199.  
  200.     return ((char *) last);
  201. }    /* StrRFindLocalPathDelim */
  202.  
  203.  
  204.  
  205.  
  206. void
  207. StrRemoveTrailingLocalPathDelim(char *dst)
  208. {
  209.     char *cp;
  210.  
  211.     cp = StrRFindLocalPathDelim(dst);
  212.     if ((cp == NULL) || (cp[1] != '\0'))
  213.         return;
  214.  
  215.     /* Note: Do not destroy a path of "/" */
  216.     while ((cp > dst) && (IsLocalPathDelim(*cp)))
  217.         *cp-- = '\0';
  218. }    /* StrRemoveTrailingLocalPathDelim */
  219.  
  220.  
  221.  
  222. void
  223. TVFSPathToLocalPath(char *dst)
  224. {
  225.     int c;
  226.  
  227.     /* Note: Technically we don't need to do this,
  228.      * since Win32 accepts a / as equivalent to a \
  229.      * in a pathname.
  230.      */
  231.     if (dst != NULL) {
  232.         for (;;) {
  233.             c = *dst++;
  234.             if (c == '\0')
  235.                 break;
  236.             if (c == '/')
  237.                 dst[-1] = LOCAL_PATH_DELIM;
  238.         }
  239.     }
  240. }    /* TVFSPathToLocalPath */
  241.  
  242.  
  243. void
  244. LocalPathToTVFSPath(char *dst)
  245. {
  246.     int c;
  247.  
  248.     if (dst != NULL) {
  249.         for (;;) {
  250.             c = *dst++;
  251.             if (c == '\0')
  252.                 break;
  253.             if (c == LOCAL_PATH_DELIM)
  254.                 dst[-1] = '/';
  255.         }
  256.     }
  257. }    /* LocalPathToTVFSPath */
  258. #endif
  259.  
  260.  
  261.  
  262.  
  263.  
  264. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  265. int
  266. WinFStat64(const int h0, struct WinStat64 *const stp)
  267. {
  268.     HANDLE h;
  269.     __int64 fSize;
  270.     DWORD fSize1, fSize2;
  271.     struct _stat st32;
  272.     DWORD winErr;
  273.  
  274.     h = (HANDLE) _get_osfhandle(h0);
  275.     if (h == INVALID_HANDLE_VALUE)
  276.         return (-1);
  277.  
  278.     memset(stp, 0, sizeof(struct WinStat64));
  279.     if (_fstat(h0, &st32) < 0)
  280.         return (-1);
  281.     stp->st_atime = st32.st_atime;
  282.     stp->st_ctime = st32.st_ctime;
  283.     stp->st_dev = st32.st_dev;
  284.     stp->st_gid = st32.st_gid;
  285.     stp->st_ino = st32.st_ino;
  286.     stp->st_mode = st32.st_mode;
  287.     stp->st_mtime = st32.st_mtime;
  288.     stp->st_nlink = st32.st_nlink;
  289.     stp->st_rdev = st32.st_rdev;
  290.     stp->st_uid = st32.st_uid;
  291.  
  292.     if (S_ISREG(stp->st_mode)) {
  293.         fSize = (_int64)0;
  294.         fSize1 = GetFileSize(h, &fSize2);
  295.         if ((fSize1 == 0xFFFFFFFF) && ((winErr = GetLastError()) != NO_ERROR))
  296.             goto return_err;
  297.  
  298.         fSize = ((__int64) fSize2 << 32) | (__int64) fSize1;
  299.         stp->st_size = fSize;
  300.     }
  301.     return (0);
  302.  
  303. return_err:
  304.     stp->st_size = (__int32) st32.st_size;
  305.     if ((winErr = GetLastError()) == ERROR_SHARING_VIOLATION) {
  306.         errno = EBUSY;
  307.     } else if ((winErr == ERROR_PATH_NOT_FOUND) || (winErr == ERROR_FILE_NOT_FOUND)) {
  308.         errno = ENOENT;
  309.     } else if (winErr == ERROR_INVALID_PARAMETER) {
  310.         errno = EINVAL;
  311.     } else {
  312.         errno = 100000 + winErr;
  313.     }
  314.     return (-1);
  315. }    /* WinFStat64 */
  316.  
  317.  
  318.  
  319.  
  320. int
  321. WinStat64(const char *const path, struct WinStat64 *const stp)
  322. {
  323.     HANDLE h;
  324.     __int64 fSize;
  325.     DWORD fSize1, fSize2;
  326.     struct _stat st32;
  327.     DWORD winErr;
  328.  
  329.     memset(stp, 0, sizeof(struct WinStat64));
  330.     if (_stat(path, &st32) < 0)
  331.         return (-1);
  332.     stp->st_atime = st32.st_atime;
  333.     stp->st_ctime = st32.st_ctime;
  334.     stp->st_dev = st32.st_dev;
  335.     stp->st_gid = st32.st_gid;
  336.     stp->st_ino = st32.st_ino;
  337.     stp->st_mode = st32.st_mode;
  338.     stp->st_mtime = st32.st_mtime;
  339.     stp->st_nlink = st32.st_nlink;
  340.     stp->st_rdev = st32.st_rdev;
  341.     stp->st_uid = st32.st_uid;
  342.  
  343.     if (S_ISREG(stp->st_mode)) {
  344.         h = CreateFile(path,
  345.             0,                /* Not GENERIC_READ; use 0 for "query attributes only" mode */
  346.             0,
  347.             NULL,
  348.             OPEN_EXISTING,    /* fails if it doesn't exist */
  349.             0,
  350.             NULL
  351.             );
  352.         
  353.         if (h == INVALID_HANDLE_VALUE)
  354.             goto return_err;
  355.  
  356.         fSize = (_int64)0;
  357.         fSize1 = GetFileSize(h, &fSize2);
  358.         if ((fSize1 == 0xFFFFFFFF) && ((winErr = GetLastError()) != NO_ERROR))
  359.             goto return_err;
  360.  
  361.         fSize = ((__int64) fSize2 << 32) | (__int64) fSize1;
  362.         stp->st_size = fSize;
  363.         CloseHandle(h);
  364.     }
  365.     return (0);
  366.  
  367. return_err:
  368.     stp->st_size = (__int32) st32.st_size;
  369.     if ((winErr = GetLastError()) == ERROR_SHARING_VIOLATION) {
  370.         errno = EBUSY;
  371.     } else if ((winErr == ERROR_PATH_NOT_FOUND) || (winErr == ERROR_FILE_NOT_FOUND)) {
  372.         errno = ENOENT;
  373.     } else if (winErr == ERROR_INVALID_PARAMETER) {
  374.         errno = EINVAL;
  375.     } else {
  376.         errno = 100000 + winErr;
  377.     }
  378.  
  379.     if (h != INVALID_HANDLE_VALUE)
  380.         CloseHandle(h);
  381.     return (-1);
  382. }    /* WinStat64 */
  383.  
  384. #endif
  385.