f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
0
|
// basename.c, version 1.0.1 |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
1
|
// OMKOV coreutils implementation of POSIX basename |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
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" |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
7
|
|
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
8
|
#include <stdio.h> |
f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
9
|
|
f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
10
|
#define VERSION "1.0.1" |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
11
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
12
|
static struct lop lops[] = { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
13
|
{ "help", ARG_NUL, 256 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
14
|
{ "version", ARG_NUL, 257 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
15
|
{ NULL, 0, 0 } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
16
|
}; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
17
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
18
|
static void hlp(void); |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
19
|
static void ver(void); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
20
|
|
f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
21
|
int main(int ac, char *av[]) { A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
22
|
struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
23
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
24
|
case 256: { hlp(); return 0; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
25
|
case 257: { ver(); return 0; } |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
26
|
default: { return 1; } |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
27
|
} |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
28
|
|
f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
29
|
if (opt.ind == ac) { error(1, "missing operand"); } |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
30
|
|
f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
31
|
register char *p = av[opt.ind]; |
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
32
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
33
|
// If the string is empty, print "." |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
34
|
if (!*p) { fputc('.', stdout); fputc('\n', stdout); return 0; } |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
35
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
36
|
// From the end of the string, move left to the first non '/' character |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
37
|
for (++p; *p; ++p) {} for (--p; *p == '/'; --p); |
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
38
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
39
|
// If the string contains only '/' characters, print "/" |
f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
40
|
if (p + 1 == av[opt.ind]) { fputs("/\n", stdout); return 0; } |
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
41
|
else { p[1] = 0; } // Otherwise remove the trailing '/' characters |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
42
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
43
|
if (av[opt.ind + 1]) { // If a suffix operand is provided |
f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
44
|
register char *s = av[opt.ind + 1]; for (; *s; ++s); |
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
45
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
46
|
// Move left through string and suffix as long as they are the same |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
47
|
for (--s; *p && *p != '/' && *s && *p == *s; --p, --s); |
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
48
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
49
|
// If the suffix is matched completely and characters remain in the |
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
50
|
// string without it, remove the suffix from the string |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
51
|
if (!*s && *p && *p != '/') { p[1] = 0; } |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
52
|
} |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
53
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
54
|
// Move pointer left until the start of the string or '/' is found and print |
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
55
|
for (; *p && *p != '/'; --p) {} fputs(p + 1, stdout); fputc('\n', stdout); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
56
|
return 0; |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
57
|
} |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
58
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
59
|
/* Print help information */ |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
60
|
static void hlp(void) { |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
61
|
puts("basename - return the non-directory portion of a path\n"); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
62
|
puts("usage: basename string [suffix]\n"); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
63
|
puts("options:"); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
64
|
puts(" --help Display help information"); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
65
|
puts(" --version Display version information"); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
66
|
} |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
67
|
|
fbdf077 |
Jamozed |
2020-09-17 01:05:46 |
68
|
/* Print version information */ |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
69
|
static void ver(void) { |
f9068a8 |
Jamozed |
2020-07-06 21:59:53 |
70
|
puts("OMKOV coreutils basename, version " VERSION); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
71
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
72
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
703c1e1 |
Jamozed |
2020-06-26 15:54:59 |
73
|
} |
|
|
|
74
|
|