summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-02 19:36:12 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-02 19:36:12 -0400
commitb37f62c10c0c099a1d49f3823b3fb40eb494d6f1 (patch)
tree3020d5c5aef261be8378fddf1c1d999c25e6f714
parent45005cdc20fb445aa8d76ac774efd2dc72a8191d (diff)
add more braces and correct return values
-rw-r--r--rm.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/rm.c b/rm.c
index e1412d7..6cd2884 100644
--- a/rm.c
+++ b/rm.c
@@ -18,18 +18,15 @@
*/
#define _XOPEN_SOURCE 700
+#include <errno.h>
+#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
-#include <unistd.h>
-#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
-#include <ftw.h>
-
-const char *rm_desc = "remove directory entries";
-const char *rm_inv = "rm [-fiRr] file...";
+#include <unistd.h>
static int force = 0;
static int interactive = 0;
@@ -63,8 +60,9 @@ static int rm(const char *p)
{
struct stat st;
if (lstat(p, &st) != 0) {
- if (!force)
+ if (!force) {
perror(p);
+ }
return 1;
}
@@ -73,24 +71,31 @@ static int rm(const char *p)
fprintf(stderr, "%s: %s\n", p, strerror(EISDIR));
return 1;
}
+
if (!force && ((access(p, W_OK) != 0 && isatty(fileno(stdin)))
|| interactive)) {
- if (rm_prompt(p) == 0)
+ if (rm_prompt(p) == 0) {
return 0;
+ }
}
- if (rmdir(p) != 0)
+
+ if (rmdir(p) != 0) {
perror(p);
+ return 1;
+ }
return 0;
}
if (!force && ((access(p, W_OK) != 0 && isatty(fileno(stdin)))
|| interactive)) {
- if (rm_prompt(p) == 0)
+ if (rm_prompt(p) == 0) {
return 0;
+ }
}
- if (unlink(p) != 0)
+ if (unlink(p) != 0) {
perror(p);
+ }
return 0;
}
@@ -104,14 +109,17 @@ int main(int argc, char **argv)
force = 1;
interactive = 0;
break;
+
case 'i':
force = 0;
interactive = 1;
break;
+
case 'r':
case 'R':
recursive = 1;
break;
+
default:
return 1;
}
@@ -120,13 +128,15 @@ int main(int argc, char **argv)
if (optind >= argc)
return 1;
+ int ret = 0;
while (optind < argc) {
- if (recursive)
- nftw(argv[optind], nftw_rm, 1024, FTW_DEPTH);
- else
- rm(argv[optind]);
+ if (recursive) {
+ ret |= nftw(argv[optind], nftw_rm, 1024, FTW_DEPTH);
+ } else {
+ ret |= rm(argv[optind]);
+ }
optind++;
}
- return 0;
+ return ret;
}