home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / utime.c < prev    next >
C/C++ Source or Header  |  1993-06-01  |  2KB  |  120 lines

  1. /* utime -- set the file modification time of the given file
  2.  * according to the time given; a time of 0 means the current
  3.  * time.
  4.  *
  5.  * stime -- set the current time to the value given.
  6.  *
  7.  * All times are in Unix format, i.e. seconds since to
  8.  * midnight, January 1, 1970 GMT
  9.  *
  10.  * written by Eric R. Smith, and placed in the public domain.
  11.  *
  12.  */
  13.  
  14. #include <compiler.h>
  15. #include <limits.h>
  16. #include <time.h>
  17. #include <errno.h>
  18. #include <osbind.h>
  19. #include <mintbind.h>
  20. #include <ioctl.h>
  21. #include <assert.h>
  22. #ifdef __TURBOC__
  23. #include <sys\types.h>
  24. #else
  25. #include <sys/types.h>
  26. #endif
  27. #include "lib.h"
  28.  
  29. extern int __mint;
  30.  
  31. time_t _dostime __PROTO((time_t t));
  32.  
  33. /* convert a Unix time into a DOS time. The longword returned contains
  34.    the time word first, then the date word */
  35.  
  36. time_t
  37. _dostime(t)
  38.     time_t t;
  39. {
  40.         time_t time, date;
  41.     struct tm *ctm;
  42.  
  43.     if ((ctm = localtime(&t)) == NULL)
  44.         return 0;
  45.     time = (ctm->tm_hour << 11) | (ctm->tm_min << 5) | (ctm->tm_sec >> 1);
  46.     date = ((ctm->tm_year - 80) & 0x7f) << 9;
  47.     date |= ((ctm->tm_mon+1) << 5) | (ctm->tm_mday);
  48.     return (time << 16) | date;
  49. }
  50.  
  51. int
  52. utime(_filename, tset)
  53.       const char *_filename;
  54.       const struct utimbuf *tset;
  55. {
  56.     int fh;
  57.     time_t actime, modtime;
  58.     unsigned long dtime;    /* dos time equivalent */
  59.     
  60.     char filename[PATH_MAX];
  61.     struct _mutimbuf settime;
  62.     long res;
  63.  
  64.     if (tset)
  65.     {
  66.         modtime = tset->modtime;
  67.         actime = tset->actime;
  68.     }
  69.     else
  70.     {
  71.         time(&actime);
  72.         modtime = actime;
  73.     }
  74.  
  75.     (void)_unx2dos(_filename, filename);
  76.  
  77.     dtime = _dostime(actime);
  78.     settime.actime = (unsigned short) ((dtime >> 16) & 0xFFFF);
  79.     settime.acdate = (unsigned short) (dtime & 0xFFFF);
  80.     dtime = _dostime(modtime);
  81.     settime.modtime = (unsigned short) ((dtime >> 16) & 0xFFFF);
  82.     settime.moddate = (unsigned short) (dtime & 0xFFFF);
  83.     fh = (int) Fopen(filename, 2);
  84.     if (fh < 0) {
  85.         errno = -fh;
  86.         return -1;
  87.     }
  88.  
  89.     res = -EINVAL;
  90.     if (__mint > 90)
  91.         res = Fcntl(fh, (long)&settime, FUTIME);
  92.  
  93.     if (res == -EINVAL)
  94.         (void)Fdatime((_DOSTIME *) &dtime, fh, 1);
  95.  
  96.     if ((fh = Fclose(fh)) != 0) {
  97.         errno = -fh;
  98.         return -1;
  99.     }
  100.     return 0;
  101. }
  102.  
  103. int stime(t)
  104.     time_t *t;
  105. {
  106.     unsigned long dtime;
  107.     unsigned date, time;
  108.  
  109.     assert(t != 0);
  110.     dtime = _dostime(*t);
  111.     date = (int) (dtime & 0xffff);
  112.     time = (int) (dtime >> 16) & 0xffff;
  113.  
  114.     if (Tsetdate(date) || Tsettime(time)) {
  115.         errno = EBADARG;
  116.         return -1;
  117.     }
  118.     return 0;
  119. }
  120.