7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
0
|
// rmdir.c, version 1.0.1 |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
1
|
// OMKOV coreutils implementation of POSIX rmdir |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
4
|
|
b181413 |
Jamozed |
2022-02-05 22:32:00 |
5
|
#include "util/error.h" |
b181413 |
Jamozed |
2022-02-05 22:32:00 |
6
|
#include "util/optget.h" |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
7
|
|
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
8
|
#include <unistd.h> |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
9
|
|
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
10
|
#include <stdbool.h> |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
11
|
#include <stdio.h> |
7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
12
|
|
7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
13
|
#define VERSION "1.0.1" |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
14
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
15
|
static struct lop lops[] = { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
16
|
{ "help", ARG_NUL, 256 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
17
|
{ "version", ARG_NUL, 257 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
18
|
{ NULL, 0, 0 } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
19
|
}; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
20
|
|
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
21
|
static bool pflag; |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
22
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
23
|
static void hlp(void); |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
24
|
static void ver(void); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
25
|
|
7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
26
|
int main(int ac, char *av[]) { A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
27
|
struct opt opt = OPTGET_INIT; opt.str = "p"; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
28
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
29
|
case 'p': { pflag = true; break; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
30
|
case 256: { hlp(); return 0; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
31
|
case 257: { ver(); return 0; } |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
32
|
default: { return 1; } |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
33
|
} |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
34
|
|
7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
35
|
if (opt.ind == ac) { error(1, "missing operand"); } |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
36
|
|
7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
37
|
for (char **p = &av[opt.ind]; *p; ++p) { |
7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
38
|
if (rmdir(*p)) { warn("%s: %s", *p, serr()); continue; } |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
39
|
if (pflag) { |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
40
|
char *c = *p; for (; *c; ++c); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
41
|
for (--c; *p < c && *(c - 1) == '/'; --c); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
42
|
for (*c = 0; *p < c; --c) { |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
43
|
if (*c != '/' || *(c - 1) == '/') { continue; } |
7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
44
|
*c = 0; if (rmdir(*p)) { warn("%s: %s", *p, serr()); break; } |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
45
|
} |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
46
|
} |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
47
|
} |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
48
|
|
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
49
|
return 0; |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
50
|
} |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
51
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
52
|
static void hlp(void) { |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
53
|
puts("rmdir - remove directories\n"); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
54
|
puts("usage: rmdir [-p] dir...\n"); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
55
|
puts("options:"); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
56
|
puts(" -p Remove all parent directories in a path"); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
57
|
puts(" --help Display help information"); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
58
|
puts(" --version Display version information"); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
59
|
} |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
60
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
61
|
static void ver(void) { |
7b4e9b3 |
Jamozed |
2020-07-06 23:25:19 |
62
|
puts("OMKOV coreutils rmdir, version " VERSION); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
63
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
64
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
dee2949 |
Jamozed |
2020-06-26 22:08:46 |
65
|
} |
|
|
|
66
|
|