home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 08file / touch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.7 KB  |  133 lines

  1. /*
  2.  *    touch -- update modification time of file(s)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <sys\types.h>
  9. #include <sys\stat.h>
  10. #include <sys\utime.h>
  11. #include <io.h>
  12. #include <errno.h>
  13. #include <local\std.h>
  14.  
  15. /* error return -- big enough not to be mistaken for a bad file count */
  16. #define ERR    0x7FFF
  17.  
  18. main(argc, argv)
  19. int argc;
  20. char *argv[];
  21. {
  22.     int ch;
  23.     int i;
  24.     int badcount;        /* # of files that can't be updated */
  25.     struct stat statbuf;    /* buffer for stat results */
  26.     BOOLEAN errflag,    /* error flag */
  27.         cflag,        /* creation flag */
  28.         vflag;        /* verbose flag */
  29.     FILE *fp;
  30.  
  31.     static char pgm[MAXNAME + 1] = { "touch" };
  32.     extern int getopt(int, char **, char *);
  33.     extern int optind, opterr;
  34.     extern char *optarg;
  35.     extern void getpname(char *, char *);
  36.     static void usage(char *);
  37.  
  38.     /* get program name from DOS (version 3.00 and later) */
  39.     if (_osmajor >= 3)
  40.         getpname(argv[0], pgm);
  41.  
  42.     /* process optional arguments first */
  43.     errflag = cflag = vflag = FALSE;
  44.     badcount = 0;
  45.     while ((ch = getopt(argc, argv, "cv")) != EOF)
  46.         switch (ch) {
  47.         case 'c':
  48.             /* don't create files */
  49.             cflag = TRUE;
  50.             break;
  51.         case 'v':
  52.             /* verbose -- report activity */
  53.             vflag = TRUE;
  54.             break;
  55.         case '?':
  56.             errflag = TRUE;
  57.             break;
  58.         }
  59.     argc -= optind;
  60.     argv += optind;
  61.  
  62.     /* check for errors including no file names */
  63.     if (errflag == TRUE || argc <= 0) {
  64.         usage(pgm);
  65.         exit(ERR);
  66.     }
  67.  
  68.     /* update modification times of files */
  69.     for (; argc-- > 0; ++argv) {
  70.         if (stat(*argv, &statbuf) == -1) {
  71.             /* file doesn't exist */
  72.             if (cflag == TRUE) {
  73.                 /* don't create it */
  74.                 ++badcount;
  75.                 continue;
  76.             }
  77.             else if ((fp = fopen(*argv, "w")) == NULL) {
  78.                 fprintf(stderr, "%s: Cannot create %s\n",
  79.                     pgm, *argv);
  80.                 ++badcount;
  81.                 continue;
  82.             }
  83.             else {
  84.                 if (fclose(fp) == EOF) {
  85.                     perror("Error closing file");
  86.                     exit(ERR);
  87.                 }
  88.                 if (stat(*argv, &statbuf) == -1) {
  89.                     fprintf(stderr, "%s: Cannot stat %s\n", pgm, *argv);
  90.                     ++badcount;
  91.                     continue;
  92.                 }
  93.             }
  94.         }            
  95.         if (utime(*argv, NULL) == -1) {
  96.             ++badcount;
  97.             perror("Error updating date/time stamp");
  98.             continue;
  99.         }
  100.         if (vflag == TRUE)
  101.             fprintf(stderr, "Touched file %s\n", *argv);
  102.     }
  103.  
  104.     exit(badcount);
  105. } /* end main() */
  106.  
  107. /*
  108.  *    usage -- display an informative usage message
  109.  */
  110.  
  111. static void
  112. usage(pname)
  113. char *pname;
  114. {
  115.     fprintf(stderr, "Usage: %s [-cv] file ...\n", pname);
  116.     fprintf(stderr, "\t-c  Do not create any files\n");
  117.     fprintf(stderr, "\t-v  Verbose mode -- report activities\n");
  118. } /* end usage() */
  119.  
  120. /*
  121.  *    dummy functions to show how to save a little space
  122.  */
  123.  
  124. _setenvp()
  125. {
  126. }
  127.  
  128. #ifndef DEBUG
  129. _nullcheck()
  130. {
  131. }
  132. #endif
  133.