commit 74036a7496e94ed5761006723793ed2aed2ccd31
parent 0c26c29860f604295a5258cab9384b67839fe57a
Author: phoebos <ben@bvnf.space>
Date: Sat, 21 May 2022 18:46:56 +0100
rm: add -f
Diffstat:
M | rm.c | | | 29 | +++++++++++++++++++---------- |
1 file changed, 19 insertions(+), 10 deletions(-)
diff --git a/rm.c b/rm.c
@@ -6,27 +6,31 @@
#include <sys/stat.h>
#include <unistd.h>
+int FLAG_f;
+
int
rm(char *path) {
struct stat sb;
- if (stat(path, &sb) == -1) {
- fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
+ if (stat(path, &sb) == -1)
return 1;
- }
- if (unlink(path) == -1) {
- fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
+
+ if (unlink(path) == -1)
return 1;
- }
+
return 0;
}
int
main(int argc, char **argv) {
int c, ret = 0;
- while ((c = getopt(argc, argv, "")) != -1) {
+ FLAG_f = 0;
+ while ((c = getopt(argc, argv, "f")) != -1) {
switch (c) {
+ case 'f':
+ FLAG_f = 1;
+ break;
default:
- fprintf(stderr, "usage: %s file...\n", argv[0]);
+ fprintf(stderr, "usage: %s [-f] file...\n", argv[0]);
return 1;
}
}
@@ -34,9 +38,14 @@ main(int argc, char **argv) {
argc -= optind;
argv += optind;
- while (argc--)
- if (rm(*argv++) != 0)
+ while (argc--) {
+ if (rm(*argv++) != 0) {
+ if (FLAG_f)
+ continue;
+ fprintf(stderr, "rm: %s: %s\n", argv[-1], strerror(errno));
ret = 1;
+ }
+ }
return ret;
}