home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv2_4 / quickc / touch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-02  |  3.2 KB  |  158 lines

  1. /*
  2. Microsoft Systems Journal
  3. Volume 2; Issue 4; September, 1987
  4.  
  5. Code Listings For:
  6.  
  7.     TOUCH
  8.     pp. 15-22
  9.  
  10. Author(s): Augie Hansen
  11. Title:     Programming in C the Fast and Easy Way with Microsoft QuickC
  12.  
  13.  
  14.  
  15.  
  16.  
  17. Figure 7
  18. ========
  19.  
  20. */
  21.  
  22. /*
  23.  * PROGRAM:  TOUCH
  24.  *
  25.  * DESCRIPTION:  Update the last modification time of a file or a
  26.  *    group of files to the current time.
  27.  *
  28.  * ENTRY:  A list of files to "touch".  The filenames can be preceded
  29.  *      by one or both of the following options:
  30.  *
  31.  *        -c    don't create any files
  32.  *              -v      operate in verbose mode (report activity)
  33.  *
  34.  * SYNTAX:
  35.  *      touch [-cv] filename ...
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include <io.h>
  43. #include <errno.h>
  44. #include <sys\types.h>
  45. #include <sys\stat.h>
  46. #include <sys\utime.h>
  47.  
  48. #define ERR     0x7FFF
  49. #define MAXNAME 8
  50.  
  51. typedef enum { FALSE, TRUE } BOOLEAN;
  52. extern void usage(char *);
  53.  
  54. int
  55. main(argc, argv)
  56. int argc;
  57. char *argv[];
  58. {
  59.     int ch;                 /* character buffer */
  60.     char *cp;               /* character pointer */
  61.     int badcount;           /* number of files that cannot */
  62.                 /* be successfully updated */
  63.     struct stat statbuf;    /* buffer for stat results */
  64.     BOOLEAN errflag;    /* error flag */
  65.     BOOLEAN cflag;          /* creation control flag */
  66.     BOOLEAN vflag;          /* verbose control flag */
  67.     FILE *fp;        /* file pointer */
  68.  
  69.     static char pgm[MAXNAME + 1] = { "touch" };
  70.  
  71.  
  72.     /*
  73.      * Initialize flags and variables
  74.      */
  75.     errflag = cflag = vflag = FALSE;
  76.     badcount = 0;
  77.  
  78.     /*
  79.      * Move past the command name argument and look for
  80.      * optional arguments (signaled by a leading dash)
  81.      */
  82.     ++argv;
  83.     --argc;
  84.     while (argc > 1) {
  85.         cp = *argv;
  86.         if (*cp != '-')
  87.             /* not an option flag */
  88.             break;
  89.  
  90.         /*
  91.          * Having seen an option flag ('-'), look at
  92.          * any option letters that follow it in the
  93.          * current argument string
  94.          */
  95.         for (++cp; *cp != '\0'; ++cp)
  96.             switch (*cp) {
  97.             case 'c':
  98.                 /* don't create files */
  99.                 cflag = TRUE;
  100.                 puts("CREATION flag set");
  101.                 break;
  102.             case 'v':
  103.                 /* verbose -- report activity */
  104.                 vflag = TRUE;
  105.                 break;
  106.             default:
  107.                 fprintf(stderr, "%s: unknown option %c\n",
  108.                     pgm, *cp);
  109.                 usage(pgm);
  110.                 exit(ERR);
  111.             }
  112.         ++argv;
  113.         --argc;
  114.     }
  115.  
  116.  
  117.     /*
  118.      * Update modification times of files
  119.      */
  120.     for (; argc-- > 0; ++argv) {
  121.         if (stat(*argv, &statbuf) == -1) {
  122.             /* file doesn't exist */
  123.             if (cflag == TRUE) {
  124.                 /* don't create it */
  125.                 ++badcount;
  126.                 continue;
  127.             }
  128.             else if ((fp = fopen(*argv, "w")) == NULL) {
  129.                 fprintf(stderr, "%s: Cannot create %s\n",
  130.                     pgm, *argv);
  131.                 ++badcount;
  132.                 continue;
  133.             }
  134.             else {
  135.                 if (fclose(fp) == EOF) {
  136.                     perror("Error closing file");
  137.                     exit(ERR);
  138.                 }
  139.                 if (stat(*argv, &statbuf) == -1) {
  140.                     fprintf(stderr, "%s: Can't stat %s\n",
  141.                         pgm, *argv);
  142.                     ++badcount;
  143.                     continue;
  144.                 }
  145.             }
  146.         }            
  147.         if (utime(*argv, NULL) == -1) {
  148.             ++badcount;
  149.             perror("Error updating date/time stamp");
  150.             continue;
  151.         }
  152.         if (vflag == TRUE)
  153.             fprintf(stderr, "Touched file %s\n", *argv);
  154.     }
  155.  
  156.     return (badcount);
  157. }
  158.