ecf4694 |
Jamozed |
2020-07-06 23:16:54 |
0
|
// pwd.c, version 1.0.1 |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
1
|
// OMKOV coreutils implementation of POSIX pwd |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
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" |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
7
|
|
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
8
|
#include <unistd.h> |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
9
|
|
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
10
|
#include <stdio.h> |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
11
|
#include <stdlib.h> |
ecf4694 |
Jamozed |
2020-07-06 23:16:54 |
12
|
|
ecf4694 |
Jamozed |
2020-07-06 23:16:54 |
13
|
#define VERSION "1.0.1" |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
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
|
|
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
21
|
static int mode = 0; |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
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); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
25
|
|
b634f01 |
Jamozed |
2020-07-09 22:11:16 |
26
|
int main(int ac, char *av[]) { (void)ac; A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
27
|
struct opt opt = OPTGET_INIT; opt.str = "LP"; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
28
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
29
|
case 'L': { mode = 0; break; } |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
30
|
case 'P': { mode = 1; break; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
31
|
case 256: { hlp(); return 0; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
32
|
case 257: { ver(); return 0; } |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
33
|
default: { return 1; } |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
34
|
} |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
35
|
|
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
36
|
pwd:; |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
37
|
char *cwd = mode ? getcwd(NULL, 0) : getenv("PWD"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
38
|
if (!cwd && !mode) { mode = 1; goto pwd; } |
ecf4694 |
Jamozed |
2020-07-06 23:16:54 |
39
|
else if (!cwd) { error(1, "%s", av[0]); } |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
40
|
|
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
41
|
fputs(cwd, stdout); fputc('\n', stdout); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
42
|
|
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
43
|
if (mode) { free(cwd); } return 0; |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
44
|
} |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
45
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
46
|
static void hlp(void) { |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
47
|
puts("pwd - print working directory name\n"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
48
|
puts("usage: pwd [-L|-P]\n"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
49
|
puts("options:"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
50
|
puts(" -L Display the logical current working directory"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
51
|
puts(" -P Display the physical current working directory"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
52
|
puts(" --help Display help information"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
53
|
puts(" --version Display version information"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
54
|
} |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
55
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
56
|
static void ver(void) { |
ecf4694 |
Jamozed |
2020-07-06 23:16:54 |
57
|
puts("OMKOV coreutils pwd, version " VERSION); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
58
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
59
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
b3fcfc9 |
Jamozed |
2020-06-26 22:55:54 |
60
|
} |
|
|
|
61
|
|