Advertisement
shiftdot515

remove.c

Jul 9th, 2019
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 KB | None | 0 0
  1. /* user unlink ... maybe qrm for query rm would be better
  2.  * which is alot like rm, maybe erase or del, or remove would be better still.
  3.  */
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #include <string.h>
  10.  
  11. #ifndef S_IAMB
  12. #define S_IAMB (~(S_IFMT))
  13. #endif
  14.  
  15.  
  16. off_t readablefilesize(char * sizechar, off_t size_in_bytes )
  17. {
  18.   int i;
  19.   off_t size0;
  20.   struct {
  21.     char ctype;
  22.     int div;
  23.   } table [5] = {
  24.     { 'b',  1  },
  25.     { 'k',  1024 },
  26.     { 'M',  1024*1024 },
  27.     { 'G',  1024*1024*1024 },
  28.     { 'T',  1024*1024*1024 }   /* should 5 be 6? -- it's never come up */
  29.   };
  30.   i=0;
  31.   size0=size_in_bytes;
  32.   while ( (size0 /= 1024) >= 1 )
  33.     i++;
  34.   if ( i>=5 ) {
  35.     *sizechar='b';
  36.     return size_in_bytes;
  37.   }
  38.   *sizechar=table[i].ctype;
  39.   return size_in_bytes/table[i].div ;
  40. }
  41.  
  42.  
  43. int main ( int argc, char * argv[] )
  44. {
  45.   int i;
  46.   unsigned int numerrs=0;
  47.   unsigned int numdel=0;
  48.   int haserrs=0;
  49.   int dontdodel=0;
  50.   int somenotowner=0;
  51.   int longmode=0;
  52.   struct stat statbuf;
  53.  
  54.   {
  55. #if 0
  56.   int lenarg0;
  57.   lenarg0=strlen(argv[0]);
  58.   longmode=(  lenarg0 == 0) || (argv[0][lenarg0-1] == 'l' ) ;
  59. #else
  60.   /* remove vs erase*/
  61.   char * ptr;
  62.   ptr=strrchr(argv[0], '/');
  63.   if ( !ptr )
  64.     ptr=argv[0];
  65.   else
  66.     ptr++;
  67.   longmode=( '\0' !=ptr[0] && 'r' == ptr[0] );
  68. #endif
  69.   }
  70.  
  71.   for ( i=1; i < argc ; i++ ) {
  72.     if ( -1 != lstat(argv[i],&statbuf)) {
  73.       char typechar=' ';
  74.       char sizec;
  75.       int  filesize;
  76.       if ( statbuf.st_uid != getuid() ) {
  77.         typechar='!';
  78.         somenotowner=1;
  79.       } else if ( S_ISDIR(statbuf.st_mode))
  80.         typechar='D';
  81.       else if ( S_ISFIFO(statbuf.st_mode))
  82.         typechar='|';
  83.        else if ( statbuf.st_mode & S_IFCHR )
  84.         typechar='@'; /* sym link */
  85.       else if ( S_ISBLK(statbuf.st_mode)
  86.                 || S_ISCHR(statbuf.st_mode)
  87.                 || S_ISSOCK(statbuf.st_mode)) {
  88.         fprintf(stderr,"\nWill not operate on sockets or special files:%s\n",
  89.                 argv[i]);
  90.         return 3;
  91.       } else if ( ((statbuf.st_mode & S_IAMB ) & S_IEXEC))
  92.         typechar='*';
  93.       if ( longmode ) {
  94.         filesize=readablefilesize(&sizec,statbuf.st_size);
  95.         printf("%c %03o %4i%c %s\n", typechar,( statbuf.st_mode & S_IAMB ),
  96.                filesize, sizec, argv[i]);
  97.       } else { /* short mode */
  98.         static int col=0;
  99.         int cur_col;
  100.         cur_col=col;
  101.       }
  102.     } else {
  103.       fprintf(stderr,"lstat: %s : %s\n", argv[i], strerror(errno));
  104.       numerrs++;
  105.       haserrs=1;
  106.     }
  107.   }
  108.   if ( haserrs ) {
  109.     fprintf(stderr,"%d errors\n", numerrs);
  110.     return 2;
  111.   }
  112.   if ( somenotowner ) {
  113.     puts("Some files are not owned by you.");
  114.   }
  115.   printf("Delete %i file%s? ", argc-1, (argc-1 > 1 ? "s": ""));
  116.   {
  117.     char buf[81];
  118.     fgets(buf,81,stdin);
  119.     if ( strchr( buf, 'N' ) || strchr( buf, 'n' ) )
  120.       dontdodel=1;
  121.     else if ( strchr( buf, 'Y' ) || strchr( buf, 'y'))
  122.       dontdodel=0;
  123.     else
  124.       dontdodel=1;
  125.   }
  126.   if ( dontdodel ) {
  127.     puts("Cancelled!");
  128.     return 1;
  129.   }
  130.  
  131.   for ( i=1; i < argc ; i++ ) {
  132.     if (-1 != remove(argv[i]) )
  133.      numdel++;
  134.     else {
  135.       haserrs=1;
  136.       fprintf(stderr,"remove: %s : %s\n", argv[i], strerror(errno));
  137.     }
  138.   }
  139.   printf("Performed %d deletion%s.\n",numdel, (numdel > 1 ? "s": ""));
  140.   return haserrs;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement