home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / findfile.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  7KB  |  269 lines

  1. /***
  2. *findfile.c - C find file functions
  3. *
  4. *       Copyright (c) 1991-1995, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines findfirst(), findnext(), and findclose().
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <oscalls.h>
  13. #include <errno.h>
  14. #include <io.h>
  15. #include <time.h>
  16. #include <ctime.h>
  17. #include <string.h>
  18. #include <internal.h>
  19. #include <tchar.h>
  20.  
  21. #ifndef _WIN32
  22. #error ERROR - ONLY WIN32 TARGET SUPPORTED!
  23. #endif  /* _WIN32 */
  24.  
  25. time_t __cdecl __timet_from_ft(FILETIME * pft);
  26.  
  27. /***
  28. *long findfirst(wildspec, finddata) - Find first matching file
  29. *
  30. *Purpose:
  31. *       Finds the first file matching a given wild card filespec and
  32. *       returns data about the file.
  33. *
  34. *Entry:
  35. *       char * wild - file spec optionally containing wild cards
  36. *
  37. *       struct _finddata_t * finddata - structure to receive file data
  38. *
  39. *Exit:
  40. *       Good return:
  41. *       Unique handle identifying the group of files matching the spec
  42. *
  43. *       Error return:
  44. *       Returns -1 and errno is set to error value
  45. *
  46. *Exceptions:
  47. *       None.
  48. *
  49. *******************************************************************************/
  50.  
  51. #ifdef _USE_INT64
  52.  
  53. long __cdecl _tfindfirsti64(
  54.         _TSCHAR * szWild,
  55.         struct _tfinddatai64_t * pfd
  56.         )
  57.  
  58. #else  /* _USE_INT64 */
  59.  
  60. long __cdecl _tfindfirst(
  61.         _TSCHAR * szWild,
  62.         struct _tfinddata_t * pfd
  63.         )
  64.  
  65. #endif  /* _USE_INT64 */
  66.  
  67. {
  68.     WIN32_FIND_DATA wfd;
  69.     HANDLE          hFile;
  70.     DWORD           err;
  71.  
  72.     if ((hFile = FindFirstFile(szWild, &wfd)) == INVALID_HANDLE_VALUE) {
  73.         err = GetLastError();
  74.         switch (err) {
  75.             case ERROR_NO_MORE_FILES:
  76.             case ERROR_FILE_NOT_FOUND:
  77.             case ERROR_PATH_NOT_FOUND:
  78.                 errno = ENOENT;
  79.                 break;
  80.  
  81.             case ERROR_NOT_ENOUGH_MEMORY:
  82.                 errno = ENOMEM;
  83.                 break;
  84.  
  85.             default:
  86.                 errno = EINVAL;
  87.                 break;
  88.         }
  89.         return (-1);
  90.     }
  91.  
  92.     pfd->attrib       = (wfd.dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
  93.                       ? 0 : wfd.dwFileAttributes;
  94.     pfd->time_create  = __timet_from_ft(&wfd.ftCreationTime);
  95.     pfd->time_access  = __timet_from_ft(&wfd.ftLastAccessTime);
  96.     pfd->time_write   = __timet_from_ft(&wfd.ftLastWriteTime);
  97.  
  98. #ifdef _USE_INT64
  99.     pfd->size         = ((__int64)(wfd.nFileSizeHigh)) * (0x100000000i64) +
  100.                         (__int64)(wfd.nFileSizeLow);
  101. #else  /* _USE_INT64 */
  102.     pfd->size         = wfd.nFileSizeLow;
  103. #endif  /* _USE_INT64 */
  104.  
  105.     _tcscpy(pfd->name, wfd.cFileName);
  106.  
  107.     return ((long)hFile);
  108. }
  109.  
  110. /***
  111. *int _findnext(hfind, finddata) - Find next matching file
  112. *
  113. *Purpose:
  114. *       Finds the next file matching a given wild card filespec and
  115. *       returns data about the file.
  116. *
  117. *Entry:
  118. *       hfind - handle from _findfirst
  119. *
  120. *       struct _finddata_t * finddata - structure to receive file data
  121. *
  122. *Exit:
  123. *       Good return:
  124. *       0 if file found
  125. *       -1 if error or file not found
  126. *       errno set
  127. *
  128. *Exceptions:
  129. *       None.
  130. *
  131. *******************************************************************************/
  132.  
  133. #ifdef _USE_INT64
  134.  
  135. int __cdecl _tfindnexti64(long hFile, struct _tfinddatai64_t * pfd)
  136.  
  137. #else  /* _USE_INT64 */
  138.  
  139. int __cdecl _tfindnext(long hFile, struct _tfinddata_t * pfd)
  140.  
  141. #endif  /* _USE_INT64 */
  142.  
  143. {
  144.     WIN32_FIND_DATA wfd;
  145.     DWORD           err;
  146.  
  147.     if (!FindNextFile((HANDLE)hFile, &wfd)) {
  148.         err = GetLastError();
  149.         switch (err) {
  150.             case ERROR_NO_MORE_FILES:
  151.             case ERROR_FILE_NOT_FOUND:
  152.             case ERROR_PATH_NOT_FOUND:
  153.                 errno = ENOENT;
  154.                 break;
  155.  
  156.             case ERROR_NOT_ENOUGH_MEMORY:
  157.                 errno = ENOMEM;
  158.                 break;
  159.  
  160.             default:
  161.                 errno = EINVAL;
  162.                 break;
  163.         }
  164.         return (-1);
  165.     }
  166.  
  167.     pfd->attrib       = (wfd.dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
  168.                       ? 0 : wfd.dwFileAttributes;
  169.     pfd->time_create  = __timet_from_ft(&wfd.ftCreationTime);
  170.     pfd->time_access  = __timet_from_ft(&wfd.ftLastAccessTime);
  171.     pfd->time_write   = __timet_from_ft(&wfd.ftLastWriteTime);
  172.  
  173. #ifdef _USE_INT64
  174.     pfd->size         = ((__int64)(wfd.nFileSizeHigh)) * (0x100000000i64) +
  175.                         (__int64)(wfd.nFileSizeLow);
  176. #else  /* _USE_INT64 */
  177.     pfd->size         = wfd.nFileSizeLow;
  178. #endif  /* _USE_INT64 */
  179.  
  180.     _tcscpy(pfd->name, wfd.cFileName);
  181.  
  182.     return (0);
  183. }
  184.  
  185. #if !defined (_UNICODE) && !defined (_USE_INT64)
  186.  
  187. /***
  188. *int _findclose(hfind) - Release resources of find
  189. *
  190. *Purpose:
  191. *       Releases resources of a group of files found by _findfirst and
  192. *       _findnext
  193. *
  194. *Entry:
  195. *       hfind - handle from _findfirst
  196. *
  197. *Exit:
  198. *       Good return:
  199. *       0 if success
  200. *       -1 if fail, errno set
  201. *
  202. *Exceptions:
  203. *       None.
  204. *
  205. *******************************************************************************/
  206.  
  207. int __cdecl _findclose(long hFile)
  208. {
  209.     if (!FindClose((HANDLE)hFile)) {
  210.         errno = EINVAL;
  211.         return (-1);
  212.     }
  213.     return (0);
  214. }
  215.  
  216.  
  217. /***
  218. *time_t _fttotimet(ft) - convert Win32 file time to Xenix time
  219. *
  220. *Purpose:
  221. *       converts a Win32 file time value to Xenix time_t
  222. *
  223. *       Note: We cannot directly use the ft value. In Win32, the file times
  224. *       returned by the API are ambiguous. In Windows NT, they are UTC. In
  225. *       Win32S, and probably also Win32C, they are local time values. Thus,
  226. *       the value in ft must be converted to a local time value (by an API)
  227. *       before we can use it.
  228. *
  229. *Entry:
  230. *       int yr, mo, dy -        date
  231. *       int hr, mn, sc -        time
  232. *
  233. *Exit:
  234. *       returns Xenix time value
  235. *
  236. *Exceptions:
  237. *
  238. *******************************************************************************/
  239.  
  240. time_t __cdecl __timet_from_ft(FILETIME * pft)
  241. {
  242.     SYSTEMTIME st;
  243.     FILETIME lft;
  244.  
  245.     /* 0 FILETIME returns a -1 time_t */
  246.  
  247.     if (!pft->dwLowDateTime && !pft->dwHighDateTime) {
  248.         return (-1L);
  249.     }
  250.  
  251.     /*
  252.      * Convert to a broken down local time value
  253.      */
  254.     if ( !FileTimeToLocalFileTime(pft, &lft) ||
  255.          !FileTimeToSystemTime(&lft, &st) )
  256.     {
  257.         return (-1L);
  258.     }
  259.  
  260.     return ( __loctotime_t(st.wYear,
  261.                            st.wMonth,
  262.                            st.wDay,
  263.                            st.wHour,
  264.                            st.wMinute,
  265.                            st.wSecond) );
  266. }
  267.  
  268. #endif  /* !defined (_UNICODE) && !defined (_USE_INT64) */
  269.