home *** CD-ROM | disk | FTP | other *** search
/ Between Heaven & Hell 2 / BetweenHeavenHell.cdr / 100 / 74 / rm.c < prev    next >
Text File  |  1986-03-29  |  3KB  |  135 lines

  1. /* rm.c: remove files
  2.  *
  3.  *   (c) Chuck Allison, 1985
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. #define NLOOK 22
  10. #define MAXLINE 256
  11. #define yes 1
  12. #define no 0
  13.  
  14. FILE *screen;
  15.  
  16. main(argc,argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     register i;
  21.     int go = no,
  22.         delete = yes;
  23.     unsigned k;
  24.  
  25.     screen = fopen("con","w");
  26.  
  27.     /* ..get optional "go" switch.. */
  28.     if (argv[1][0] == '-' || argv[1][0] == '/')
  29.     {
  30.         if (tolower(argv[1][1]) == 'g')
  31.             go = yes;
  32.         else
  33.         {
  34.             fprintf(screen,"---> ERROR: bad option: -%c\n",argv[1][1]);
  35.             fflush(screen);
  36.         }
  37.         argv++;
  38.         argc--;
  39.     }
  40.  
  41.     for (i = 1; i < argc; ++i)
  42.     {
  43.         if (!exist(argv[i]))
  44.         {
  45.             fprintf(screen,"---> %s not found\n",argv[i]);
  46.             fflush(screen);
  47.             continue;
  48.         }
  49.         delete = yes;
  50.  
  51. again:
  52.         if (go == no)
  53.         {
  54.             fprintf(screen,"Delete \033[7m%s\033[0m (y,n,p,g,q)? ",argv[i]);
  55.             fflush(screen);
  56.             k = getcnb();
  57.             switch (k)
  58.             {
  59.                 case 'g':
  60.                 case 'G':
  61.                     go = yes;
  62.                 case 'y':
  63.                 case 'Y':
  64.                     delete = yes;
  65.                     break;
  66.                 case 'n':
  67.                 case 'N':
  68.                     delete = no;
  69.                     putc('\n',screen);
  70.                     break;
  71.                 case 'p':
  72.                 case 'P':
  73.                     look(argv[i]);
  74.                     goto again;
  75.                 case 'q':
  76.                 case 'Q':
  77.                     exit(0);
  78.                 default :
  79.                     putc('\n',screen);
  80.                     goto again;
  81.             }
  82.         }
  83.         if (delete)
  84.             if (unlink(argv[i]) == 0)
  85.                 fprintf(screen,"%s deleted\n",argv[i]);
  86.             else
  87.                 fprintf(screen,"---> %s NOT deleted -\n",argv[i]);
  88.         fflush(screen);
  89.     }
  90. }
  91.  
  92. look(s)            /* ..review first page of file.. */
  93. char *s;
  94. {
  95.     FILE *f;
  96.     int i;
  97.     char l[MAXLINE];
  98.  
  99.     putc('\n',screen);
  100.     if ((f = fopen(s,"r")) == NULL) 
  101.     {
  102.         fprintf(screen,"---> ERROR: can't open %s\n",s);
  103.         fflush(screen);
  104.     }
  105.     else
  106.     {
  107.         fputs("\033[2J",screen);
  108.         for (i = 0; i < NLOOK && fgets(l,MAXLINE-1,f); ++i)
  109.             fputs(l,screen);
  110.         fclose(f);
  111.         fputs("\n\033[7m-- hit any key --\033[0m",screen);
  112.         fflush(screen);
  113.         getcnb();
  114.         fputs("\n\033[2J",screen);
  115.         fflush(screen);
  116.     }
  117. }
  118.  
  119. int exist(s)
  120. char *s;
  121. {
  122.     FILE *f;
  123.     int found;
  124.  
  125.     if ((f = fopen(s,"r")) == NULL)
  126.         found = no;
  127.     else
  128.     {
  129.         found = yes;
  130.         fclose(f);
  131.     }
  132.  
  133.     return found;
  134. }
  135.