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

  1. /*
  2.  *    rm -- remove file(s)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys\types.h>
  8. #include <sys\stat.h>
  9. #include <ctype.h>
  10. #include <io.h>
  11. #include <local\std.h>
  12.  
  13. main(argc, argv)
  14. int argc;
  15. char *argv[];
  16. {
  17.     int ch;
  18.     BOOLEAN errflag,
  19.         iflag;
  20.  
  21.     static char pgm[MAXNAME + 1] = { "rm" };
  22.     extern void getpname(char *, char *);
  23.     static void do_rm(char *, char *, BOOLEAN);
  24.     extern int getopt(int, char **, char *);
  25.     extern int optind, opterr;
  26.     extern char *optarg;
  27.     
  28.     /* get program name from DOS (version 3.00 and later) */
  29.     if (_osmajor >= 3)
  30.         getpname(*argv, pgm);
  31.  
  32.     /* process optional arguments first */
  33.     errflag = iflag = FALSE;
  34.     while ((ch = getopt(argc, argv, "i")) != EOF)
  35.         switch (ch) {
  36.         case 'i':
  37.             /* interactive -- requires confirmation */
  38.             iflag = TRUE;
  39.             break;
  40.         case '?':
  41.             /* say what? */
  42.             errflag = TRUE;
  43.             break;
  44.         }
  45.     argc -= optind;
  46.     argv += optind;
  47.  
  48.     if (argc <= 0 || errflag == TRUE) {
  49.         fprintf(stderr, "%s [-i] file(s)\n", pgm);
  50.         exit(1);
  51.     }
  52.  
  53.     /* process remaining arguments */
  54.     for (; argc-- > 0; ++argv)
  55.         do_rm(pgm, *argv, iflag);
  56.  
  57.     exit(0);
  58. } /* end main() */
  59.  
  60. /*
  61.  *    do_rm -- remove a file
  62.  */
  63.  
  64. static void
  65. do_rm(pname, fname, iflag)
  66. char *pname, *fname;
  67. BOOLEAN iflag;
  68. {
  69.     int result = 0;
  70.     struct stat statbuf;
  71.     static BOOLEAN affirm();
  72.  
  73.     if (iflag == TRUE) {
  74.         fprintf(stderr, "%s (y/n): ", fname);
  75.         if (affirm() == FALSE)
  76.             return;
  77.     }
  78.     if ((result = unlink(fname)) == -1) {
  79.         fprintf(stderr, "%s: ", pname);
  80.         perror(fname);
  81.     }
  82.     return;
  83. }
  84.  
  85. /*
  86.  *    affirm -- return TRUE if the first character of the
  87.  *    user's response is 'y' or FALSE otherwise
  88.  */
  89.  
  90. #define MAXSTR    64
  91.  
  92. static BOOLEAN
  93. affirm()
  94. {
  95.     char line[MAXSTR + 1];
  96.     char *response;
  97.  
  98.     response = fgets(line, MAXSTR, stdin); 
  99.     return (tolower(*response) == 'y' ? TRUE : FALSE);
  100. }
  101.