Source code of Windows XP (NT5)
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.

218 lines
7.2 KiB

  1. /***
  2. *utime.c - set modification time for a file
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Sets the access/modification times for a file.
  8. *
  9. *Revision History:
  10. * 03-??-84 RLB initial version
  11. * 05-17-86 SKS ported to OS/2
  12. * 08-21-87 JCR error return if localtime() returns NULL.
  13. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  14. * 10-03-88 JCR 386: Change DOS calls to SYS calls
  15. * 10-04-88 JCR 386: Removed 'far' keyword
  16. * 10-10-88 GJF Made API names match DOSCALLS.H
  17. * 10-11-88 GJF Made API arg types match DOSCALLS.H
  18. * 04-12-89 JCR New syscall interface
  19. * 05-01-89 JCR Corrected OS/2 time/date interpretation
  20. * 05-25-89 JCR 386 OS/2 calls use '_syscall' calling convention
  21. * 08-16-89 PHG moved date validation above open() so file isn't left
  22. * open if the date is invalid
  23. * 03-20-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  24. * <cruntime.h>, removed #include <register.h>, removed
  25. * some leftover 16-bit support and fixed the copyright.
  26. * Also, cleaned up the formatting a bit.
  27. * 07-25-90 SBM Compiles cleanly with -W3 (added include, removed
  28. * unreferenced variable), removed '32' from API names
  29. * 10-04-90 GJF New-style function declarator.
  30. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  31. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  32. * 01-18-91 GJF ANSI naming.
  33. * 02-14-91 SRW Fix Mips compile error (_WIN32_)
  34. * 02-26-91 SRW Fix SetFileTime parameter ordering (_WIN32_)
  35. * 08-21-91 BWM Add _futime to set time on open file
  36. * 08-26-91 BWM Change _utime to call _futime
  37. * 05-19-92 DJM ifndef for POSIX build.
  38. * 08-18-92 SKS SystemTimeToFileTime now takes UTC/GMT, not local time.
  39. * Remove _CRUISER_ conditional
  40. * 04-02-93 GJF Changed interpretation of error on SetFileTime call.
  41. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  42. * 09-09-93 GJF Have Win32 convert from a local file time value to a
  43. * (system) file time value. This is symmetric with
  44. * _stat() and a better work-around for the Windows NT
  45. * bug in converting file times on FAT (applies DST
  46. * offset based on current time rather than the file's
  47. * time stamp).
  48. * 11-01-93 CFW Enable Unicode variant, rip out Cruiser.
  49. * 02-09-95 GJF Replaced WPRFLAG with _UNICODE.
  50. * 02-13-95 GJF Appended Mac version of source file (somewhat cleaned
  51. * up), with appropriate #ifdef-s.
  52. * 05-17-99 PML Remove all Macintosh support.
  53. * 10-27-99 GB Remove #inlcude <dostypes.h>
  54. *
  55. *******************************************************************************/
  56. #ifndef _POSIX_
  57. #include <cruntime.h>
  58. #include <sys/types.h>
  59. #include <sys/utime.h>
  60. #include <msdos.h>
  61. #include <time.h>
  62. #include <fcntl.h>
  63. #include <io.h>
  64. #include <dos.h>
  65. #include <oscalls.h>
  66. #include <errno.h>
  67. #include <stddef.h>
  68. #include <internal.h>
  69. #include <stdio.h>
  70. #include <tchar.h>
  71. /***
  72. *int _utime(pathname, time) - set modification time for file
  73. *
  74. *Purpose:
  75. * Sets the modification time for the file specified by pathname.
  76. * Only the modification time from the _utimbuf structure is used
  77. * under MS-DOS.
  78. *
  79. *Entry:
  80. * struct _utimbuf *time - new modification date
  81. *
  82. *Exit:
  83. * returns 0 if successful
  84. * returns -1 and sets errno if fails
  85. *
  86. *Exceptions:
  87. *
  88. *******************************************************************************/
  89. int __cdecl _tutime (
  90. const _TSCHAR *fname,
  91. struct _utimbuf *times
  92. )
  93. {
  94. int fh;
  95. int retval;
  96. /* open file, fname, since filedate system call needs a handle. Note
  97. * _utime definition says you must have write permission for the file
  98. * to change its time, so open file for write only. Also, must force
  99. * it to open in binary mode so we dont remove ^Z's from binary files.
  100. */
  101. if ((fh = _topen(fname, _O_RDWR | _O_BINARY)) < 0)
  102. return(-1);
  103. retval = _futime(fh, times);
  104. _close(fh);
  105. return(retval);
  106. }
  107. #ifndef _UNICODE
  108. /***
  109. *int _futime(fh, time) - set modification time for open file
  110. *
  111. *Purpose:
  112. * Sets the modification time for the open file specified by fh.
  113. * Only the modification time from the _utimbuf structure is used
  114. * under MS-DOS.
  115. *
  116. *Entry:
  117. * struct _utimbuf *time - new modification date
  118. *
  119. *Exit:
  120. * returns 0 if successful
  121. * returns -1 and sets errno if fails
  122. *
  123. *Exceptions:
  124. *
  125. *******************************************************************************/
  126. int __cdecl _futime (
  127. int fh,
  128. struct _utimbuf *times
  129. )
  130. {
  131. REG1 struct tm *tmb;
  132. SYSTEMTIME SystemTime;
  133. FILETIME LocalFileTime;
  134. FILETIME LastWriteTime;
  135. FILETIME LastAccessTime;
  136. struct _utimbuf deftimes;
  137. if (times == NULL) {
  138. time(&deftimes.modtime);
  139. deftimes.actime = deftimes.modtime;
  140. times = &deftimes;
  141. }
  142. if ((tmb = localtime(&times->modtime)) == NULL) {
  143. errno = EINVAL;
  144. return(-1);
  145. }
  146. SystemTime.wYear = (WORD)(tmb->tm_year + 1900);
  147. SystemTime.wMonth = (WORD)(tmb->tm_mon + 1);
  148. SystemTime.wDay = (WORD)(tmb->tm_mday);
  149. SystemTime.wHour = (WORD)(tmb->tm_hour);
  150. SystemTime.wMinute = (WORD)(tmb->tm_min);
  151. SystemTime.wSecond = (WORD)(tmb->tm_sec);
  152. SystemTime.wMilliseconds = 0;
  153. if ( !SystemTimeToFileTime( &SystemTime, &LocalFileTime ) ||
  154. !LocalFileTimeToFileTime( &LocalFileTime, &LastWriteTime ) )
  155. {
  156. errno = EINVAL;
  157. return(-1);
  158. }
  159. if ((tmb = localtime(&times->actime)) == NULL) {
  160. errno = EINVAL;
  161. return(-1);
  162. }
  163. SystemTime.wYear = (WORD)(tmb->tm_year + 1900);
  164. SystemTime.wMonth = (WORD)(tmb->tm_mon + 1);
  165. SystemTime.wDay = (WORD)(tmb->tm_mday);
  166. SystemTime.wHour = (WORD)(tmb->tm_hour);
  167. SystemTime.wMinute = (WORD)(tmb->tm_min);
  168. SystemTime.wSecond = (WORD)(tmb->tm_sec);
  169. SystemTime.wMilliseconds = 0;
  170. if ( !SystemTimeToFileTime( &SystemTime, &LocalFileTime ) ||
  171. !LocalFileTimeToFileTime( &LocalFileTime, &LastAccessTime ) )
  172. {
  173. errno = EINVAL;
  174. return(-1);
  175. }
  176. /* set the date via the filedate system call and return. failing
  177. * this call implies the new file times are not supported by the
  178. * underlying file system.
  179. */
  180. if (!SetFileTime((HANDLE)_get_osfhandle(fh),
  181. NULL,
  182. &LastAccessTime,
  183. &LastWriteTime
  184. ))
  185. {
  186. errno = EINVAL;
  187. return(-1);
  188. }
  189. return(0);
  190. }
  191. #endif /* _UNICODE */
  192. #endif /* _POSIX_ */