home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / RM_OM / RM.C < prev    next >
C/C++ Source or Header  |  1991-10-16  |  4KB  |  155 lines

  1. /*
  2. **    RM - remove files
  3. */
  4.  
  5. #include    <stdio.h>
  6. #include    <errno.h>
  7. #include    "formatch.h"
  8. #include    "option.h"
  9. #include    "ascii.h"
  10. #include    "askreply.h"
  11.  
  12. char copyright[] =
  13. "(c) 1988,89,90,91 by Otto Makela, Jyvaskyla, Finland\r\n"
  14. "Distributed under the GNU General Licence -- see file COPYING for details\r\n"
  15. "Call JyBox +358 41 211 562, V.32bis/HST/V.42/V.42bis, 24h/day\r\n";
  16.  
  17. #define OPTMASK     (OPTIONA|OPTIONF|OPTIONI|OPTIONQ|OPTIONR)
  18.  
  19. #define ATTRIBUTE   (DOTHIDDEN)
  20. #define DIRECTORYS  (DIRECTORY|RECURSIVE)
  21. #define SAFEATTR    (READONLY|SYSTEM|HIDDEN)
  22.  
  23. int rm();
  24. OPTION optset=0L;
  25.  
  26. int readchar();
  27. char *progname,*getenv();
  28.  
  29. main(argc,argv)
  30. int argc;
  31. char *argv[];   {
  32.     register int loop;
  33.     int i,matches=0;
  34.     char *p;
  35.  
  36.     progname="rm";
  37.  
  38.     if((p=getenv("RMFLAGS")) && ((optset=option(p,OPTMASK,NULL))&OPTERR))
  39.         fprintf(stderr,
  40.         "%s: unknown option %s in environment variable RMFLAGS\n",
  41.         progname,p);
  42.  
  43.         /* If stdin is redirected, fake a stdin argument just in case */
  44.     if(!isatty(fileno(stdin))) argv[argc++]="-";
  45.  
  46.     initmatch();
  47.     for(loop=1; loop<argc; loop++)
  48.         if(argv[loop][0]==*optsepar && argv[loop][1])    {
  49.         if((optset^=option(++argv[loop],OPTMASK,NULL))&OPTERR)  {
  50.         fprintf(stderr,"%s: unknown option %s\n",
  51.             progname,argv[loop]);
  52.         goto usage;
  53.         }
  54.         } else if(argv[loop][0]=='-' && !argv[loop][1])    {
  55.         char filename[BUFSIZ];
  56.         while(scanf("%s",filename)==1)
  57.         if(!(i=formatch(filename, SAFEATTR |
  58.             ((optset&OPTIONR)?DIRECTORYS:0), rm)))    {
  59.             if(!(optset&OPTIONF))
  60.             fprintf(stderr,"%s: no match for %s\n",
  61.                 progname,filename);
  62.         } else if(i>0)
  63.             matches+=i;
  64.         else
  65.             goto exit;
  66.         } else    {
  67.             if(!(i=formatch(argv[loop], SAFEATTR |
  68.                 ((optset&OPTIONR)?DIRECTORYS:0), rm)))    {
  69.                 if(!(optset&OPTIONF))
  70.             fprintf(stderr,"%s: no match for %s\n",progname,argv[loop]);
  71.             } else if(i>0)
  72.                 matches+=i;
  73.             else
  74.                 goto exit;
  75.     }
  76.  
  77.     if(!matches)    {
  78. usage:    fprintf(stderr,"usage: %s [%s%safiqr] {filename}...\n",
  79.         progname,optsepar,optsepar);
  80.     return(1);
  81.     }
  82.  
  83. exit:
  84.     return(0);
  85. }
  86.  
  87.  
  88. /*
  89. **    rm does the actual removing
  90. */
  91. unsigned int notrmd=0;
  92.  
  93. rm(file_match)
  94. FILE_MATCH *file_match; {
  95.     unsigned register char attribute=file_match->attribute;
  96.  
  97.     /* We get all names, but react to SAFEATTR files only on -a option */
  98.     if( !(optset&OPTIONA) && (attribute&SAFEATTR))    {
  99.     if(attribute&DIRECTORY)
  100.         notrmd=0;
  101.     else
  102.         notrmd++;
  103.     return(0);
  104.     }
  105.  
  106.     if( optset&OPTIONI || (attribute&SAFEATTR && !(optset&OPTIONF)) ) {
  107.         if(optset&OPTIONI)
  108.             printf("%s %s\t? ",progname,file_match->filename);
  109.         else
  110.             printf("%s: override mode %02x for %s\t? ",progname,
  111.                 attribute,file_match->filename);
  112.         switch(askreply(NO))    {
  113.         case QUIT:
  114.             return(1);
  115.         case NO:
  116.         if(attribute&DIRECTORY)
  117.             notrmd=0;
  118.         else
  119.         notrmd++;
  120.             return(0);
  121.     case ALL:
  122.         if(optset&OPTIONI)
  123.         optset &= ~OPTIONI;
  124.         else
  125.             optset |= OPTIONF;
  126.         }
  127.     }
  128.  
  129.     /* MS-DOS does not need write permission on directory to rmdir() it */
  130.     if(!(attribute & DIRECTORY) && (attribute & SAFEATTR) &&
  131.         chmod(file_match->filename,attribute&(~SAFEATTR)))  {
  132.     if(!(optset&OPTIONF))
  133.         fprintf(stderr,"%s: cannot chmod \"%s\" from %02x (status %d)\n",
  134.         progname,file_match->filename,attribute,errno);
  135.         return(1);
  136.     } else if(attribute&DIRECTORY)  {
  137.         if(notrmd)
  138.         return(notrmd=0);
  139.     else if(rmdir(file_match->filename) && !(optset&OPTIONF))   {
  140.             fprintf(stderr,"%s: cannot rmdir \"%s\" (status %d)\n",
  141.                 progname,file_match->filename,errno);
  142.             return(1);
  143.         }
  144.     } else if(unlink(file_match->filename) && !(optset&OPTIONF))    {
  145.         fprintf(stderr,"%s: cannot remove \"%s\" (status %d)\n",
  146.             progname,file_match->filename,errno);
  147.         return(1);
  148.     }
  149.  
  150.     if(!(optset&(OPTIONQ|OPTIONI)))
  151.         printf("%s: %s\n",progname,file_match->filename);
  152.  
  153.     return(0);
  154. }
  155.