Second Extended Filesystem (ext2) and other improvements (#33)
[lunaix-os.git] / lunaix-os / usr / rm.c
diff --git a/lunaix-os/usr/rm.c b/lunaix-os/usr/rm.c
new file mode 100644 (file)
index 0000000..7fea833
--- /dev/null
@@ -0,0 +1,44 @@
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+int
+main(int argc, const char* argv[])
+{
+    if (argc != 2) {
+        printf("expect a file name\n");
+        return 1;
+    }
+
+    int err, fd;
+    char* path = argv[1];
+    struct file_stat stat;
+
+    fd = open(path, O_RDONLY);
+                
+    if (fd < 0) {
+        printf("open failed: %s (error: %d)", path, fd);
+        return 1;
+    }
+
+    if (fstat(fd, &stat) < 0) {
+        printf("fail to get stat %d\n", errno);
+        return 1;
+    }
+
+    close(fd);
+
+    if ((stat.mode & F_DIR)) {
+        err = rmdir(path);
+    }
+    else {
+        err = unlink(path);
+    }
+
+    if (err) {
+        printf("fail to delete: %s (%d)", path, errno);
+    }
+
+    return err;
+}
\ No newline at end of file