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.

173 lines
4.8 KiB

  1. /***
  2. *utime64.c - set modification time for a file
  3. *
  4. * Copyright (c) 1998-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Sets the access/modification times for a file.
  8. *
  9. *Revision History:
  10. * 05-28-98 GJF Created.
  11. *
  12. *******************************************************************************/
  13. #ifndef _POSIX_
  14. #include <cruntime.h>
  15. #include <sys/types.h>
  16. #include <sys/utime.h>
  17. #include <msdos.h>
  18. #include <time.h>
  19. #include <fcntl.h>
  20. #include <io.h>
  21. #include <dos.h>
  22. #include <oscalls.h>
  23. #include <errno.h>
  24. #include <stddef.h>
  25. #include <internal.h>
  26. #include <stdio.h>
  27. #include <tchar.h>
  28. /***
  29. *int _utime64(pathname, time) - set modification time for file
  30. *
  31. *Purpose:
  32. * Sets the modification time for the file specified by pathname.
  33. * Only the modification time from the _utimbuf structure is used
  34. * under MS-DOS.
  35. *
  36. *Entry:
  37. * struct __utimbuf64 *time - new modification date
  38. *
  39. *Exit:
  40. * returns 0 if successful
  41. * returns -1 and sets errno if fails
  42. *
  43. *Exceptions:
  44. *
  45. *******************************************************************************/
  46. int __cdecl _tutime64 (
  47. const _TSCHAR *fname,
  48. struct __utimbuf64 *times
  49. )
  50. {
  51. int fh;
  52. int retval;
  53. /* open file, fname, since filedate system call needs a handle. Note
  54. * _utime definition says you must have write permission for the file
  55. * to change its time, so open file for write only. Also, must force
  56. * it to open in binary mode so we dont remove ^Z's from binary files.
  57. */
  58. if ((fh = _topen(fname, _O_RDWR | _O_BINARY)) < 0)
  59. return(-1);
  60. retval = _futime64(fh, times);
  61. _close(fh);
  62. return(retval);
  63. }
  64. #ifndef _UNICODE
  65. /***
  66. *int __futime64(fh, time) - set modification time for open file
  67. *
  68. *Purpose:
  69. * Sets the modification time for the open file specified by fh.
  70. * Only the modification time from the _utimbuf64 structure is used
  71. * under MS-DOS.
  72. *
  73. *Entry:
  74. * struct __utimbuf64 *time - new modification date
  75. *
  76. *Exit:
  77. * returns 0 if successful
  78. * returns -1 and sets errno if fails
  79. *
  80. *Exceptions:
  81. *
  82. *******************************************************************************/
  83. int __cdecl _futime64 (
  84. int fh,
  85. struct __utimbuf64 *times
  86. )
  87. {
  88. struct tm *tmb;
  89. SYSTEMTIME SystemTime;
  90. FILETIME LocalFileTime;
  91. FILETIME LastWriteTime;
  92. FILETIME LastAccessTime;
  93. struct __utimbuf64 deftimes;
  94. if (times == NULL) {
  95. _time64(&deftimes.modtime);
  96. deftimes.actime = deftimes.modtime;
  97. times = &deftimes;
  98. }
  99. if ((tmb = _localtime64(&times->modtime)) == NULL) {
  100. errno = EINVAL;
  101. return(-1);
  102. }
  103. SystemTime.wYear = (WORD)(tmb->tm_year + 1900);
  104. SystemTime.wMonth = (WORD)(tmb->tm_mon + 1);
  105. SystemTime.wDay = (WORD)(tmb->tm_mday);
  106. SystemTime.wHour = (WORD)(tmb->tm_hour);
  107. SystemTime.wMinute = (WORD)(tmb->tm_min);
  108. SystemTime.wSecond = (WORD)(tmb->tm_sec);
  109. SystemTime.wMilliseconds = 0;
  110. if ( !SystemTimeToFileTime( &SystemTime, &LocalFileTime ) ||
  111. !LocalFileTimeToFileTime( &LocalFileTime, &LastWriteTime ) )
  112. {
  113. errno = EINVAL;
  114. return(-1);
  115. }
  116. if ((tmb = _localtime64(&times->actime)) == NULL) {
  117. errno = EINVAL;
  118. return(-1);
  119. }
  120. SystemTime.wYear = (WORD)(tmb->tm_year + 1900);
  121. SystemTime.wMonth = (WORD)(tmb->tm_mon + 1);
  122. SystemTime.wDay = (WORD)(tmb->tm_mday);
  123. SystemTime.wHour = (WORD)(tmb->tm_hour);
  124. SystemTime.wMinute = (WORD)(tmb->tm_min);
  125. SystemTime.wSecond = (WORD)(tmb->tm_sec);
  126. SystemTime.wMilliseconds = 0;
  127. if ( !SystemTimeToFileTime( &SystemTime, &LocalFileTime ) ||
  128. !LocalFileTimeToFileTime( &LocalFileTime, &LastAccessTime ) )
  129. {
  130. errno = EINVAL;
  131. return(-1);
  132. }
  133. /* set the date via the filedate system call and return. failing
  134. * this call implies the new file times are not supported by the
  135. * underlying file system.
  136. */
  137. if (!SetFileTime((HANDLE)_get_osfhandle(fh),
  138. NULL,
  139. &LastAccessTime,
  140. &LastWriteTime
  141. ))
  142. {
  143. errno = EINVAL;
  144. return(-1);
  145. }
  146. return(0);
  147. }
  148. #endif /* _UNICODE */
  149. #endif /* _POSIX_ */