home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / libsrc / c / sys / utime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  1.7 KB  |  64 lines

  1. /*
  2.   (c) Copyright 1992 Eric Backus
  3.  
  4.   This software may be used freely so long as this copyright notice is
  5.   left intact.  There is no warrantee on this software.
  6. */
  7.  
  8. #include <time.h>        /* For localtime() */
  9. #include <fcntl.h>        /* For open() */
  10. #include <dos.h>        /* For intdos() */
  11. #include <errno.h>        /* For errno */
  12. #include <utime.h>        /* For utime() */
  13.  
  14. /* An implementation of utime() for DJGPP.  The utime() function
  15.    specifies an access time and a modification time.  DOS has only one
  16.    time, so we will (arbitrarily) use the modification time. */
  17. int
  18. utime(const char *path, const struct utimbuf *times)
  19. {
  20.     union REGS        regs;
  21.     struct tm        *tm;
  22.     time_t        modtime;
  23.     int            fildes;
  24.     unsigned int    dostime, dosdate;
  25.  
  26.     /* DOS wants the file open */
  27.     fildes = open(path, O_RDONLY);
  28.     if (fildes == -1) return -1;
  29.  
  30.     /* NULL times means use current time */
  31.     if (times == NULL)
  32.     modtime = time((time_t *) 0);
  33.     else
  34.     modtime = times->modtime;
  35.  
  36.     /* Convert UNIX time to DOS date and time */
  37.     tm = localtime(&modtime);
  38.     dosdate = tm->tm_mday + ((tm->tm_mon + 1) << 5) +
  39.     ((tm->tm_year - 80) << 9);
  40.     dostime = tm->tm_sec / 2 + (tm->tm_min << 5) +
  41.     (tm->tm_hour << 11);
  42.  
  43.     /* Set the file timestamp */
  44.     regs.h.ah = 0x57;        /* DOS FileTimes call */
  45.     regs.h.al = 0x01;        /* Set date/time request */
  46.     regs.x.bx = fildes;        /* File handle */
  47.     regs.x.cx = dostime;    /* New time */
  48.     regs.x.dx = dosdate;    /* New date */
  49.     (void) intdos(®s, ®s);
  50.  
  51.     /* Close the file */
  52.     (void) close(fildes);
  53.  
  54.     /* Return */
  55.     if (regs.x.cflag != 0)
  56.     {
  57.     /* Not sure this is even possible. */
  58.     errno = regs.x.ax;
  59.     return -1;
  60.     }
  61.     else
  62.     return 0;
  63. }
  64.