From: Fredrik Tolf Date: Fri, 30 Sep 2011 13:13:22 +0000 (+0200) Subject: nextep: Remove empty data directories when unsetting values. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=1bed4569ed2f2b64f83e18be6f118a035f076684;p=utils.git nextep: Remove empty data directories when unsetting values. --- diff --git a/nextep.c b/nextep.c index 0a8b6b6..151db93 100644 --- a/nextep.c +++ b/nextep.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -15,6 +16,26 @@ static int ensuredir(char *base, char *dir) return(0); } +static void checkempty(char *dir) +{ + DIR *dh; + int empty; + struct dirent *de; + + if((dh = opendir(dir)) == NULL) + return; + empty = 1; + while((de = readdir(dh)) != NULL) { + if(de->d_name[0] != '.') { + empty = 0; + break; + } + } + closedir(dh); + if(empty) + rmdir(dir); +} + static void usage(FILE *out) { fprintf(out, "usage: nextep [-h] [-f FILE] [-s SET-VALUE] [DIR]\n"); @@ -81,6 +102,7 @@ int main(int argc, char **argv) fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno)); exit(1); } + checkempty(base); } else { if(ensuredir(buf, base)) { fprintf(stderr, "nextep: %s: %s\n", base, strerror(errno));