Advertisement
shiftdot515

rmio.c

Aug 30th, 2020
2,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <limits.h>
  5.  
  6.  
  7. #ifdef FLOCK
  8.  
  9. // define this, and it will hang on pipes, like this
  10. // rmio -v $(<stillneed)
  11. // rmio -v $(cat stillneed)
  12. // vs
  13. // rmio -v <stillneed
  14. // but not, true pipes, like,
  15. // cp rmio* /tmp/ ; cd /tmp
  16. // ls -1 r*| rmio -v
  17. // was that what I wanted?
  18. // otherwise, sees pointless,
  19. // as flockfile(FILE *) ;
  20. // seems to be for coprocesses, and threads, or coroutines, etc.
  21. // true for uname -sr = NetBSD 5.2.3
  22. #endif
  23.  
  24.  
  25. void usage(void);
  26.  
  27. int verbose;
  28. int directory;
  29.  
  30. int main ( int argc, char * argv[] )
  31. {
  32.     int numerrs=0;
  33.     char pathbuf[PATH_MAX*2];
  34.     int len; /* current path length */
  35.     char  delim; /* delimiter */
  36.     int c;
  37.  
  38.     delim='\n';  /* newline */
  39.     verbose=0;
  40.     directory=0;
  41.  
  42.     do {  /* option processing w/ getopt */
  43.         c=getopt(argc,argv, "z0nvdh?");
  44.         switch(c) {
  45.         case 'z':
  46.         case '0':
  47.             delim='\0';  /* Null char , find . -print0 */
  48.             break;
  49.         case 'n':
  50.             delim='\n';  /* newline */
  51.             break;
  52.         case 'v':
  53.             verbose=1 ;
  54.             break;
  55.         case 'd':
  56.             directory=1;
  57.             puts("Feature not implemented.");
  58.             /*drop */
  59.         case '?':
  60.         case 'h':
  61.             usage();
  62.             return 1;
  63.  
  64.         }
  65.     } while (c != -1 ) ;
  66.      argc -= optind;
  67.      argv += optind;
  68.  
  69.  
  70.     len=0;
  71. #ifdef FLOCK
  72.     flockfile(stdin);
  73. #endif    
  74.     for (;;) {
  75.         c=getc(stdin);
  76.         if ( EOF == c )
  77.             break;  /* break out of loop */
  78.         if ( c != delim )  
  79.             pathbuf[len++]=c;  /* fill in pathbuf */
  80.         else {
  81.             pathbuf[len]=0; /* make buf into string */
  82.             if (verbose )
  83.               puts(pathbuf);
  84.             if ( unlink(pathbuf) ) {  /* unlink the file or directory*/
  85.                 perror("unlink:");
  86.                 fputs(pathbuf,stderr);
  87.                 fputs("\n", stderr);
  88.                 numerrs++;
  89.             }
  90.             len=0;
  91.         }
  92.  
  93.     }
  94. #ifdef FLOCK
  95.     funlockfile(stdin);
  96. #endif    
  97.     return numerrs;
  98. }
  99.  
  100. void usage(void)
  101. {
  102.     puts("rmio -z0nvhd?");
  103.     puts("");
  104.     puts("     z/0    null delimited -- find . -print 0 ");
  105.     puts("     n      \\n delimited");
  106.     puts("     v      print file name, before attempting unlink");
  107.     puts("     d      stack non empty directories, in order to try again");
  108.  
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement