home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / utime / utime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-26  |  2.2 KB  |  85 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <utime.h>        /* For utime() */
  4. #include <time.h>        /* For localtime() */
  5. #include <fcntl.h>        /* For open() */
  6. #include <unistd.h>
  7. #include <errno.h>        /* For errno */
  8. #include <go32.h>
  9. #include <dpmi.h>
  10.  
  11. /* An implementation of utime() for DJGPP.  The utime() function
  12.    specifies an access time and a modification time.  DOS has only one
  13.    time, so we will (arbitrarily) use the modification time.
  14.  
  15.    IF LFN is supported, then both times are used.  */
  16. int
  17. utime(const char *path, const struct utimbuf *times)
  18. {
  19.   __dpmi_regs r;
  20.   struct tm *tm;
  21.   time_t modtime;
  22.   int fildes;
  23.   unsigned int dostime, dosdate;
  24.   int retval = 0, e = 0;
  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.   r.h.ah = 0x57; /* DOS FileTimes call */
  45.   r.h.al = 0x01; /* Set date/time request */
  46.   r.x.bx = fildes; /* File handle */
  47.   r.x.cx = dostime; /* New time */
  48.   r.x.dx = dosdate; /* New date */
  49.   __dpmi_int(0x21, &r);
  50.   if (r.x.flags & 1)
  51.   {
  52.     e = EIO;
  53.     retval = -1;
  54.   }
  55.   else if (_USE_LFN)
  56.   {
  57.     /* We can set access time as well.  */
  58.     if (times)
  59.       modtime = times->actime;
  60.     tm = localtime(&modtime);
  61.     dosdate = tm->tm_mday + ((tm->tm_mon + 1) << 5) +
  62.       ((tm->tm_year - 80) << 9);
  63.     dostime = tm->tm_sec / 2 + (tm->tm_min << 5) +
  64.       (tm->tm_hour << 11);
  65.  
  66.     r.x.ax = 0x5705;
  67.     r.x.bx = fildes;
  68.     r.x.cx = dostime;    /* this might be ignored */
  69.     r.x.dx = dosdate;
  70.     __dpmi_int(0x21, &r);
  71.     if (r.x.flags & 1)
  72.     {
  73.       e = EIO;
  74.       retval = -1;
  75.     }
  76.   }
  77.  
  78.   /* Close the file */
  79.   (void) close(fildes);
  80.   if (e)
  81.     errno = e;
  82.  
  83.   return retval;
  84. }
  85.