home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 154_01 / rm.c < prev    next >
Text File  |  1979-12-31  |  2KB  |  125 lines

  1. /* rm.c: remove files
  2.  
  3. -----
  4.     (c) Chuck Allison, 1985
  5. -----
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #define NLOOK 5
  11. #define MAXFILES 150
  12. #define MAXLINE 256
  13. #define ESC 27
  14. #define yes 1
  15. #define no 0
  16.  
  17. main(argc,argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.     int i,
  22.         k,
  23.         go = no,
  24.         delete = yes,
  25.         xargc;
  26.     char *xargv[MAXFILES];
  27.     static char prompt[] = "files to delete (^Z when done)\n";
  28.  
  29.     /* ..get optional "go" switch.. */
  30.     if (argv[1][0] == '-')
  31.         if (tolower(argv[1][1]) == 'g')
  32.         {
  33.             go = yes;
  34.             argv++;
  35.             argc--;
  36.         }
  37.         else
  38.         {
  39.             fprintf(stderr,"unknown switch: -%c\n",argv[1][1]);
  40.             exit(1);
  41.         }
  42.  
  43.     /* ..expand wildcards, if any.. (Mark Williams C only!) */
  44.     xargc = exargs(prompt,argc,argv,xargv,MAXFILES);
  45.  
  46.     for (i = 0; i < xargc; ++i)
  47.     {
  48.         if (!exist(xargv[i]))
  49.         {
  50.             printf("%s not found\n",xargv[i]);
  51.             continue;
  52.         }
  53.         delete = yes;
  54. again:
  55.         if (go == no)
  56.         {
  57.             printf("\nDelete %c[7m%s%c[0m? (y,n,p,g,q): ",
  58.               ESC,xargv[i],ESC);
  59.             fflush(stdout);
  60.             k = getkey();
  61.             switch (tolower(k))
  62.             {
  63.                 case 'g':
  64.                     go = yes;
  65.                 case 'y':
  66.                     delete = yes;
  67.                     break;
  68.                 case 'n':
  69.                     delete = no;
  70.                     break;
  71.                 case 'p':
  72.                     look(xargv[i]);
  73.                     goto again;
  74.                 case 'q':
  75.                     exit(0);
  76.                 default :
  77.                     goto again;
  78.             }
  79.         }
  80.         if (delete)
  81.         {
  82.             if (unlink(xargv[i]) == 0)
  83.                 fprintf(stdout,"%s deleted\n",xargv[i]);
  84.             else
  85.                 fprintf(stdout,"error deleting %s\n",xargv[i]);
  86.         }
  87.     }
  88. }
  89.  
  90. look(s)            /* ..review first NLOOK (5) lines of file.. */
  91. char *s;
  92. {
  93.     FILE *f;
  94.     int i;
  95.     char l[MAXLINE];
  96.  
  97.     putchar('\n');
  98.     if ((f = fopen(s,"r")) == NULL) 
  99.         printf("can't open %s\n",s);
  100.     else
  101.     {
  102.         printf("\n***** %s: *****\n",s);
  103.         for (i = 0; i < NLOOK && fgets(l,MAXLINE-1,f); ++i)
  104.             fputs(l,stdout);
  105.         fclose(f);
  106.         printf("********************\n\n");
  107.     }
  108. }
  109.  
  110. int exist(s)
  111. char *s;
  112. {
  113.     FILE *f;
  114.     int found;
  115.  
  116.     if ((f = fopen(s,"r")) == NULL)
  117.         found = no;
  118.     else
  119.     {
  120.         found = yes;
  121.         fclose(f);
  122.     }
  123.     return found;
  124. }
  125.