home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / UTIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.9 KB  |  133 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - utime.c
  3.  *
  4.  * function(s)
  5.  *        utime - set file access and modification times
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <_io.h>
  18. #include <dos.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <utime.h>
  23.  
  24. static char Days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  25.  
  26. /*---------------------------------------------------------------------*
  27.  
  28. Name            utime - set file modification time
  29.  
  30. Usage           int utime(char *path, struct utimbuf *times);
  31.  
  32. Prototype in    utime.h
  33.  
  34. Description     Sets the modification time for the file 'path'
  35.                 to the UNIX-format time in times->modtime.  The
  36.                 access time times->actime is ignored on DOS.
  37.                 If times is NULL, the current time is used.
  38.  
  39. Return value    If successful, 0 is returned.  Otherwise -1 is returned
  40.                 and errno is set as follows:
  41.  
  42.                 ENOENT          File not found
  43.                 EMFILE          Too many open files
  44.                 EACCESS         Permission denied
  45.  
  46. *---------------------------------------------------------------------*/
  47.  
  48. int _FARFUNC utime(char *path, struct utimbuf *times)
  49. {
  50.     time_t tim;
  51.     struct dosdate_t dd;
  52.     struct dostime_t dt;
  53.     unsigned fdate, ftime;
  54.     unsigned err;
  55.     int handle;
  56.  
  57.     /* We have to open the file for writing before we can modify
  58.      * its time.
  59.      */
  60.     if (_dos_open(path,O_WRONLY,&handle) != 0)
  61.             return (-1);
  62.  
  63.     /* If times not supplied, use current time.
  64.      */
  65.     if (times == NULL)
  66.         {
  67.             _dos_getdate(&dd);
  68.             _dos_gettime(&dt);
  69.         }
  70.     else
  71.         {
  72.         /* Convert supplied modification time to a DOS date and time.
  73.          * This code was stolen from unixtodos().
  74.          */
  75.         tim = times->modtime;
  76.  
  77.         tzset();                                /* get timezone info */
  78.  
  79.         tim -= 24L * 60L * 60L * 3652L + timezone;
  80.         dt.second = tim % 60;
  81.         tim /= 60;                              /* Time in minutes */
  82.         dt.minute = tim % 60;
  83.         tim /= 60;                              /* Time in hours */
  84.         dd.year = 1980 + (int)((tim / (1461L * 24L)) << 2);
  85.         tim %= 1461L * 24L;
  86.         if (tim >= 366 * 24)
  87.         {
  88.                 tim -= 366 * 24;
  89.                 dd.year++;
  90.                 dd.year += (int)(tim / (365 * 24));
  91.                 tim %= 365 * 24;
  92.         }
  93.         if (daylight && __isDST( (int)(tim % 24), (int)(tim / 24), 0, dd.year-1970 ))
  94.                 tim++;
  95.         dt.hour = tim % 24;
  96.         tim /= 24;                              /* Time in days */
  97.         tim++;
  98.         if ((dd.year & 3) == 0)
  99.         {
  100.                 if (tim > 60)
  101.                         tim--;
  102.                 else
  103.                 if (tim == 60)
  104.                 {
  105.                         dd.month = 2;
  106.                         dd.day = 29;
  107.                         goto done;
  108.                 }
  109.         }
  110.         for (dd.month = 0; Days[dd.month] < tim; dd.month++)
  111.                 tim -= Days[dd.month];
  112.         dd.month++;
  113.         dd.day = tim;
  114. done:   ;
  115.     }
  116.  
  117.     /* Convert the date and time structures in the bitmapped
  118.      * file date and time.
  119.      */
  120.     ftime = ((unsigned)dt.second >> 1)
  121.           | ((unsigned)dt.minute << 5)
  122.           | ((unsigned)dt.hour   << 11);
  123.     fdate = ((unsigned)dd.day)
  124.           | ((unsigned)dd.month << 5)
  125.           | ((unsigned)(dd.year - 1980) << 9);
  126.  
  127.     /* Set the file time and date, close the file.
  128.      */
  129.     err = _dos_setftime(handle,fdate,ftime);
  130.     _dos_close(handle);
  131.     return (err == 0 ? 0 : -1);
  132. }
  133.