aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rm/rm.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cmd/rm/rm.c')
-rw-r--r--sys/cmd/rm/rm.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/sys/cmd/rm/rm.c b/sys/cmd/rm/rm.c
new file mode 100644
index 0000000..7e83f5f
--- /dev/null
+++ b/sys/cmd/rm/rm.c
@@ -0,0 +1,119 @@
+#include <u.h>
+#include <libn.h>
+#include <sys/stat.h>
+
+/*
+ * globals
+ */
+
+static char errbuf[1024];
+static int numerrs;
+static struct
+{
+ uchar i : 1;
+ uchar f : 1;
+ uchar r : 1;
+} flag;
+
+/*
+ * utilities
+ */
+
+static
+void
+errors(char *fmt, ...)
+{
+ va_list args;
+
+ if(flag.f)
+ return;
+
+ va_start(args, fmt);
+ vsnprintf(errbuf, arrlen(errbuf), fmt, args);
+ va_end(args);
+
+ fprintf(stderr, "rm: %s\n", errbuf);
+ numerrs++;
+}
+
+static
+int
+yes(void)
+{
+ int i, b;
+ i = b = getchar();
+ while(b != '\n' && b != EOF)
+ b = getchar();
+
+ return(i== 'y');
+}
+
+static
+int
+rm(char *path, int level)
+{
+ int d;
+ struct stat buf;
+ if(stat(path, &buf)) {
+ errors("%s: non-existent", path);
+ return 1;
+ }
+
+ if((buf.st_mode&S_IFMT) == S_IFDIR) {
+ if(!flag.r) {
+ errors("%s: is directory", path);
+ return 1;
+ }
+ if(access(path, 02) < 0) {
+ errors("%s: not permitted", path);
+ return 1;
+ }
+ if(flag.i && level != 0) {
+ printf("directory %s: ", path);
+ if(!yes())
+ return 0;
+ }
+ if((d=
+ }
+}
+
+/*
+ * main point of entry
+ */
+
+static
+void
+usage(void)
+{
+ fprintf(stderr, "usage: rm [-ifr] file ...\n");
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i;
+ char *s;
+
+ ARGBEGIN{
+ case 'i':
+ flag.i = 1;
+ break;
+ case 'r':
+ flag.r = 1;
+ break;
+ case 'f':
+ flag.f = 1;
+ break;
+ default:
+ usage();
+ }ARGEND;
+
+ for(i = 0; i < argc; i++){
+ s = argv[i];
+ if (rm(s, 0) != -1)
+ continue;
+ }
+
+ exit(numerrs);
+}