home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / dirutl / rm104.ark / RM.C next >
Encoding:
C/C++ Source or Header  |  1987-02-16  |  5.3 KB  |  208 lines

  1. /*
  2.  * R M . C
  3.  *
  4.  * File deletion utility
  5.  *
  6.  * This routine is a file deletion program for the CP/M-80 environment
  7.  * that operates more or less in the style of UNIX (tm of AT&T) "rm". 
  8.  *
  9.  *  Usage: rm [-f] [-i] [-q] [-] [s:]filename [filename...]
  10.  *
  11.  *            s: => Expand afn only with SYStem attribute files
  12.  *        -f => Delete files, even if read-only
  13.  *        -i => Interactive query before deleting each file
  14.  *        -q => Quiet mode
  15.  *        -  => Designates that filenames follow
  16.  *
  17.  * The distributed compiled version of this program used the M. Kersenbrock
  18.  * version of croot.c where the filenames are Unix-like regular-expression
  19.  * filenames.  The 's:' handling is done by that croot.c .  Afn expansion
  20.  * normally expands ONLY non-SYS attribute files ("DIR" or "normal" files).
  21.  * 's:' forces expansion to ONLY SYS attribute files.
  22.  *
  23.  * The handling of "SYStem attribute" and "R/O" are those defined under
  24.  * CP/M 3.0, although this program should be compatible with CP/M 2.2 .
  25.  *
  26.  * The compiled binary, when run under CP/M 3.0, will set the system
  27.  * error-status upon error to that value handled by the CCP105 
  28.  * CCP replacement for reasons explained there (if you use CP/M 3.0
  29.  * and haven't replaced your CCP with CCP105, then you're working
  30.  * too hard!).  
  31.  *
  32.  * (C) Copyright 1987 by Michael D. Kersenbrock, Aloha, Oregon.  All rights
  33.  * reserved.
  34.  *
  35.  * This program may be freely distributed for non-commercial purposes 
  36.  * so long as this notice is retained in its entirety.
  37.  */
  38.  
  39.  
  40. #include "c:stdio.h"         /* I keep'em in the RAMDISK           */
  41.  
  42. #ifndef POSTERROR
  43. #define POSTERROR 0xff12    /* Error that the colon processor uses    */
  44. #endif
  45.  
  46. #define TRUE 1
  47. #define FALSE 0
  48. #define VERSION "1.04"
  49. #define RO_FLAG (0x80 & Fcb[9]) /* CP/M 3.0 R/O flag              */
  50.  
  51. char Reply_buf[] = "\006        ";    /* Buffer for 'y/n' queries      */
  52. char Forceflag = FALSE;            /* remove r/o files when TRUE     */
  53. char Interactive = FALSE;        /* query for each file, when TRUE */
  54. char Quiet = FALSE;            /* no "xx deleted" msgs when TRUE */
  55. char Fcb[36];
  56. char getreply();
  57.  
  58. main(argc, argv)
  59. int argc;
  60. char *argv[];
  61. {
  62.     register int loop;
  63.     char file,c;
  64.  
  65.     for (loop = 1, file = FALSE ; loop < argc && file == FALSE ; loop++) {
  66.         if (*argv[loop] == '-') {
  67.             switch(tolower(*(argv[loop]+1))) {
  68.                 /*
  69.                  * Plain dash means "all following are
  70.                  * filenames"
  71.                  */
  72.               case '\0':    file = TRUE;
  73.                       break;
  74.  
  75.                 /*
  76.                  * '-f' means to override anything Read/Only
  77.                  */
  78.               case  'f':
  79.                     Forceflag = TRUE;
  80.                     break;
  81.  
  82.                 /*
  83.                  * '-i' means to delete files interactively
  84.                  */
  85.               case 'i':
  86.                     Interactive = TRUE;
  87.                         break;
  88.  
  89.                 /*
  90.                  * '-q' means not to be verbose
  91.                  */
  92.               case 'q':
  93.                     Quiet = TRUE;
  94.                     break;
  95.  
  96.                default:
  97.                     fputs("Unknown option: ",stderr);
  98.                     fputs(argv[loop],stderr);
  99.                     usage();
  100.                     exit(POSTERROR);
  101.             }
  102.         }
  103.         else {
  104.             loop--;
  105.             file = TRUE;
  106.         }
  107.     }
  108.  
  109.     if (loop >= argc || argc <= 1) {
  110.         fputs("\nFilename\(s\) are missing\n",stderr);
  111.         usage(0);
  112.         exit(POSTERROR);
  113.     }
  114.  
  115.     /*
  116.      * At this point, argv[loop] should be the first file to operate on.
  117.      */
  118.     for ( ; loop < argc ; loop++) {
  119.         /*
  120.          * Try to open the file
  121.          */
  122.         fcbinit(argv[loop],Fcb);
  123.  
  124.         /*
  125.          * Note difference between CP/M 2.2 and 3.0:
  126.          * 2.2 => Returns 0<->3 with successful open, 0xff with error
  127.          * 3.0 => Returns ONLY 0  with successful open, 0xff with error
  128.          *
  129.          * Lesson: don't look for the zero "good return".  :-)
  130.          */
  131.         if (bdos(0x0f,Fcb) == 0xff) {
  132.             fputs("File: ",stderr);
  133.             fputs(argv[loop],stderr);
  134.             fputs(" not found\n",stderr);
  135.             continue;
  136.         }
  137.  
  138.         /*
  139.          * Poll to see if a CTL-C (abort) has been posted
  140.          */
  141.         chekkbd();
  142.  
  143.         /*
  144.          * Be neat, and close file if it opened OK
  145.          */
  146.         bdos(0x10,Fcb);
  147.         
  148.         if (RO_FLAG != 0 && Forceflag == FALSE) {
  149.             fputs("File: ",stderr);
  150.             fputs(argv[loop],stderr);
  151.             fputs(" is R/O \n",stderr);
  152.             continue;
  153.  
  154.         }
  155.         else if (Interactive == TRUE) {
  156.             fputs("File: ",stdout);
  157.             fputs(argv[loop],stdout);
  158.             fputs(" , delete \(y/n\)? ",stdout);
  159.             if (tolower(getreply()) != 'y') {
  160.                 putchar('\n');                
  161.                 continue;
  162.             }
  163.             fputs("\t\t\t\t\t\r",stdout);
  164.         }
  165.         if (RO_FLAG != 0 && Forceflag == TRUE) {
  166.             /*
  167.              * Make file R/W 
  168.              */
  169.             Fcb[9] &= 0x7f;
  170.             bdos(0x1e,Fcb);
  171.         }
  172.         unlink(argv[loop]);
  173.         if (Quiet != TRUE) {
  174.             fputs("File: ",stdout);
  175.             fputs(argv[loop],stdout);
  176.             fputs(" deleted\n",stdout);
  177.         }
  178.     }
  179.     exit(0);
  180. }
  181.  
  182. usage() {
  183.     fputs("\nReMove file utility\t Version: ",stderr);
  184.     fputs(VERSION,stderr);
  185.     fputs("\t\t(c) 1987 M. Kersenbrock",stderr);
  186.     fputs("\n\nUsage: rm [-f] [-i] [-q] [-] [s:]filename [filename...]",
  187.                                       stderr);
  188.     fputs("\n\t\ts: => Expand afn only with SYStem attribute files",
  189.                                       stderr);
  190.     fputs("\n\t\t-f => Delete files, even if read-only",stderr);
  191.     fputs("\n\t\t-i => Query before deleting each file",stderr);
  192.     fputs("\n\t\t-q => \"Quiet\" mode",stderr);
  193.     fputs("\n\t\t-  => Designates that filenames follow\n",stderr);
  194. }
  195.  
  196.  
  197. char
  198. getreply() {
  199.     bdos(0x0a,Reply_buf);    /* Get a line of edited input from console */
  200.     if (Reply_buf[1] >= 1)
  201.         return(Reply_buf[2]);
  202.     return(0);
  203. }
  204. out);
  205.         }
  206.         if (RO_FLAG != 0 && Forceflag == TRUE) {
  207.             /*
  208.              *