home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol02 / 04 / quickc.all < prev   
Text File  |  1987-10-30  |  4KB  |  229 lines

  1. Microsoft Systems Journal
  2. Volume 2; Issue 4; September, 1987
  3.  
  4. Code Listings For:
  5.  
  6.     TOUCH
  7.     pp. 15-22
  8.  
  9. Author(s): Augie Hansen
  10. Title:     Programming in C the Fast and Easy Way with Microsoft QuickC
  11.  
  12.  
  13.  
  14.  
  15.  
  16. Figure 7
  17. ========
  18.  
  19.  
  20. /*
  21.  * PROGRAM:  TOUCH
  22.  *
  23.  * DESCRIPTION:  Update the last modification time of a file or a
  24.  *    group of files to the current time.
  25.  *
  26.  * ENTRY:  A list of files to "touch".  The filenames can be preceded
  27.  *      by one or both of the following options:
  28.  *
  29.  *        -c    don't create any files
  30.  *              -v      operate in verbose mode (report activity)
  31.  *
  32.  * SYNTAX:
  33.  *      touch [-cv] filename ...
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #include <io.h>
  41. #include <errno.h>
  42. #include <sys\types.h>
  43. #include <sys\stat.h>
  44. #include <sys\utime.h>
  45.  
  46. #define ERR     0x7FFF
  47. #define MAXNAME 8
  48.  
  49. typedef enum { FALSE, TRUE } BOOLEAN;
  50. extern void usage(char *);
  51.  
  52. int
  53. main(argc, argv)
  54. int argc;
  55. char *argv[];
  56. {
  57.     int ch;                 /* character buffer */
  58.     char *cp;               /* character pointer */
  59.     int badcount;           /* number of files that cannot */
  60.                 /* be successfully updated */
  61.     struct stat statbuf;    /* buffer for stat results */
  62.     BOOLEAN errflag;    /* error flag */
  63.     BOOLEAN cflag;          /* creation control flag */
  64.     BOOLEAN vflag;          /* verbose control flag */
  65.     FILE *fp;        /* file pointer */
  66.  
  67.     static char pgm[MAXNAME + 1] = { "touch" };
  68.  
  69.  
  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.  
  119.     /*
  120.      * Update modification times of files
  121.      */
  122.     for (; argc-- > 0; ++argv) {
  123.         if (stat(*argv, &statbuf) == -1) {
  124.             /* file doesn't exist */
  125.             if (cflag == TRUE) {
  126.                 /* don't create it */
  127.                 ++badcount;
  128.                 continue;
  129.             }
  130.             else if ((fp = fopen(*argv, "w")) == NULL) {
  131.                 fprintf(stderr, "%s: Cannot create %s\n",
  132.                     pgm, *argv);
  133.                 ++badcount;
  134.                 continue;
  135.             }
  136.             else {
  137.                 if (fclose(fp) == EOF) {
  138.                     perror("Error closing file");
  139.                     exit(ERR);
  140.                 }
  141.                 if (stat(*argv, &statbuf) == -1) {
  142.                     fprintf(stderr, "%s: Can't stat %s\n",
  143.                         pgm, *argv);
  144.                     ++badcount;
  145.                     continue;
  146.                 }
  147.             }
  148.         }            
  149.         if (utime(*argv, NULL) == -1) {
  150.             ++badcount;
  151.             perror("Error updating date/time stamp");
  152.             continue;
  153.         }
  154.         if (vflag == TRUE)
  155.             fprintf(stderr, "Touched file %s\n", *argv);
  156.     }
  157.  
  158.     return (badcount);
  159. }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. Figure 8
  169. ========
  170.  
  171.  
  172.  
  173. /*
  174.  * usage()
  175.  *
  176.  * DESCRIPTION: Display an informative usage message using the
  177.  *  actual program name, which may have been changed by the user.
  178.  *
  179.  * ENTRY: A pointer to the program name.
  180.  */
  181.  
  182. #include <stdio.h>
  183.  
  184. void
  185. usage(pname)
  186. char *pname;
  187. {
  188.     fprintf(stderr, "Usage: %s [-c] [-v] file ...\n", pname);
  189.     fprintf(stderr, "\t-c  Do not create any files\n");
  190.     fprintf(stderr, "\t-v  Verbose mode -- report activities\n");
  191. }
  192.  
  193.  
  194.  
  195.  
  196. Figure 9
  197. ========
  198.  
  199.  
  200. #
  201. # Program: Touch
  202. #
  203.  
  204. .c.obj:
  205.     qcl -c  -Iusage.c -W0 -Ze -Zid -AM $*.c
  206.  
  207. usage.obj : usage.c
  208.  
  209. touch.obj : touch.c
  210.  
  211. Touch.exe : usage.obj touch.obj 
  212.     del Touch.lnk
  213.     echo usage.obj+ >>Touch.lnk
  214.     echo touch.obj  >>Touch.lnk
  215.     echo Touch.exe >>Touch.lnk
  216.     echo Touch.map >>Touch.lnk
  217.     link @Touch.lnk;
  218.  
  219.  
  220. Link Information - Touch.lnk
  221. ============================
  222. touch.obj+
  223. usage.obj
  224. Touch.exe
  225. Touch.map
  226.  
  227.  
  228.  
  229.