home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Windows Tools / SOS-WIN_TOOLS.ISO / programm / shell / w_one41 / touch.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-30  |  5.6 KB  |  234 lines

  1. #include "touch.h"
  2.  
  3. /******************************************************************\
  4. *                                                                  *
  5. *           w       w                oooo                           *
  6. *           w       w  iii  n   n   o    o   n   n  eeee            *
  7. *           w       w   i   nn  n  o      o  nn  n  e               *
  8. *           w   w   w   i   n n n  o      o  n n n  eee             *
  9. *              w w w w    i   n  nn   o    o   n  nn  e               *
  10. *              w   w    iii  n   n    oooo    n   n  eeee            *
  11. *                                                                      *
  12. *     C o m m a n d   L a n g u a g e   I n t e r p r e t e r      *
  13. *                                                                      *
  14. *                                                                      *
  15. *    Written by Lucien Cinc                                         *
  16. *    Copyright (c) 1992, 1993                                       *
  17. *                                                                  *
  18. \******************************************************************/
  19.  
  20. DOS_FILE_DATE fdate;
  21. DOS_FILE_TIME ftime;
  22.  
  23. int count = 0;
  24.  
  25. void touch(char *p);
  26. int getuser(char *s, struct dosdate_t *date, struct dostime_t *time);
  27. void summary(void);
  28.  
  29. int main(void)
  30. {
  31.     int n, i;
  32.     struct dosdate_t date;
  33.     struct dostime_t time;
  34.     char *sp;
  35.  
  36.     sp = args();                // parse command line switches
  37.     while(*sp)
  38.         switch(*sp++) {
  39.             case 'v' :          // show version information
  40.                        printf("%cVersion %c%d.%01d\n", WHITE, YELLOW, VERSION / 10, VERSION % 10);
  41.                        return 0;
  42.             default:            // invalid switch
  43.                        perror("Invalid switch");
  44.                        return 1;
  45.         }
  46.  
  47.     if (argnstr() != 0) {           // no command line string's allowed
  48.         perror("Invalid argument");
  49.         return 1;
  50.     }
  51.  
  52.     _dos_getdate(&date);            // get system date and time
  53.     _dos_gettime(&time);
  54.  
  55.     switch(argc()) {               // parse command line arguments
  56.         case 1:
  57.                 break;
  58.         case 2:
  59.                 if (getuser(argv(2), &date, &time))
  60.                     return 1;        // invalid date or time
  61.                 break;
  62.         case 3:
  63.                 if (getuser(argv(2), &date, &time) ||
  64.                     getuser(argv(3), &date, &time))
  65.                         return 1;    // invalid date or time
  66.                 break;
  67.         default:
  68.                 perror("Too many or few arguments");
  69.                 return 2;
  70.     }
  71.  
  72.     fdate.b.Day = date.day;                // date to set
  73.     fdate.b.Month = date.month;
  74.     fdate.b.Year = date.year - 1980;
  75.  
  76.     ftime.b.Hour = time.hour;            // time to set
  77.     ftime.b.Minute = time.minute;
  78.     ftime.b.Second = time.second;
  79.  
  80.     if ((n = fillfile(argpath(1), ATT_RHSA)) > 0) {        // some files to touch
  81.  
  82.         limit(n);    // status bar upper limit
  83.  
  84.         for (i = 0;i < n;i++, inc(1)) {
  85.             if (isbreak())
  86.                 break;
  87.  
  88.             touch(getfilepath(i));        // file to be touched
  89.         }
  90.  
  91.         summary();
  92.  
  93.         empty();    // finished with status bar
  94.  
  95.     } else {                                            // a file to create and touch
  96.  
  97.         // no need to check for wildcards in argabs() because
  98.         // touch() will fail after trying to create the file
  99.  
  100.         touch(argabs(1));    
  101.         summary();
  102.  
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
  108. int stoi(char **str)
  109. {
  110.     char *tstr = (char *)*str;
  111.     int n = 0;
  112.  
  113.     while (isdigit(*tstr))
  114.           n = n * 10 + (*tstr++ - '0');
  115.  
  116.     *str = (char *)tstr;
  117.  
  118.     return n;
  119. }
  120.  
  121. int getdate(char *s, struct dosdate_t *date)
  122. {
  123.     int mon, day, year;
  124.     int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  125.     int okflag = FALSE;
  126.  
  127.     if (isdigit(*s)) {
  128.         day = stoi(&s);                    // parse day
  129.  
  130.         if (*s++ == '-' && isdigit(*s)) {
  131.             mon = stoi(&s);                // parse month
  132.  
  133.             if (*s++  == '-' && isdigit(*s)) {
  134.                 year = stoi(&s);        // parse year
  135.  
  136.                 if (year % 4 == 0 && year % 100 != 0 || year & 400 == 0)
  137.                     days[2] = 29;              // adjust for leap year
  138.  
  139.                 if (*s == '\0')
  140.                     okflag = TRUE;
  141.             }
  142.         }
  143.     }
  144.  
  145.     if (okflag &&
  146.        (mon > 0 && mon < 13) &&
  147.        (day > 0 && day <= days[mon]) &&
  148.        (year > 1979 && year < 2101)) {    // correctly parsed date
  149.  
  150.             date->day = day;
  151.             date->month = mon;
  152.             date->year = year;
  153.  
  154.     } else {
  155.         perror("Invalid date");
  156.         return 1;    // Invalid Date
  157.     }
  158.  
  159.     return 0;
  160. }
  161.  
  162. int gettime(char *s, struct dostime_t *time)
  163. {
  164.     int hour, min, sec = 0;
  165.     BOOL okflag = FALSE;
  166.     
  167.     if (isdigit(*s)) {
  168.         hour = stoi(&s);            // parse hour
  169.  
  170.         if (*s++ == ':' && isdigit(*s)) {
  171.               min = stoi(&s);            // parse minute
  172.  
  173.             if (*s == '\0')
  174.                 okflag = TRUE;        // done
  175.             else if (*s++ == ':' && isdigit(*s)) {
  176.                 sec = stoi (&s);     // parse optional seconds
  177.  
  178.                 if (*s == '\0')
  179.                     okflag = TRUE;    // done
  180.             }
  181.         }
  182.     }
  183.  
  184.     if (okflag &&
  185.        (hour >= 0 && hour < 24) &&
  186.        (min >= 0 && min < 60) &&
  187.        (sec >= 0 && sec < 60)) {    // correctly parsed time
  188.  
  189.             time->hour = hour;
  190.               time->minute = min;
  191.               time->second = sec;
  192.  
  193.     } else {
  194.         perror("Invalid time");
  195.         return 1;        // Invalid Time
  196.     }
  197.  
  198.     return 0;
  199. }
  200.  
  201. int getuser(char *s, struct dosdate_t *date, struct dostime_t *time)
  202. {
  203.     if (strchr(s, ':'))                // assume a time
  204.         return gettime(s, time);
  205.     else                            // assume a date
  206.         return getdate(s, date);
  207. }
  208.  
  209. void touch(char *p)
  210. {
  211.     int handle;
  212.  
  213.     // touch only works for files, since there is no function
  214.     // to set the date and time for a directory
  215.      
  216.     if (_dos_open(p, O_RDONLY, &handle) && _dos_creat(p, 0, &handle)) {
  217.         perror("Invalid path or file name");
  218.         return;
  219.     }
  220.  
  221.     _dos_setftime(handle, fdate.u, ftime.u);    // at last
  222.     _dos_close(handle);
  223.  
  224.     count++;                                    // increment counter
  225.  
  226.     printf("%c%s\n", GREEN, unixpath(padfilename(p)));
  227. }
  228.  
  229. void summary(void)
  230. {
  231.     if (count)
  232.         printf("\n%c%5d %cfile(s) touched\n", YELLOW, count, WHITE);
  233. }
  234.