coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/dirname.c (61 lines, 1.5 KiB) -rw-r--r-- file download

baad6e1 Jamozed 2020-07-06 22:24:24
0
// dirname.c, version 1.0.1
fe24453 Jamozed 2020-06-26 16:25:00
1
// OMKOV coreutils implementation of POSIX dirname
fe24453 Jamozed 2020-06-26 16:25:00
2
// Copyright (C) 2020, Jakob Wakeling
e2140ec Jamozed 2022-03-06 15:27:45
3
// MIT Licence
fe24453 Jamozed 2020-06-26 16:25:00
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"
fe24453 Jamozed 2020-06-26 16:25:00
7
fe24453 Jamozed 2020-06-26 16:25:00
8
#include <stdio.h>
fe24453 Jamozed 2020-06-26 16:25:00
9
baad6e1 Jamozed 2020-07-06 22:24:24
10
#define VERSION "1.0.1"
baad6e1 Jamozed 2020-07-06 22:24:24
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);
fe24453 Jamozed 2020-06-26 16:25:00
20
baad6e1 Jamozed 2020-07-06 22:24:24
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; }
fe24453 Jamozed 2020-06-26 16:25:00
26
	default: { return 1; }
fe24453 Jamozed 2020-06-26 16:25:00
27
	}
fe24453 Jamozed 2020-06-26 16:25:00
28
	
baad6e1 Jamozed 2020-07-06 22:24:24
29
	if (opt.ind == ac) { error(1, "missing operand"); }
fe24453 Jamozed 2020-06-26 16:25:00
30
	
baad6e1 Jamozed 2020-07-06 22:24:24
31
	register char *p = av[opt.ind];
fe24453 Jamozed 2020-06-26 16:25:00
32
	
fe24453 Jamozed 2020-06-26 16:25:00
33
	for (; *p; ++p) {} for (--p; *p == '/'; --p);
baad6e1 Jamozed 2020-07-06 22:24:24
34
	if (p + 1 == av[opt.ind]) { fputs("/\n", stdout); return 0; }
fe24453 Jamozed 2020-06-26 16:25:00
35
	
fe24453 Jamozed 2020-06-26 16:25:00
36
	for (; *p && *p != '/'; --p);
baad6e1 Jamozed 2020-07-06 22:24:24
37
	if (p + 1 == av[opt.ind]) { fputs(".\n", stdout); return 0; }
fe24453 Jamozed 2020-06-26 16:25:00
38
	
fe24453 Jamozed 2020-06-26 16:25:00
39
	for (; *--p == '/';);
baad6e1 Jamozed 2020-07-06 22:24:24
40
	if (p + 1 == av[opt.ind]) { fputs("/\n", stdout); return 0; }
fe24453 Jamozed 2020-06-26 16:25:00
41
	else { p[1] = 0; }
fe24453 Jamozed 2020-06-26 16:25:00
42
	
baad6e1 Jamozed 2020-07-06 22:24:24
43
	fputs(av[opt.ind], stdout); fputc('\n', stdout);
fe24453 Jamozed 2020-06-26 16:25:00
44
	return 0;
fe24453 Jamozed 2020-06-26 16:25:00
45
}
fe24453 Jamozed 2020-06-26 16:25:00
46
30cf081 Jamozed 2020-08-28 23:01:02
47
static void hlp(void) {
fe24453 Jamozed 2020-06-26 16:25:00
48
	puts("dirname - return the directory portion of a path\n");
fe24453 Jamozed 2020-06-26 16:25:00
49
	puts("usage: dirname string\n");
fe24453 Jamozed 2020-06-26 16:25:00
50
	puts("options:");
fe24453 Jamozed 2020-06-26 16:25:00
51
	puts("  --help     Display help information");
fe24453 Jamozed 2020-06-26 16:25:00
52
	puts("  --version  Display version information");
fe24453 Jamozed 2020-06-26 16:25:00
53
}
fe24453 Jamozed 2020-06-26 16:25:00
54
30cf081 Jamozed 2020-08-28 23:01:02
55
static void ver(void) {
baad6e1 Jamozed 2020-07-06 22:24:24
56
	puts("OMKOV coreutils dirname, version " VERSION);
fe24453 Jamozed 2020-06-26 16:25:00
57
	puts("Copyright (C) 2020, Jakob Wakeling");
e2140ec Jamozed 2022-03-06 15:27:45
58
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
fe24453 Jamozed 2020-06-26 16:25:00
59
}
60