Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* user unlink ... maybe qrm for query rm would be better
- * which is alot like rm, maybe erase or del, or remove would be better still.
- */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #ifndef S_IAMB
- #define S_IAMB (~(S_IFMT))
- #endif
- off_t readablefilesize(char * sizechar, off_t size_in_bytes )
- {
- int i;
- off_t size0;
- struct {
- char ctype;
- int div;
- } table [5] = {
- { 'b', 1 },
- { 'k', 1024 },
- { 'M', 1024*1024 },
- { 'G', 1024*1024*1024 },
- { 'T', 1024*1024*1024 } /* should 5 be 6? -- it's never come up */
- };
- i=0;
- size0=size_in_bytes;
- while ( (size0 /= 1024) >= 1 )
- i++;
- if ( i>=5 ) {
- *sizechar='b';
- return size_in_bytes;
- }
- *sizechar=table[i].ctype;
- return size_in_bytes/table[i].div ;
- }
- int main ( int argc, char * argv[] )
- {
- int i;
- unsigned int numerrs=0;
- unsigned int numdel=0;
- int haserrs=0;
- int dontdodel=0;
- int somenotowner=0;
- int longmode=0;
- struct stat statbuf;
- {
- #if 0
- int lenarg0;
- lenarg0=strlen(argv[0]);
- longmode=( lenarg0 == 0) || (argv[0][lenarg0-1] == 'l' ) ;
- #else
- /* remove vs erase*/
- char * ptr;
- ptr=strrchr(argv[0], '/');
- if ( !ptr )
- ptr=argv[0];
- else
- ptr++;
- longmode=( '\0' !=ptr[0] && 'r' == ptr[0] );
- #endif
- }
- for ( i=1; i < argc ; i++ ) {
- if ( -1 != lstat(argv[i],&statbuf)) {
- char typechar=' ';
- char sizec;
- int filesize;
- if ( statbuf.st_uid != getuid() ) {
- typechar='!';
- somenotowner=1;
- } else if ( S_ISDIR(statbuf.st_mode))
- typechar='D';
- else if ( S_ISFIFO(statbuf.st_mode))
- typechar='|';
- else if ( statbuf.st_mode & S_IFCHR )
- typechar='@'; /* sym link */
- else if ( S_ISBLK(statbuf.st_mode)
- || S_ISCHR(statbuf.st_mode)
- || S_ISSOCK(statbuf.st_mode)) {
- fprintf(stderr,"\nWill not operate on sockets or special files:%s\n",
- argv[i]);
- return 3;
- } else if ( ((statbuf.st_mode & S_IAMB ) & S_IEXEC))
- typechar='*';
- if ( longmode ) {
- filesize=readablefilesize(&sizec,statbuf.st_size);
- printf("%c %03o %4i%c %s\n", typechar,( statbuf.st_mode & S_IAMB ),
- filesize, sizec, argv[i]);
- } else { /* short mode */
- static int col=0;
- int cur_col;
- cur_col=col;
- }
- } else {
- fprintf(stderr,"lstat: %s : %s\n", argv[i], strerror(errno));
- numerrs++;
- haserrs=1;
- }
- }
- if ( haserrs ) {
- fprintf(stderr,"%d errors\n", numerrs);
- return 2;
- }
- if ( somenotowner ) {
- puts("Some files are not owned by you.");
- }
- printf("Delete %i file%s? ", argc-1, (argc-1 > 1 ? "s": ""));
- {
- char buf[81];
- fgets(buf,81,stdin);
- if ( strchr( buf, 'N' ) || strchr( buf, 'n' ) )
- dontdodel=1;
- else if ( strchr( buf, 'Y' ) || strchr( buf, 'y'))
- dontdodel=0;
- else
- dontdodel=1;
- }
- if ( dontdodel ) {
- puts("Cancelled!");
- return 1;
- }
- for ( i=1; i < argc ; i++ ) {
- if (-1 != remove(argv[i]) )
- numdel++;
- else {
- haserrs=1;
- fprintf(stderr,"remove: %s : %s\n", argv[i], strerror(errno));
- }
- }
- printf("Performed %d deletion%s.\n",numdel, (numdel > 1 ? "s": ""));
- return haserrs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement