home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / utime.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  9KB  |  339 lines

  1. /***
  2. *utime.c - set modification time for a file
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Sets the access/modification times for a file.
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _WIN32
  12.  
  13.  
  14.  
  15. #include <cruntime.h>
  16. #include <sys/types.h>
  17. #include <sys/utime.h>
  18. #include <msdos.h>
  19. #include <dostypes.h>
  20. #include <time.h>
  21. #include <fcntl.h>
  22. #include <io.h>
  23. #include <dos.h>
  24. #include <oscalls.h>
  25. #include <errno.h>
  26. #include <stddef.h>
  27. #include <internal.h>
  28.  
  29. #include <stdio.h>
  30. #include <tchar.h>
  31.  
  32. /***
  33. *int _utime(pathname, time) - set modification time for file
  34. *
  35. *Purpose:
  36. *       Sets the modification time for the file specified by pathname.
  37. *       Only the modification time from the _utimbuf structure is used
  38. *       under MS-DOS.
  39. *
  40. *Entry:
  41. *       struct _utimbuf *time - new modification date
  42. *
  43. *Exit:
  44. *       returns 0 if successful
  45. *       returns -1 and sets errno if fails
  46. *
  47. *Exceptions:
  48. *
  49. *******************************************************************************/
  50.  
  51. int __cdecl _tutime (
  52.         const _TSCHAR *fname,
  53.         struct _utimbuf *times
  54.         )
  55. {
  56.         int fh;
  57.         int retval;
  58.  
  59.         /* open file, fname, since filedate system call needs a handle.  Note
  60.          * _utime definition says you must have write permission for the file
  61.          * to change its time, so open file for write only.  Also, must force
  62.          * it to open in binary mode so we dont remove ^Z's from binary files.
  63.          */
  64.  
  65.  
  66.         if ((fh = _topen(fname, _O_RDWR | _O_BINARY)) < 0)
  67.                 return(-1);
  68.  
  69.         retval = _futime(fh, times);
  70.  
  71.         _close(fh);
  72.         return(retval);
  73. }
  74.  
  75. #ifndef _UNICODE
  76.  
  77. /***
  78. *int _futime(fh, time) - set modification time for open file
  79. *
  80. *Purpose:
  81. *       Sets the modification time for the open file specified by fh.
  82. *       Only the modification time from the _utimbuf structure is used
  83. *       under MS-DOS.
  84. *
  85. *Entry:
  86. *       struct _utimbuf *time - new modification date
  87. *
  88. *Exit:
  89. *       returns 0 if successful
  90. *       returns -1 and sets errno if fails
  91. *
  92. *Exceptions:
  93. *
  94. *******************************************************************************/
  95.  
  96. int __cdecl _futime (
  97.         int fh,
  98.         struct _utimbuf *times
  99.         )
  100. {
  101.         REG1 struct tm *tmb;
  102.  
  103.         SYSTEMTIME SystemTime;
  104.         FILETIME LocalFileTime;
  105.         FILETIME LastWriteTime;
  106.         FILETIME LastAccessTime;
  107.         struct _utimbuf deftimes;
  108.  
  109.         if (times == NULL) {
  110.                 time(&deftimes.modtime);
  111.                 deftimes.actime = deftimes.modtime;
  112.                 times = &deftimes;
  113.         }
  114.  
  115.         if ((tmb = localtime(×->modtime)) == NULL) {
  116.                 errno = EINVAL;
  117.                 return(-1);
  118.         }
  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.  
  128.         if ( !SystemTimeToFileTime( &SystemTime, &LocalFileTime ) ||
  129.              !LocalFileTimeToFileTime( &LocalFileTime, &LastWriteTime ) )
  130.         {
  131.                 errno = EINVAL;
  132.                 return(-1);
  133.         }
  134.  
  135.         if ((tmb = localtime(×->actime)) == NULL) {
  136.                 errno = EINVAL;
  137.                 return(-1);
  138.         }
  139.  
  140.         SystemTime.wYear   = (WORD)(tmb->tm_year + 1900);
  141.         SystemTime.wMonth  = (WORD)(tmb->tm_mon + 1);
  142.         SystemTime.wDay    = (WORD)(tmb->tm_mday);
  143.         SystemTime.wHour   = (WORD)(tmb->tm_hour);
  144.         SystemTime.wMinute = (WORD)(tmb->tm_min);
  145.         SystemTime.wSecond = (WORD)(tmb->tm_sec);
  146.         SystemTime.wMilliseconds = 0;
  147.  
  148.         if ( !SystemTimeToFileTime( &SystemTime, &LocalFileTime ) ||
  149.              !LocalFileTimeToFileTime( &LocalFileTime, &LastAccessTime ) )
  150.         {
  151.                 errno = EINVAL;
  152.                 return(-1);
  153.         }
  154.  
  155.         /* set the date via the filedate system call and return. failing
  156.          * this call implies the new file times are not supported by the
  157.          * underlying file system.
  158.          */
  159.  
  160.         if (!SetFileTime((HANDLE)_get_osfhandle(fh),
  161.                                 NULL,
  162.                                 &LastAccessTime,
  163.                                 &LastWriteTime
  164.                                ))
  165.         {
  166.                 errno = EINVAL;
  167.                 return(-1);
  168.         }
  169.  
  170.         return(0);
  171. }
  172.  
  173. #endif  /* _UNICODE */
  174.  
  175.  
  176.  
  177. #else  /* _WIN32 */
  178.  
  179. #if defined (_M_MPPC) || defined (_M_M68K)
  180.  
  181.  
  182. #include <cruntime.h>
  183. #include <internal.h>
  184. #include <sys/types.h>
  185. #include <sys/utime.h>
  186. #include <time.h>
  187. #include <errno.h>
  188. #include <string.h>
  189. #include <macos\osutils.h>
  190. #include <macos\files.h>
  191. #include <macos\errors.h>
  192.  
  193. /***
  194. *int _utime(pathname, time) - set modification time for file
  195. *
  196. *Purpose:
  197. *       Sets the modification time for the file specified by pathname.
  198. *       Only the modification time from the _utimbuf structure is used
  199. *       under MS-DOS.
  200. *
  201. *Entry:
  202. *       struct _utimbuf *time - new modification date
  203. *
  204. *Exit:
  205. *       returns 0 if successful
  206. *       returns -1 and sets errno if fails
  207. *
  208. *Exceptions:
  209. *
  210. *******************************************************************************/
  211. void __Ansi2MacTime(struct tm *tmb, DateTimeRec * pdt);
  212.  
  213. int __cdecl _utime (
  214.         const char *fname,
  215.         struct _utimbuf *times
  216.         )
  217. {
  218.         CInfoPBRec cinfoPB;
  219.         time_t timeval;
  220.         OSErr osErr;
  221.         struct tm *tmb;
  222.         unsigned long lTime;
  223.         DateTimeRec dt;
  224.         char szBuf[256];
  225.  
  226.         strcpy(szBuf, fname);
  227.         cinfoPB.dirInfo.ioVRefNum = 0;
  228.         cinfoPB.dirInfo.ioDrDirID = 0;
  229.         cinfoPB.dirInfo.ioFDirIndex = 0;
  230.         cinfoPB.dirInfo.ioNamePtr = _c2pstr(szBuf);
  231.         osErr = PBGetCatInfoSync(&cinfoPB);
  232.         switch (osErr)
  233.                 {
  234.                 case noErr:
  235.                         break;
  236.                 case fnfErr:
  237.                 case extFSErr:
  238.                 case bdNamErr:
  239.                 case ioErr:
  240.                 case nsvErr:
  241.                 case paramErr:
  242.                         errno = ENOENT;
  243.                         return -1;
  244.                 default:
  245.                         return -1;
  246.                  }
  247.  
  248.         //file locked or dir
  249.         if (cinfoPB.dirInfo.ioFlAttrib & 0x1 ||
  250.                 cinfoPB.dirInfo.ioFlAttrib & 0x10)
  251.                 {
  252.                 /* no write permission on file, return error */
  253.                 errno = EACCES;
  254.                 return -1;
  255.                 }
  256.  
  257.         if (times == NULL)
  258.                 {
  259.                 GetDateTime(&lTime);
  260.                 timeval = (time_t)lTime;
  261.                 }
  262.         else
  263.                 {
  264.                 timeval = times->modtime;
  265.                 if ((tmb = localtime(&timeval)) == NULL)
  266.                         {
  267.                         errno = EINVAL;
  268.                         return -1;
  269.                         }
  270.                 //convert ANSI date/time to Mac date/time
  271.                 __Ansi2MacTime(tmb, &dt);
  272.                 Date2Secs(&dt, &timeval);
  273.                 }
  274.  
  275.    cinfoPB.dirInfo.ioVRefNum = 0;
  276.    cinfoPB.dirInfo.ioDrDirID = 0;
  277.    cinfoPB.dirInfo.ioFDirIndex = 0;
  278.    cinfoPB.dirInfo.ioNamePtr = szBuf;
  279.    cinfoPB.dirInfo.ioDrMdDat = timeval;
  280.  
  281.    osErr = PBSetCatInfoSync(&cinfoPB);
  282.         switch (osErr)
  283.                 {
  284.                 case noErr:
  285.                    errno = 0;
  286.                    break;
  287.                 case extFSErr:
  288.                 case bdNamErr:
  289.                 case fnfErr:
  290.                 case ioErr:
  291.                 case nsvErr:
  292.                     errno = ENOENT;
  293.                     return -1;
  294.                 case fLckdErr:
  295.                 case vLckdErr:
  296.                 case wPrErr:
  297.                     errno = EACCES;
  298.                     return -1;
  299.                 default:
  300.                     return -1;
  301.                 }
  302.  
  303.         return 0;
  304. }
  305.  
  306. /***
  307. *void __Ansi2MacTime(struct tm *, DateTimeRec *) - internal
  308. *
  309. *
  310. *Purpose:
  311. *  convert ansi time to Mac time
  312. *
  313. *Entry:
  314. *       struct tm *tmb - ansi time
  315. *  DateTimeRec - Mac time
  316. *
  317. *Exit:
  318. *
  319. *Exceptions:
  320. *
  321. *******************************************************************************/
  322.  
  323. void __Ansi2MacTime(struct tm *tmb, DateTimeRec * pdt)
  324. {
  325.       pdt->second =  tmb->tm_sec;
  326.       pdt->minute =  tmb->tm_min;
  327.       pdt->hour = tmb->tm_hour;
  328.       pdt->day = tmb->tm_mday;
  329.       pdt->month = tmb->tm_mon + 1;
  330.       pdt->year = tmb->tm_year + 1900;
  331.       pdt->dayOfWeek = tmb->tm_wday + 1;
  332.       return;
  333. }
  334.  
  335.  
  336. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  337.  
  338. #endif  /* _WIN32 */
  339.