Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.1 KiB

  1. /***
  2. *findf64.c - C find file functions
  3. *
  4. * Copyright (c) 1998-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines _findfirst64(), _findnext64(), and _findclose64().
  8. *
  9. *Revision History:
  10. * 05-28-98 GJF Created
  11. * 05-03-99 PML Update for 64-bit merge - long -> intptr_t.
  12. *
  13. *******************************************************************************/
  14. #include <cruntime.h>
  15. #include <oscalls.h>
  16. #include <errno.h>
  17. #include <io.h>
  18. #include <time.h>
  19. #include <ctime.h>
  20. #include <string.h>
  21. #include <internal.h>
  22. #include <tchar.h>
  23. #ifndef _WIN32
  24. #error ERROR - ONLY WIN32 TARGET SUPPORTED!
  25. #endif
  26. __time64_t __cdecl __time64_t_from_ft(FILETIME * pft);
  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 _finddata64_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. intptr_t __cdecl _tfindfirst64(
  51. const _TSCHAR * szWild,
  52. struct _tfinddata64_t * pfd
  53. )
  54. {
  55. WIN32_FIND_DATA wfd;
  56. HANDLE hFile;
  57. DWORD err;
  58. if ((hFile = FindFirstFile(szWild, &wfd)) == INVALID_HANDLE_VALUE) {
  59. err = GetLastError();
  60. switch (err) {
  61. case ERROR_NO_MORE_FILES:
  62. case ERROR_FILE_NOT_FOUND:
  63. case ERROR_PATH_NOT_FOUND:
  64. errno = ENOENT;
  65. break;
  66. case ERROR_NOT_ENOUGH_MEMORY:
  67. errno = ENOMEM;
  68. break;
  69. default:
  70. errno = EINVAL;
  71. break;
  72. }
  73. return (-1);
  74. }
  75. pfd->attrib = (wfd.dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
  76. ? 0 : wfd.dwFileAttributes;
  77. pfd->time_create = __time64_t_from_ft(&wfd.ftCreationTime);
  78. pfd->time_access = __time64_t_from_ft(&wfd.ftLastAccessTime);
  79. pfd->time_write = __time64_t_from_ft(&wfd.ftLastWriteTime);
  80. pfd->size = ((__int64)(wfd.nFileSizeHigh)) * (0x100000000i64) +
  81. (__int64)(wfd.nFileSizeLow);
  82. _tcscpy(pfd->name, wfd.cFileName);
  83. return ((intptr_t)hFile);
  84. }
  85. /***
  86. *int _findnext(hfind, finddata) - Find next matching file
  87. *
  88. *Purpose:
  89. * Finds the next file matching a given wild card filespec and
  90. * returns data about the file.
  91. *
  92. *Entry:
  93. * hfind - handle from _findfirst
  94. *
  95. * struct _finddata64_t * finddata - structure to receive file data
  96. *
  97. *Exit:
  98. * Good return:
  99. * 0 if file found
  100. * -1 if error or file not found
  101. * errno set
  102. *
  103. *Exceptions:
  104. * None.
  105. *
  106. *******************************************************************************/
  107. int __cdecl _tfindnext64(intptr_t hFile, struct _tfinddata64_t * pfd)
  108. {
  109. WIN32_FIND_DATA wfd;
  110. DWORD err;
  111. if (!FindNextFile((HANDLE)hFile, &wfd)) {
  112. err = GetLastError();
  113. switch (err) {
  114. case ERROR_NO_MORE_FILES:
  115. case ERROR_FILE_NOT_FOUND:
  116. case ERROR_PATH_NOT_FOUND:
  117. errno = ENOENT;
  118. break;
  119. case ERROR_NOT_ENOUGH_MEMORY:
  120. errno = ENOMEM;
  121. break;
  122. default:
  123. errno = EINVAL;
  124. break;
  125. }
  126. return (-1);
  127. }
  128. pfd->attrib = (wfd.dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
  129. ? 0 : wfd.dwFileAttributes;
  130. pfd->time_create = __time64_t_from_ft(&wfd.ftCreationTime);
  131. pfd->time_access = __time64_t_from_ft(&wfd.ftLastAccessTime);
  132. pfd->time_write = __time64_t_from_ft(&wfd.ftLastWriteTime);
  133. pfd->size = ((__int64)(wfd.nFileSizeHigh)) * (0x100000000i64) +
  134. (__int64)(wfd.nFileSizeLow);
  135. _tcscpy(pfd->name, wfd.cFileName);
  136. return (0);
  137. }
  138. #if !defined(_UNICODE) && !defined(_USE_INT64)
  139. /***
  140. *time64_t __time64_t_from_ft(ft) - convert Win32 file time to Xenix time
  141. *
  142. *Purpose:
  143. * converts a Win32 file time value to Xenix time_t
  144. *
  145. * Note: We cannot directly use the ft value. In Win32, the file times
  146. * returned by the API are ambiguous. In Windows NT, they are UTC. In
  147. * Win32S, and probably also Win32C, they are local time values. Thus,
  148. * the value in ft must be converted to a local time value (by an API)
  149. * before we can use it.
  150. *
  151. *Entry:
  152. * int yr, mo, dy - date
  153. * int hr, mn, sc - time
  154. *
  155. *Exit:
  156. * returns Xenix time value
  157. *
  158. *Exceptions:
  159. *
  160. *******************************************************************************/
  161. __time64_t __cdecl __time64_t_from_ft(FILETIME * pft)
  162. {
  163. SYSTEMTIME st;
  164. FILETIME lft;
  165. /* 0 FILETIME returns a -1 time_t */
  166. if (!pft->dwLowDateTime && !pft->dwHighDateTime) {
  167. return ((__time64_t)-1);
  168. }
  169. /*
  170. * Convert to a broken down local time value
  171. */
  172. if ( !FileTimeToLocalFileTime(pft, &lft) ||
  173. !FileTimeToSystemTime(&lft, &st) )
  174. {
  175. return ((__time64_t)-1);
  176. }
  177. return ( __loctotime64_t(st.wYear,
  178. st.wMonth,
  179. st.wDay,
  180. st.wHour,
  181. st.wMinute,
  182. st.wSecond,
  183. -1) );
  184. }
  185. #endif