diff --git a/src/rmdir.c b/src/rmdir.c new file mode 100644 index 0000000..cd994a2 --- /dev/null +++ b/src/rmdir.c @@ -0,0 +1,84 @@ +/* rmdir.c */ + + + +#include +extern int optind; +#include + + +/* rmdir */ +static int _rmdir_p(char * pathname); +static int _rmdir(int flagp, int argc, char * argv[]) +{ + int res = 0; + int i; + + for(i = 0; i < argc; i++) + { + if(flagp) + { + if(_rmdir_p(argv[i]) == -1) + res = 2; + continue; + } + if(rmdir(argv[i]) == -1) + { + perror(argv[i]); + res = 2; + } + } + return res; +} + +static int _rmdir_p(char * pathname) +{ + char * str; + + str = pathname; + while(*str++); + while(--str != pathname) + { + if(*str != '/') + continue; + *str = '\0'; + if(rmdir(pathname) == -1) + { + perror(pathname); + return -1; + } + } + return 0; +} + + +/* usage */ +static int _usage(void) +{ + fprintf(stderr, "Usage: rmdir [-p] dir...\n\ + -p remove all directories in a pathname\n"); + return 1; +} + + +/* main */ +int main(int argc, char * argv[]) +{ + int flagp = 0; + int o; + + while((o = getopt(argc, argv, "p")) != -1) + { + switch(o) + { + case 'p': + flagp = 1; + break; + case '?': + return _usage(); + } + } + if(optind == argc) + return _usage(); + return _rmdir(flagp, argc - optind, &argv[optind]); +}