From 53386153cc7b3eb15d4625aec16af9bec4e74e46 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sun, 12 Jul 2020 13:07:10 -0400 Subject: initial version --- rempty.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 rempty.c diff --git a/rempty.c b/rempty.c new file mode 100644 index 0000000..c8846d9 --- /dev/null +++ b/rempty.c @@ -0,0 +1,73 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include + +#ifndef OPEN_MAX +#define OPEN_MAX _POSIX_OPEN_MAX +#endif + +static char *progname = NULL; +static int retval = 0; + +static int rempty(const char *path, const struct stat *st, int type, struct FTW *ftw) +{ + (void)st; + (void)ftw; + + if (type != FTW_DP) { + return 0; + } + + if (rmdir(path) == 0) { + return 0; + } + + if (errno == EEXIST || errno == ENOTEMPTY) { + return 0; + } + + fprintf(stderr, "%s: %s: %s\n", progname, path, strerror(errno)); + retval = 1; + + return 0; +} + +static void usage(void) +{ + fprintf(stderr, "usage: %s dir...\n", progname); +} + +int main(int argc, char *argv[]) +{ + progname = argv[0]; + setlocale(LC_ALL, ""); + + int c; + while ((c = getopt(argc, argv, "")) != -1) { + switch (c) { + default: + usage(); + return 1; + } + } + + if (optind >= argc) { + usage(); + return 1; + } + + while (optind < argc) { + if (nftw(argv[optind], rempty, OPEN_MAX, FTW_DEPTH) != 0) { + fprintf(stderr, "%s: %s: %s\n", progname, argv[optind], + strerror(errno)); + } + optind++; + } + + return retval; +} -- cgit v1.2.1