commit 0c26c29860f604295a5258cab9384b67839fe57a
parent 689b713397599ca098850662a91cad236a513e62
Author: phoebos <ben@bvnf.space>
Date: Sat, 21 May 2022 18:39:32 +0100
rm: add
Diffstat:
4 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -8,6 +8,7 @@ ls
mkdir
nice
pwd
+rm
rmdir
sort
tee
diff --git a/Makefile b/Makefile
@@ -11,6 +11,7 @@ BINS = \
mkdir \
nice \
pwd \
+ rm \
rmdir \
sort \
tee \
@@ -20,7 +21,7 @@ BINS = \
wc \
CC = cc
-XCFLAGS = $(CFLAGS) -Wall -Wextra -Wpedantic -g
+XCFLAGS = $(CFLAGS) -Wall -Wextra -std=c99 -pedantic -g -Og
XLDFLAGS = $(LDFLAGS)
.PHONY: clean
diff --git a/TODO.posix b/TODO.posix
@@ -85,7 +85,7 @@
[ ] ps
[x] pwd
[ ] renice
-[ ] rm
+[.] rm
[.] rmdir
[ ] sed
[ ] sh
diff --git a/rm.c b/rm.c
@@ -0,0 +1,42 @@
+#define _XOPEN_SOURCE 700
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int
+rm(char *path) {
+ struct stat sb;
+ if (stat(path, &sb) == -1) {
+ fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
+ return 1;
+ }
+ if (unlink(path) == -1) {
+ fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
+ return 1;
+ }
+ return 0;
+}
+
+int
+main(int argc, char **argv) {
+ int c, ret = 0;
+ while ((c = getopt(argc, argv, "")) != -1) {
+ switch (c) {
+ default:
+ fprintf(stderr, "usage: %s file...\n", argv[0]);
+ return 1;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ while (argc--)
+ if (rm(*argv++) != 0)
+ ret = 1;
+
+ return ret;
+}