home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / c_spec / execute / rm.c < prev    next >
C/C++ Source or Header  |  1986-02-20  |  2KB  |  120 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3.  
  4. /*    RM.C    Remove a file or group of files: Usage:
  5.  *        Usage: rm -iv <filelist>
  6.  *
  7.  *        -i (interactive) Ask before deleting files.
  8.  *        -e (echo) Suppress diagnostic echo to screen.
  9.  */
  10.  
  11. static int    Verify    = 0;
  12. static int    Echo    = 1;
  13.  
  14. #define E(x) fprintf(stderr,"%s\n", x )
  15.  
  16. /*----------------------------------------------------------------------*/
  17.  
  18. args(argc, argv)
  19. char    **argv;
  20. {
  21.     /*    Process command line arguments. If -i iv found set
  22.      *    Verify true, if -e is found set Echo false. Return
  23.      *    1 if we found an argument, 0 otherwise
  24.      *    On entry argv should point at argv[1]
  25.      */
  26.  
  27.     register char    *p;
  28.  
  29.  
  30.     if( **argv == '-' )
  31.     {
  32.         for( p = *argv + 1 ; *p ; p++ )
  33.         {
  34.             if      ( *p == 'i' )    Verify = 1;
  35.             else if ( *p == 'e' )    Echo   = 0;
  36.             else    usage("Illegal argument" );
  37.         }
  38.  
  39.         if( Verify )
  40.             Echo = 1;
  41.  
  42.         return 1;
  43.     }
  44.  
  45.     return 0;
  46. }
  47.  
  48. /*----------------------------------------------------------------------*/
  49.  
  50.  
  51. usage(str)
  52. char    *str;
  53. {
  54.     E("Rm: Copyright (c) 1986, Allen I. Holub. All rights reserved\n");
  55.     E(str);
  56.     E("");
  57.     E("Remove (delete) a file or group of files, Usage is:\n");
  58.     E("           rm [-ie] file [... file]\n");
  59.     E("\t-i (interactive) Ask before deleting");
  60.     E("\t-e (echo) Suppress echo to screen\n");
  61.     E("You can't delete a directory with rm (use rmdir)\n");
  62.     exit(1);
  63. }
  64.  
  65. /*----------------------------------------------------------------------*/
  66.  
  67. main(argc, argv)
  68. char    **argv;
  69. {
  70.     register int    c;
  71.     register int    err;
  72.     int        exit_status = 0;
  73.  
  74.     reargv(&argc, &argv);
  75.  
  76.     if( argc < 2 )
  77.         usage("Too few arguments");
  78.  
  79.     if( args(--argc, ++argv) )    /* Process command line arguments */
  80.     {
  81.         ++argv ;
  82.         --argc ; 
  83.     }
  84.  
  85.     for(; --argc >= 0; argv++ )
  86.     {
  87.         if( Verify )
  88.         {
  89.             fprintf(stderr,"Delete    %-25s (y/n/q)?", *argv );
  90.             printf("%c\n", c = getch() );
  91.  
  92.             if( c == 'n' )
  93.                 continue;
  94.  
  95.             if( c == 'q' )
  96.                 break;
  97.         }
  98.  
  99.  
  100.         if( Echo )
  101.             printf("Deleting: %s", *argv );
  102.  
  103.         if( isdir(*argv) )
  104.             printf(": May not delete a directory");
  105.  
  106.         else if( (err = unlink(*argv)) == -1 )
  107.         {
  108.             fprintf( stderr,( err == EACCES ) 
  109.                 ? ". Not a file or file is read only"
  110.                 : ". File not found"               );
  111.  
  112.             exit_status = 1;
  113.         }
  114.  
  115.         printf("\n");
  116.     }
  117.  
  118.     exit( exit_status );
  119. }
  120.