utils/src/rmdir.c
2004-03-26 17:58:41 +00:00

85 lines
1.1 KiB
C

/* rmdir.c */
#include <unistd.h>
extern int optind;
#include <stdio.h>
/* 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]);
}