home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / utime.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  2KB  |  93 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 <assert.h>
  20. #ifdef __TURBOC__
  21. #include <sys\types.h>
  22. #else
  23. #include <sys/types.h>
  24. #endif
  25. #include "lib.h"
  26.  
  27. time_t dostime __PROTO((time_t t));
  28.  
  29. /* convert a Unix time into a DOS time. The longword returned contains
  30.    the time word first, then the date word */
  31.  
  32. time_t dostime(t)
  33.     time_t t;
  34. {
  35.         time_t time, date;
  36.     struct tm *ctm;
  37.  
  38.     if ((ctm = localtime(&t)) == NULL)
  39.         return 0;
  40.     time = (ctm->tm_hour << 11) | (ctm->tm_min << 5) | (ctm->tm_sec >> 1);
  41.     date = ((ctm->tm_year - 80) & 0x7f) << 9;
  42.     date |= ((ctm->tm_mon+1) << 5) | (ctm->tm_mday);
  43.     return (time << 16) | date;
  44. }
  45.  
  46. int
  47. utime(_filename, tset)
  48.       const char *_filename;
  49.       const struct utimbuf *tset;
  50. {
  51.     int fh;
  52.     time_t settime;
  53.     unsigned long dtime;    /* dos time equivalent */
  54.     char filename[PATH_MAX];
  55.  
  56.     if (tset)
  57.         settime = tset->modtime;
  58.     else
  59.         time(&settime);
  60.  
  61.     (void)_unx2dos(_filename, filename);
  62.     dtime = dostime(settime);    /* convert unix time to dos */
  63.     fh = (int) Fopen(filename, 2);
  64.     if (fh < 0) {
  65.         errno = -fh;
  66.         return -1;
  67.     }
  68.     (void)Fdatime((_DOSTIME *) &dtime, fh, 1);
  69.     if ((fh = Fclose(fh)) != 0) {
  70.         errno = -fh;
  71.         return -1;
  72.     }
  73.     return 0;
  74. }
  75.  
  76. int stime(t)
  77.     time_t *t;
  78. {
  79.     unsigned long dtime;
  80.     unsigned date, time;
  81.  
  82.     assert(t != 0);
  83.     dtime = dostime(*t);
  84.     date = (int) (dtime & 0xffff);
  85.     time = (int) (dtime >> 16) & 0xffff;
  86.  
  87.     if (Tsetdate(date) || Tsettime(time)) {
  88.         errno = EBADARG;
  89.         return -1;
  90.     }
  91.     return 0;
  92. }
  93.