home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / dirutl / tools.arc / RM.C < prev    next >
Text File  |  1987-09-23  |  5KB  |  214 lines

  1. /*
  2.  * rm.c remove a file or files
  3.  */
  4.  
  5. #include <stdio.h>              /* Standard I/O definitions */
  6. #include <dos.h>                /* Msdos definitions */
  7. #include <errno.h>              /* error codes */
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <io.h>
  13. #include <conio.h>
  14.  
  15. static char ID[]="## rm.c 1.0 K.van Houten 230987 ##";
  16. int isdir(char *);
  17. extern char **wild(char *);
  18. int rmfile(char *);
  19. int rmdirect(char *);
  20.  
  21. int fflg=0;                     /* FORCE flag (no errors) */
  22. int rflg=0;                     /* RECURSIVE flag */
  23. int iflg=0;                     /* Interactive flag */
  24. int tmp;                        /* for return values */
  25. struct stat statbuf;            /* for file status structure */
  26. char ibuf[10];
  27.  
  28. main (argc, argv)
  29. int     argc;                   /* Number of command line arguments */
  30. char   *argv[];                 /* Array of pointers to arguments */
  31. {
  32.     for (argc--,argv++; argc>0; argc--,argv++) {
  33.         if (**argv == '-') {
  34.             while (*++(*argv)) {
  35.                 switch (**argv) {
  36.                 case 'f':
  37.                     fflg++;
  38.                     break;
  39.                 case 'r':
  40.                     rflg++;
  41.                     break;
  42.                 case 'i':
  43.                     iflg++;
  44.                     break;
  45.                 default:
  46. usage:              printf("Usage: rm [-fir] <pathname> [<pathname> ..]\n");
  47.                     exit(1);
  48.                 }
  49.             }
  50.         }
  51.  
  52.         else
  53.             break;
  54.     }
  55.     /*
  56.      * NOW THE ARGV[0] CONTAINS THE FIRST NON-OPTION ARGUMENT,
  57.      * WHILE ARGC CONTAINS THE NUMBER OF ARGUMENTS LEFT.
  58.      * SO WE HAVE AVAILABLE ARGV[0] -- ARGV[ARGC-1].
  59.      */
  60.  
  61.     *ibuf = (char)7;
  62.  
  63.     if (argc < 1)
  64.         goto usage;
  65.  
  66.     if (rflg && !fflg) {
  67.         printf("rm: Are you sure you want recursive remove?");
  68.         cgets(ibuf);
  69.         putch('\n');
  70.         if (!((ibuf[2] == 'y') || (ibuf[2] == 'Y'))) {
  71.             exit(0);
  72.         }
  73.  
  74.     }
  75.  
  76.     while (argc > 0) {
  77.         /* check arg is dir ? */
  78.         if (stat(*argv,&statbuf) == -1) {
  79.             if (fflg == 0) {
  80.                 printf("rm: %s no such file or directory\n",*argv);
  81.             }
  82.             argc--;
  83.             argv++;
  84.             continue;
  85.         }
  86.         if (statbuf.st_mode & S_IFDIR) {
  87.             if (rflg) {
  88.                 (void)rmdirect(*argv);
  89.             }
  90.             else {
  91.                 if (fflg == 0) {
  92.                     printf("rm: %s is directory\n",*argv);
  93.                 }
  94.             }
  95.         }
  96.         else {
  97.             (void)rmfile(*argv);
  98.         }
  99.  
  100.         /* Next argument */
  101.         argc--;
  102.         argv++;
  103.     }
  104.     return(0);
  105. }
  106.  
  107. int
  108. rmfile(p)
  109. char *p;
  110. {
  111.     if (iflg) {
  112.         printf("rm: remove %s ?",p);
  113.         cgets(ibuf);
  114.         putch('\n');
  115.         if ((ibuf[2] == 'q') || (ibuf[2] == 'Q'))
  116.             exit(2);
  117.         if (!((ibuf[2] == 'y') || (ibuf[2] == 'Y'))) {
  118.             return(0);
  119.         }
  120.     }
  121.     if (fflg) {
  122. #ifdef DEBUG
  123.         tmp = 0;
  124.         printf("Changing mode %s\n",p);
  125. #else
  126.         tmp = chmod(p, S_IREAD | S_IWRITE);
  127. #endif
  128.         if (tmp == -1)
  129.             perror("chmod");
  130.     }
  131. #ifdef DEBUG
  132.     tmp = 0;
  133.     printf("Unlinking %s\n",p);
  134. #else
  135.     tmp = unlink(p);
  136. #endif
  137.     if (tmp == -1) {
  138.         perror("unlink");
  139.         return(0);
  140.     }
  141.     return(1);
  142. }
  143.  
  144. int
  145. rmdirect(p)
  146. char *p;
  147. {
  148.     char buf[100];
  149.     char base[100];
  150.     char **w;
  151.     int i;
  152.     int error = 0; /* counts errors */
  153.  
  154.     strcpy(base,p);
  155.     strcpy(buf,p);
  156.     if (buf[strlen(buf)-1] != '\\')
  157.        strcat(buf,"\\");
  158.     strcat(buf,"*.*\0");
  159.     w = wild(buf);
  160.     if (w != NULL) {
  161.         for (i=0;(w[i] != NULL);i++) {
  162.             strcpy(buf,base);
  163.             if (buf[strlen(buf)-1] != '\\')
  164.                 strcat(buf,"\\");
  165.             strcat(buf,w[i]);
  166.             if (isdir(buf) == 1) {
  167.                 if (rflg == 1) {
  168.                     if (rmdirect(buf) == 0) {
  169.                         error++;
  170.                     }
  171.                 }
  172.                 else {
  173.                     if (fflg == 0) {
  174.                         printf("rm: %s is directory\n",buf);
  175.                     }
  176.                     error++;
  177.                 }
  178.             }
  179.             else {
  180.                 if (rmfile(buf) == 0)
  181.                     error++;
  182.             }
  183.         }
  184.     }
  185.     /* all entrys done , now we can rmdir */
  186.     if (error == 0) {
  187. #ifdef DEBUG
  188.         tmp = 0;
  189.         printf("Removing Directory %s\n",base);
  190. #else
  191.         tmp = rmdir(base);
  192. #endif
  193.         if (tmp == -1) {
  194.             perror("rmdir");
  195.             return(0);
  196.         }
  197.         return(1);
  198.     }
  199.     return(0);
  200. }
  201.  
  202. int
  203. isdir(p)
  204. char *p;
  205. {
  206.     struct stat st;
  207.     if (stat(p,&st) == -1) {
  208.         if (fflg == 0) {
  209.             printf("rm: %s no such file or directory\n",p);
  210.         }
  211.     }
  212.     return((st.st_mode & S_IFDIR) ? 1 : 0);
  213. }
  214.