sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

rmdir.c (716B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <libgen.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 
      6 #include "util.h"
      7 
      8 static void
      9 usage(void)
     10 {
     11 	eprintf("usage: %s [-p] dir ...\n", argv0);
     12 }
     13 
     14 int
     15 main(int argc, char *argv[])
     16 {
     17 	int pflag = 0, ret = 0;
     18 	char *d;
     19 
     20 	ARGBEGIN {
     21 	case 'p':
     22 		pflag = 1;
     23 		break;
     24 	default:
     25 		usage();
     26 	} ARGEND
     27 
     28 	if (!argc)
     29 		usage();
     30 
     31 	for (; *argv; argc--, argv++) {
     32 		if (rmdir(*argv) < 0) {
     33 			weprintf("rmdir %s:", *argv);
     34 			ret = 1;
     35 		} else if (pflag) {
     36 			d = dirname(*argv);
     37 			for (; strcmp(d, "/") && strcmp(d, ".") ;) {
     38 				if (rmdir(d) < 0) {
     39 					weprintf("rmdir %s:", d);
     40 					ret = 1;
     41 					break;
     42 				}
     43 				d = dirname(d);
     44 			}
     45 		}
     46 	}
     47 
     48 	return ret;
     49 }