Fix build error by adding -fno-stack-protector to CFLAGS in usr/LBuild (#63)
[lunaix-os.git] / lunaix-os / usr / rm.c
1 #include <errno.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5
6 int
7 main(int argc, const char* argv[])
8 {
9     if (argc != 2) {
10         printf("expect a file name\n");
11         return 1;
12     }
13
14     int err, fd;
15     char* path = argv[1];
16     struct file_stat stat;
17
18     fd = open(path, O_RDONLY);
19                 
20     if (fd < 0) {
21         printf("open failed: %s (error: %d)", path, fd);
22         return 1;
23     }
24
25     if (fstat(fd, &stat) < 0) {
26         printf("fail to get stat %d\n", errno);
27         return 1;
28     }
29
30     close(fd);
31
32     if (((stat.st_mode >> 16) & F_DIR)) {
33         err = rmdir(path);
34     }
35     else {
36         err = unlink(path);
37     }
38
39     if (err) {
40         printf("fail to delete: %s (%d)", path, errno);
41     }
42
43     return err;
44 }