home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / kill2.zip / kill2src.zip / xtime.c < prev   
C/C++ Source or Header  |  1992-12-21  |  4KB  |  165 lines

  1. #define INCL_DOS
  2.  
  3. #include <os2.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <io.h>
  9. #include <fcntl.h>
  10. #include <share.h>
  11.  
  12.   #define SECS_PER_DAY 86400L
  13.  
  14.  
  15.     struct _ftime {     /* Structs & unions for file date/time */
  16.         unsigned int sec: 5;
  17.         unsigned int min: 6;
  18.         unsigned int hour:5;
  19.     };
  20.  
  21.     struct _fdate {
  22.         unsigned int day: 5;
  23.         unsigned int mon: 4;
  24.         unsigned int year:7;
  25.     };
  26.  
  27.     union _fd {
  28.         struct _fdate fd;
  29.         unsigned int x;
  30.     };
  31.  
  32.     union _ft {
  33.         struct _ftime ft;
  34.         unsigned int x;
  35.     };
  36.  
  37.     struct _file_buffer {   /* For DosFindFirst/DosFindNext */
  38.         union _fd cd;
  39.         union _ft ct;
  40.         union _fd ld;
  41.         union _ft lt;
  42.         union _fd wd;
  43.         union _ft wt;
  44.         long size;
  45.         long falloc;
  46.         unsigned int attr;
  47.         unsigned char namelen;
  48.         char filename[257];
  49.     };
  50.  
  51.  
  52.  
  53. time_t _fastcall dosfile_2_unix (struct _ftime *ft,struct _fdate *fd) {
  54.  
  55.     struct tm tm;
  56.  
  57.  
  58.     memset(&tm,0,sizeof(tm));
  59.     tm.tm_sec = ft->sec;
  60.     tm.tm_min = ft->min;
  61.     tm.tm_hour = ft->hour;
  62.     tm.tm_mday = fd->day;
  63.     tm.tm_mon = fd->mon - 1;
  64.     tm.tm_year = fd->year + 80;
  65.     return mktime(&tm);
  66. }
  67.  
  68.  
  69. /* note 1980 base */
  70.  
  71. time_t _fastcall unixgetftime (int handle) {
  72.  
  73.     struct tm tm;
  74.     struct {
  75.       struct _fdate  cfd;
  76.       struct _ftime  cft;
  77.       struct _fdate  afd;
  78.       struct _ftime  aft;
  79.       struct _fdate  wfd;
  80.       struct _ftime  wft;
  81.       long           size;
  82.       long           alc;
  83.       int            attrib;
  84.     } fdata;
  85.  
  86.     if(DosQFileInfo(handle,1,&fdata,sizeof(fdata))) return -1L;
  87.     memset(&tm,0,sizeof(tm));
  88.     tm.tm_sec  = fdata.wft.sec;
  89.     tm.tm_min  = fdata.wft.min;
  90.     tm.tm_hour = fdata.wft.hour;
  91.     tm.tm_mday = fdata.wfd.day;
  92.     tm.tm_mon  = fdata.wfd.mon - 1;
  93.     tm.tm_year = fdata.wfd.year + 80;
  94.     return mktime(&tm);
  95. }
  96.  
  97.  
  98.  
  99. int _fastcall unixsetftime (int handle,time_t t) {
  100.  
  101.   struct tm *tm;
  102.   struct {
  103.     struct _fdate  cfd;
  104.     struct _ftime  cft;
  105.     struct _fdate  afd;
  106.     struct _ftime  aft;
  107.     struct _fdate  wfd;
  108.     struct _ftime  wft;
  109.   } fdata;
  110.   char macmode;
  111.  
  112.   tm = localtime(&t);
  113.   if(tm) {
  114.     fdata.wft.sec =  tm->tm_sec;
  115.     fdata.wft.min =  tm->tm_min;
  116.     fdata.wft.hour = tm->tm_hour;
  117.     fdata.wfd.day =  tm->tm_mday;
  118.     fdata.wfd.mon =  tm->tm_mon + 1;
  119.     fdata.wfd.year = tm->tm_year - 80;
  120.     fdata.cfd = fdata.afd = fdata.wfd;
  121.     fdata.cft = fdata.aft = fdata.wft;
  122.     DosGetMachineMode(&macmode);
  123.     if(!macmode) {  /* Messydos alert -- gaaah!  run away! */
  124.       memset(&fdata.cfd,0,sizeof(fdata.cfd));
  125.       memset(&fdata.cft,0,sizeof(fdata.cft));
  126.       fdata.afd = fdata.cfd;
  127.       fdata.aft = fdata.cft;
  128.     }
  129.     return
  130.       DosSetFileInfo(handle,1,&fdata,sizeof(fdata));
  131.   }
  132.   return -1;
  133. }
  134.  
  135.  
  136.  
  137. int _fastcall oldenough (char *filename,ULONG days) {
  138.  
  139.   int    handle,ret = 0;
  140.   time_t filetime,curtime;
  141.   ULONG  daysdiff;
  142.  
  143.   if(!days) return 1; /* always match if 0 */
  144.  
  145.   handle = sopen(filename,O_RDONLY | O_BINARY,SH_DENYNO);
  146.   if(handle == -1)
  147.     return 0;  /* can't open */
  148.  
  149.   filetime = unixgetftime(handle);
  150.   if(filetime != -1L) {
  151.     curtime = time(NULL);
  152.     if(curtime != -1L) {
  153.       if(filetime < curtime) {
  154.         daysdiff = (curtime - filetime) / SECS_PER_DAY;
  155.         if(daysdiff >= days)
  156.           ret = 1;
  157.       }
  158.     }
  159.   }
  160.  
  161.   close(handle);
  162.  
  163.   return ret;
  164. }
  165.