5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
0
|
// realpath.c, version 1.0.1 |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
1
|
// OMKOV coreutils realpath |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
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" |
5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
7
|
|
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
8
|
#include <stdbool.h> |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
9
|
#include <stdio.h> |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
10
|
#include <stdlib.h> |
5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
11
|
|
5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
12
|
#define VERSION "1.0.1" |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
13
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
14
|
static struct lop lops[] = { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
15
|
{ "help", ARG_NUL, 256 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
16
|
{ "version", ARG_NUL, 257 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
17
|
{ NULL, 0, 0 } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
18
|
}; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
19
|
|
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
20
|
static inline int rpath(const char *path); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
21
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
22
|
static void hlp(void); |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
23
|
static void ver(void); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
24
|
|
5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
25
|
int main(int ac, char *av[]) { A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
26
|
struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
27
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
28
|
case 256: { hlp(); return 0; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
29
|
case 257: { ver(); return 0; } |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
30
|
default: { return 1; } |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
31
|
} |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
32
|
|
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
33
|
bool warned = false; |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
34
|
|
5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
35
|
if (opt.ind == ac) { rpath("."); } |
5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
36
|
else for (char **p = &av[opt.ind]; *p; ++p) if (rpath(*p)) { |
5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
37
|
warn("%s: %s\n", *p, serr()); warned = true; |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
38
|
} |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
39
|
|
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
40
|
return warned; |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
41
|
} |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
42
|
|
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
43
|
static inline int rpath(const char *file) { |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
44
|
char *path; |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
45
|
|
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
46
|
if (!(path = realpath(file, NULL))) { return 1; } |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
47
|
fputs(path, stdout); fputc('\n', stdout); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
48
|
|
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
49
|
free(path); return 0; |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
50
|
} |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
51
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
52
|
static void hlp(void) { |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
53
|
puts("realpath - resolve an absolute pathname\n"); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
54
|
puts("usage: realpath [file...]\n"); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
55
|
puts("options:"); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
56
|
puts(" --help Display help information"); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
57
|
puts(" --version Display version information"); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
58
|
} |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
59
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
60
|
static void ver() { |
5ee8803 |
Jamozed |
2020-07-06 23:22:05 |
61
|
puts("OMKOV coreutils realpath, version " VERSION); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
62
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
63
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
44d97c8 |
Jamozed |
2020-06-27 02:13:22 |
64
|
} |
|
|
|
65
|
|