home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n02.zip / TOUCHW.C < prev    next >
C/C++ Source or Header  |  1988-07-13  |  4KB  |  154 lines

  1.  
  2. /*
  3.   For MSC 5.0/QuickC: cl touch.c setargv /link /NOE
  4.  */
  5. #include<stdio.h>
  6. #include<ctype.h>
  7. #include<stdlib.h>
  8. #include<string.h>
  9. #include<time.h>
  10. #include<errno.h>
  11. #include<fcntl.h>
  12. #include<sys\utime.h>
  13. #include<sys\types.h>
  14. #include<sys\stat.h>
  15.  
  16. #ifndef ERROR
  17. #define ERROR -1
  18. #endif
  19.  
  20. main(argc,argv)
  21. int argc;
  22. char **argv;
  23. {
  24.   int i, hdl;
  25.   char *date = NULL,*time = NULL;
  26.   struct utimbuf datetimes, *tptr;
  27.   extern int errno;
  28.  
  29.   if(argc < 2)    /* if no filenames passed, print usage message */
  30.   {
  31.     printf("\nUsage: touch [/Dmm/dd/yy /Thh:mm:ss] <filename> [filename...]");
  32.     exit(1);      /* set errorlevel to 1 */
  33.   }
  34.  
  35. /* process any switches, only allowed in argv[1] and argv[2]*/
  36.  
  37.   for( i = 1; i < 3; i++)
  38.     if(argv[i][0] == '/')             /* slash found? */
  39.       if(toupper(argv[i][1]) == 'D')      /* if Date string */
  40.         date = &argv[i][2];
  41.       else if(toupper(argv[i][1]) == 'T')     /* if Time string */
  42.         time = &argv[i][2];
  43.  
  44. /* if date or time switch set, set ptr to times structure */
  45.  
  46.   if(date || time)
  47.   {
  48.     convert_date_time(&datetimes,date,time);
  49.     tptr = &datetimes;
  50.   }
  51.   else
  52.     tptr = (struct utimbuf *)NULL;    /* else to NULL */
  53.  
  54.     /* increment argv and decrement argc
  55.        if either date or time switches are on */
  56.   if(date)
  57.   {
  58.     argv++;
  59.     argc--;
  60.   }
  61.   if(time)
  62.   {
  63.     argv++;
  64.     argc--;
  65.   }
  66.                    /* process each filename passed */
  67.   for( i = 1; i < argc; )
  68.   {
  69.     if(utime(argv[i],tptr) == ERROR)   /* unable to update the time */
  70.     {
  71.     switch(errno)      /* print appropriate error message */
  72.     {
  73.       case EACCES:
  74.         printf("\ntouch: Directory or Read-only file: %s",argv[i]);
  75.         break;
  76.       case EINVAL:
  77.         printf("\ntouch: Bad time argument changing %s",argv[i]);
  78.         exit(1);
  79.       case EMFILE:
  80.         printf("\ntouch: Too many open files (check FILES= in CONFIG.SYS)");
  81.         exit(1);
  82.       case ENOENT:
  83.                  /* if file doesn't exist, create it  */
  84.           if((hdl = open(argv[i], (O_WRONLY | O_CREAT),S_IWRITE)) == -1)
  85.             printf("\ntouch: Unable to create %s",argv[i]);
  86.           else
  87.           {
  88.             close(hdl);  
  89.             continue;
  90.           }
  91.           break;
  92.       }
  93.     }
  94.     i++;
  95.   }
  96.   exit(0);               /* set errorlevel to 0   */
  97. }
  98.  
  99. /*
  100.  * This function places date and time values in the datestr and timestr
  101.  * pointers into a tm structure, overriding the current time.
  102.  */
  103. convert_date_time(struct utimbuf *datetimes, char *datestr, char *timestr)
  104. {
  105.   struct tm *current_time;
  106.  
  107.   time((time_t *)&datetimes->modtime);      /* get current time  */
  108.   current_time = localtime(&datetimes->modtime);  /* get pointer to it */
  109.  
  110.   if(datestr)            /* if date passed, put it into place */
  111.   {
  112.     current_time->tm_mon = (get_date_time(&datestr) - 1);
  113.     current_time->tm_mday = get_date_time(&datestr);
  114.     if((current_time->tm_year = get_date_time(&datestr)) > 1900)
  115.       current_time->tm_year -= 1900;
  116.   }
  117.  
  118.   if(timestr)            /* if time passed, put into place */
  119.   {
  120.     current_time->tm_hour = get_date_time(×tr);
  121.     current_time->tm_min = get_date_time(×tr);
  122.     current_time->tm_sec = get_date_time(×tr);
  123.   }
  124.  
  125.   datetimes->modtime = mktime(current_time);    /* place in time_t */
  126. }
  127.  
  128. /*
  129.  * This function takes a pointer to a date or time pointer, gets the
  130.  * next piece of the date or time string and returns it as an integer
  131.  * value.  It advances the date or time pointer to point to the next
  132.  * piece available.
  133.  */
  134. int  get_date_time(char **ptr)
  135. {
  136.   char phrase[15];
  137.   char *p;
  138.  
  139.   strcpy(phrase,*ptr);         /* copy the current string  */
  140.  
  141.   if(p = strchr(phrase,'/'))     /* find next separator and set to NULL */
  142.     *p = NULL;
  143.   else if(p = strchr(phrase,':'))
  144.     *p = NULL;
  145.  
  146.   (*ptr) += strlen(phrase);      /* advance pointer to next component   */
  147.  
  148.   if(**ptr == '/' || **ptr == ':')   /* if a separator, move past it  */
  149.     (*ptr)++;
  150.  
  151.   return atoi(phrase);         /* return the current component  */
  152. }
  153.  
  154.