summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-03 08:58:19 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-03 08:58:19 -0400
commit25082b8a3ecae2c1b1a4babe37a56f6c2efc48c1 (patch)
tree130edef1db037d4dd1509da1133b44c7fe29507a
parent215614ec50d427f427750e647d0412a05d97345b (diff)
move main loop to function rm_dir()
-rw-r--r--rmdir.c47
1 files changed, 27 insertions, 20 deletions
diff --git a/rmdir.c b/rmdir.c
index ebe5298..c4ee227 100644
--- a/rmdir.c
+++ b/rmdir.c
@@ -25,15 +25,38 @@
#define _XOPEN_SOURCE 500
#include <errno.h>
#include <libgen.h>
+#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+static int rm_dir(char *path, int prune)
+{
+ if (prune) {
+ char *working = path;
+ while (strcmp(working, ".") && strcmp(working, "/")) {
+ if (rmdir(working) != 0) {
+ fprintf(stderr, "rmdir: %s: %s\n",
+ working, strerror(errno));
+ return 1;
+ }
+ working = dirname(working);
+ }
+
+ } else if (rmdir(path) != 0) {
+ fprintf(stderr, "rmdir: %s: %s\n", path, strerror(errno));
+ return 1;
+ }
+
+ return 0;
+}
+
int main(int argc, char *argv[])
{
- int prune = 0;
+ setlocale(LC_ALL, "");
int c;
+ int prune = 0;
while ((c = getopt(argc, argv, "p")) != -1) {
switch (c) {
case 'p':
@@ -49,26 +72,10 @@ int main(int argc, char *argv[])
return 1;
}
- int retval = 0;
+ int ret = 0;
do {
- if (prune) {
- char *working = argv[optind];
- while (strcmp(working, ".") && strcmp(working, "/")) {
- if (rmdir(working) != 0) {
- fprintf(stderr, "rmdir: %s: %s\n",
- working, strerror(errno));
- retval = 1;
- break;
- }
- working = dirname(working);
- }
-
- } else if (rmdir(argv[optind]) != 0) {
- fprintf(stderr, "rmdir: %s: %s\n", argv[optind],
- strerror(errno));
- retval = 1;
- }
+ ret |= rm_dir(argv[optind], prune);
} while (++optind < argc);
- return retval;
+ return ret;
}