5604fd4 |
Jamozed |
2021-02-22 16:40:57 |
0
|
// cat.c, version 1.0.5 |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
1
|
// OMKOV coreutils implementation of POSIX cat |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
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" |
934dc2e |
Jamozed |
2020-07-06 22:11:16 |
7
|
|
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
8
|
#include <stdbool.h> |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
9
|
#include <stdio.h> |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
10
|
|
5604fd4 |
Jamozed |
2021-02-22 16:40:57 |
11
|
#define VERSION "1.0.5" |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
12
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
13
|
static struct lop lops[] = { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
14
|
{ "help", ARG_NUL, 256 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
15
|
{ "version", ARG_NUL, 257 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
16
|
{ NULL, 0, 0 } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
17
|
}; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
18
|
|
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
19
|
static inline int cat(const char *file); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
20
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
21
|
static void hlp(void); |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
22
|
static void ver(void); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
23
|
|
934dc2e |
Jamozed |
2020-07-06 22:11:16 |
24
|
int main(int ac, char *av[]) { A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
25
|
struct opt opt = OPTGET_INIT; opt.str = "u"; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
26
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
27
|
case 'u': { break; } |
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; } |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
30
|
default: { return 1; } |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
31
|
} |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
32
|
|
5fb1515 |
Jamozed |
2020-09-16 19:26:59 |
33
|
// Disable buffering on stdout |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
34
|
setvbuf(stdout, NULL, _IONBF, 0); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
35
|
|
934dc2e |
Jamozed |
2020-07-06 22:11:16 |
36
|
if (opt.ind == ac) { cat("-"); } |
934dc2e |
Jamozed |
2020-07-06 22:11:16 |
37
|
else for (char **p = &av[opt.ind]; *p; ++p) if (cat(*p)) { |
5604fd4 |
Jamozed |
2021-02-22 16:40:57 |
38
|
warn("%s: %s", *p, serr()); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
39
|
} |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
40
|
|
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
41
|
return warned; |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
42
|
} |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
43
|
|
5fb1515 |
Jamozed |
2020-09-16 19:26:59 |
44
|
/* |
5fb1515 |
Jamozed |
2020-09-16 19:26:59 |
45
|
Write a file to stdout using a fixed size buffer. |
5fb1515 |
Jamozed |
2020-09-16 19:26:59 |
46
|
If the file path given is "-", then use stdin. |
5fb1515 |
Jamozed |
2020-09-16 19:26:59 |
47
|
*/ |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
48
|
static inline int cat(const char *file) { |
5fb1515 |
Jamozed |
2020-09-16 19:26:59 |
49
|
char buf[BUFSIZ * 16]; FILE *fi; |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
50
|
|
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
51
|
if (file[0] == '-' && file[1] == 0) { fi = stdin; } |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
52
|
else if (!(fi = fopen(file, "r"))) { return 1; } |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
53
|
|
5604fd4 |
Jamozed |
2021-02-22 16:40:57 |
54
|
for (register size_t c; (c = fread(buf, 1, sizeof (buf), fi));) { |
5604fd4 |
Jamozed |
2021-02-22 16:40:57 |
55
|
if (fwrite(buf, 1, c, stdout) != c) { fclose(fi); return 1; } |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
56
|
} |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
57
|
|
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
58
|
if (fi != stdin) { fclose(fi); } return 0; |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
59
|
} |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
60
|
|
5fb1515 |
Jamozed |
2020-09-16 19:26:59 |
61
|
/* Print help information */ |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
62
|
static void hlp(void) { |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
63
|
puts("cat - concatenate and print files\n"); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
64
|
puts("usage: cat [-u] [file...]\n"); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
65
|
puts("options:"); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
66
|
puts(" -u Disable output buffering (ignored)"); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
67
|
puts(" --help Display help information"); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
68
|
puts(" --version Display version information"); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
69
|
} |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
70
|
|
5fb1515 |
Jamozed |
2020-09-16 19:26:59 |
71
|
/* Print version information */ |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
72
|
static void ver(void) { |
3e21bfd |
Jamozed |
2020-07-02 14:46:42 |
73
|
puts("OMKOV coreutils cat, version " VERSION); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
74
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
75
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
34337b4 |
Jamozed |
2020-06-26 14:17:26 |
76
|
} |
|
|
|
77
|
|