601b288 |
Jamozed |
2020-07-06 23:40:13 |
0
|
// tee.c, version 1.0.3 |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
1
|
// OMKOV coreutils implementation of POSIX tee |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
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" |
601b288 |
Jamozed |
2020-07-06 23:40:13 |
7
|
|
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
8
|
#include <signal.h> |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
9
|
#include <stdbool.h> |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
10
|
#include <stdio.h> |
601b288 |
Jamozed |
2020-07-06 23:40:13 |
11
|
#include <stdlib.h> |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
12
|
|
601b288 |
Jamozed |
2020-07-06 23:40:13 |
13
|
#define VERSION "1.0.3" |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
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
|
|
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
21
|
static bool aflag; |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
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); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
25
|
|
601b288 |
Jamozed |
2020-07-06 23:40:13 |
26
|
int main(int ac, char *av[]) { A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
27
|
struct opt opt = OPTGET_INIT; opt.str = "ai"; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
28
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
29
|
case 'a': { aflag = true; break; } |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
30
|
case 'i': { signal(SIGINT, SIG_IGN); 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; } |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
33
|
default: { return 1; } |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
34
|
} |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
35
|
|
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
36
|
setvbuf(stdout, NULL, _IONBF, 0); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
37
|
bool warned = false; |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
38
|
|
d8671c3 |
Jamozed |
2020-07-09 22:12:02 |
39
|
FILE **files = (FILE **)malloc(sizeof (FILE) * (unsigned)(ac - opt.ind)); |
601b288 |
Jamozed |
2020-07-06 23:40:13 |
40
|
if (!files) { error(1, "%s", serr()); } |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
41
|
|
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
42
|
FILE **f = files; |
601b288 |
Jamozed |
2020-07-06 23:40:13 |
43
|
for (char **p = &av[opt.ind]; *p; ++p, ++f) { |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
44
|
*f = fopen(*p, aflag ? "w" : "a"); |
601b288 |
Jamozed |
2020-07-06 23:40:13 |
45
|
if (!*f) { warn("%s: %s", *p, serr()); --f; } |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
46
|
} *f = NULL; |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
47
|
|
09b6070 |
Jamozed |
2020-07-02 15:10:57 |
48
|
char stdbuf[BUFSIZ * 16]; |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
49
|
for (size_t c; (c = fread(stdbuf, 1, sizeof (stdbuf), stdin)) != 0;) { |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
50
|
for (f = files; *f; ++f) { fwrite(stdbuf, 1, c, *f); } |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
51
|
fwrite(stdbuf, 1, c, stdout); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
52
|
} |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
53
|
|
601b288 |
Jamozed |
2020-07-06 23:40:13 |
54
|
for (f = files; *f; ++f) { if (fclose(*f)) { warn("%s", serr()); }} |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
55
|
|
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
56
|
free(files); return warned; |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
57
|
} |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
58
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
59
|
static void hlp(void) { |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
60
|
puts("tee - duplicate standard input"); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
61
|
puts("usage: tee [-ai] [file...]\n"); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
62
|
puts("options:"); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
63
|
puts(" -a Append to files"); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
64
|
puts(" -i Ignore interrupt signals"); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
65
|
puts(" --help Display help information"); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
66
|
puts(" --version Display version information"); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
67
|
} |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
68
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
69
|
static void ver(void) { |
09b6070 |
Jamozed |
2020-07-02 15:10:57 |
70
|
puts("OMKOV coreutils tee, version " VERSION); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
71
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
72
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
8883cf3 |
Jamozed |
2020-06-26 17:27:18 |
73
|
} |
|
|
|
74
|
|