home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / RM_HS / RM.C next >
Text File  |  1989-02-17  |  4KB  |  197 lines

  1. /* rm.c - a UNIXish command for MSDOS
  2.  * developed under Turbo C 2.0
  3.  * Hume Smith
  4.  */
  5.  
  6. #include <dir.h>
  7. #include <io.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <conio.h>
  11. #include <dos.h>
  12. #include <errno.h>
  13. #include <stdio.h>
  14.  
  15. #define Recurse (1<<0)
  16. #define All (1<<1)
  17. #define Select (1<<2)
  18. #define Test (1<<3)
  19. #define Force (1<<4)
  20.  
  21. char Usage[]=
  22. "Usage: rm [-f] [-r] [-s] [-t] [-] name ...\n"
  23. "option\n"
  24. "-f - force     - delete read-only files without asking\n"
  25. "-r - recurse   - delete subdirectories too\n"
  26. "-s - selective - user is prompted at each file\n"
  27. "-t - test      - list files that would have been deleted without deleting\n"
  28. "-  - all following arguments are taken as filenames\n"
  29. "\nCaution!  Can make a big mess very fast!\n";
  30.  
  31. int Switches=0;
  32.  
  33. int Wait(void)
  34. {
  35.   int r;
  36.  
  37.   while(1)
  38.   {
  39.     r=getch();
  40.     if(r=='y'||r=='Y')
  41.     {
  42.       fprintf(stderr,"yes\n");
  43.       return !0;
  44.     }
  45.     if(r=='n'||r=='N')
  46.     {
  47.       fprintf(stderr,"no\n");
  48.       return 0;
  49.     }
  50.     if(r==3)
  51.     {
  52.       fprintf(stderr,"Break!\n");
  53.       exit(0);
  54.     }
  55.   }
  56. }
  57.  
  58. void Remove(char *Drive,char *Path,char *Name,char *Extension)
  59. {
  60.   struct ffblk FFBlk;
  61.   int r;
  62.   char Space[200];
  63.  
  64.   fnmerge(Space,Drive,Path,Name,Extension);
  65.   for(r=findfirst(Space,&FFBlk,(Switches&Recurse)?FA_HIDDEN|FA_DIREC:FA_HIDDEN);
  66.       !r;
  67.       r=findnext(&FFBlk)
  68.      )
  69.   {
  70.     /* avoid the "." and ".." files! */
  71.     if(!strcmp(FFBlk.ff_name,".")||!strcmp(FFBlk.ff_name,".."))
  72.       continue;
  73.  
  74.     /* if it is a directory, recurse */
  75.     if(FFBlk.ff_attrib&FA_DIREC)
  76.     {
  77.       strcpy(Space,Path);
  78.       strcat(Space,FFBlk.ff_name);
  79.       strcat(Space,"\\");
  80.       Remove(Drive,Space,"*",".*");
  81.     }
  82.  
  83.     /* build full name */
  84.     strcpy(Space,Drive);
  85.     strcat(Space,Path);
  86.     strcat(Space,FFBlk.ff_name);
  87.  
  88.     /* did the user want selectiveness? */
  89.     if(Switches&Select)
  90.     {
  91.       fprintf(stderr,
  92.               "Remove %s%s %s? ",
  93.               (FFBlk.ff_attrib&FA_HIDDEN)?"(hidden) ":"",
  94.               (FFBlk.ff_attrib&FA_DIREC)?"Directory":"File",
  95.               Space
  96.              );
  97.       if(Wait())
  98.         goto Delete;
  99.       else
  100.         goto Skip;
  101.     }
  102.  
  103. Delete:
  104.     /* unlink would fail on these */
  105.     if(FFBlk.ff_attrib&FA_RDONLY)
  106.     {
  107.       /* if the force switch is set */
  108.       if(Switches&Force)
  109.         goto ChMod;
  110.  
  111.       /* double-check with user */
  112.       fprintf(stderr,"Remove (read-only) %s? ",Space);
  113.  
  114.       /* if he says no */
  115.       if(!Wait())
  116.         goto Skip;
  117.  
  118. ChMod:
  119.       /* change mode */
  120.       if(chmod(Space,~FA_RDONLY))
  121.       {
  122.         printf("Error %d (%s) in chmod\n",errno,sys_errlist[errno]);
  123.         goto Skip;
  124.       }
  125.     }
  126.  
  127.     /* farewell */
  128.     if(Switches&Test)
  129.       printf("%s\n",Space);
  130.     else
  131.       if(FFBlk.ff_attrib&FA_DIREC)
  132.         if(rmdir(Space))
  133.           printf("Error %d (%s) in rmdir(%s)\n",
  134.                  errno,
  135.                  sys_errlist[errno],
  136.                  Space
  137.                 );
  138.         else
  139.           printf("%s Deleted\n",Space);
  140.       else
  141.         if(unlink(Space))
  142.           printf("Error %d (%s) in unlink(%s)\n",
  143.                  errno,
  144.                  sys_errlist[errno],
  145.                  Space
  146.                 );
  147.         else
  148.           printf("%s Deleted\n",Space);
  149. Skip:
  150.     ;
  151.   }
  152. }
  153.  
  154. void main(int ArgC,char **ArgV)
  155. {
  156.   int i,n=0;
  157.   char Drive[5],Path[100],Name[13],Extension[4];
  158.  
  159.   /* check all args */
  160.   for(i=1;i<ArgC;i++)
  161.     /* if an option */
  162.     if(!(Switches&All)&&ArgV[i][0]=='-')
  163.       /* check which */
  164.       switch(ArgV[i][1])
  165.       {
  166.       case 0:
  167.         Switches|=All;
  168.         break;
  169.       case 'f':
  170.         Switches|=Force;
  171.         break;
  172.       case 'r':
  173.         Switches|=Recurse;
  174.         break;
  175.       case 's':
  176.         Switches|=Select;
  177.         break;
  178.       case 't':
  179.         Switches|=Test;
  180.         break;
  181.       default:
  182.         fprintf(stderr,"rm: unknown option %s\n",ArgV[i]);
  183.       }
  184.     else
  185.       /* pack list */
  186.       ArgV[++n]=ArgV[i];
  187.  
  188.   if(!n)
  189.     printf(Usage);
  190.   else
  191.     for(i=1;i<=n;i++)
  192.     {
  193.       fnsplit(ArgV[i],Drive,Path,Name,Extension);
  194.       Remove(Drive,Path,Name,Extension);
  195.     }
  196. }
  197.