home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / unix / arcunx11 / arc.sh1 / arcdos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-01  |  3.8 KB  |  131 lines

  1. /*
  2.  *    arcdos.c    1.1
  3.  *
  4.  *    Author: Thom Henderson
  5.  *    Original System V port: Mike Stump
  6.  *    Enhancements, Bug fixes, and cleanup: Chris Seaman
  7.  *    Date: Fri Mar 20 09:57:02 1987
  8.  *    Last Mod.    3/21/87
  9.  *
  10.  */
  11.  
  12. /*
  13.  * ARC - Archive utility - ARCDOS
  14.  * 
  15.  * Version 1.43, created on 11/09/85 at 22:24:44
  16.  * 
  17.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  18.  * 
  19.  *     Description:
  20.  *          This file contains certain DOS level routines that assist
  21.  *          in doing fancy things with an archive, primarily reading and
  22.  *          setting the date and time last modified.
  23.  * 
  24.  *          These are, by nature, system dependant functions.  But they are
  25.  *          also, by nature, very expendable.
  26.  */
  27.  
  28. #include "arc.h"
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <time.h>
  32.  
  33. INT getstamp(f,date,time)              /* get a file's date/time stamp */
  34. FILE *f;                               /* file to get stamp from */
  35. unsigned INT *date, *time;             /* storage for the stamp */
  36. {
  37.     struct stat buf;
  38.     struct tm *tmbuf;
  39.  
  40.     fstat(fileno(f),&buf);
  41.     tmbuf=localtime(&buf.st_mtime);
  42.     
  43.     *date = ((tmbuf->tm_year-80)<<9) + ((tmbuf->tm_mon+1)<<5) + tmbuf->tm_mday;
  44.     *time = (tmbuf->tm_hour<<11) + (tmbuf->tm_min<<5) + (tmbuf->tm_sec>>1);;
  45. }
  46.  
  47. struct utimbuf {
  48.     time_t    actime;
  49.     time_t    modtime;
  50. };
  51.  
  52. INT setstamp(file,date,time)           /* set a file's date/time stamp */
  53. char *file;                            /* file to set stamp on */
  54. unsigned INT date, time;               /* desired date, time */
  55. {
  56.     struct utimbuf times;
  57.     struct tm *tmbuf;
  58.     long m_time;
  59.     int yr, mo, dy, hh, mm, ss, leap, days = 0;
  60.  
  61.     /*
  62.      * These date conversions look a little wierd, so I'll explain.
  63.      * UNIX bases all file modification times on the number of seconds
  64.      * elapsed since Jan 1, 1970, 00:00:00 GMT.  Therefore, to maintain
  65.      * compatibility with MS-DOS archives, which date from Jan 1, 1980,
  66.      * with NO relation to GMT, the following conversions must be made:
  67.      *         the Year (yr) must be incremented by 10;
  68.      *        the Date (dy) must be decremented by 1;
  69.      *        and the whole mess must be adjusted by TWO factors:
  70.      *            relationship to GMT (ie.,Pacific Time adds 8 hrs.),
  71.      *            and whether or not it is Daylight Savings Time.
  72.      * Also, the usual conversions must take place to account for leap years,
  73.      * etc.
  74.      *                                     C. Seaman
  75.      */
  76.  
  77.     yr = (((date >> 9) & 0x7f) + 10);  /* dissect the date */
  78.     mo = ((date >> 5) & 0x0f);
  79.     dy = ((date & 0x1f) - 1);
  80.  
  81.     hh = ((time >> 11) & 0x1f);        /* dissect the time */
  82.     mm = ((time >> 5) & 0x3f);
  83.     ss = ((time & 0x1f) * 2);
  84.  
  85.     leap = ((yr+1970)/4);              /* Leap year base factor */
  86.  
  87.     /* How many days from 1970 to this year? */
  88.     days = (yr * 365) + (leap - 492);
  89.  
  90.     switch(mo)                   /* calculate expired days this year */
  91.     {
  92.     case 12:
  93.         days += 30;
  94.     case 11:
  95.         days += 31;
  96.     case 10:
  97.         days += 30;
  98.     case 9:
  99.         days += 31;
  100.     case 8:
  101.         days += 31;
  102.     case 7:
  103.         days += 30;
  104.     case 6:
  105.         days += 31;
  106.     case 5:
  107.         days += 30;
  108.     case 4:
  109.         days += 31;
  110.     case 3:
  111.         days += 28;                    /* account for leap years */
  112.         if ((leap * 4) == (yr+1970) && (yr+1970) != 2000)
  113.             ++days;
  114.     case 2:
  115.         days += 31;
  116.     }
  117.  
  118.     /* convert date & time to seconds relative to 00:00:00, 01/01/1970 */
  119.     m_time = ((days + dy) * 86400) + (hh * 3600) + (mm * 60) + ss;
  120.  
  121.     tmbuf = localtime(&m_time);        /* check for Daylight Savings Time */
  122.     if (tmbuf->tm_isdst != 0)
  123.         m_time -= 3600;
  124.  
  125.     m_time += timezone;                /* account for timezone differences */
  126.  
  127.     times.actime = m_time;             /* set the stamp on the file */
  128.     times.modtime = m_time;
  129.     utime(file,×);
  130. }
  131.