-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtac.c
More file actions
81 lines (69 loc) · 1 KB
/
Copy pathtac.c
File metadata and controls
81 lines (69 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct line {
char *str;
struct line *next;
};
struct line *
push(struct line *l, const char *str)
{
struct line *p;
p = malloc(sizeof(*p));
if (!p) {
err(1, "malloc");
}
p->next = l;
p->str = strdup(str);
if (!p->str) {
err(1, "strdup");
}
return p;
}
void
consume(struct line *l)
{
struct line *p;
while (l) {
fputs(l->str, stdout);
p = l;
l = l->next;
free(p->str);
free(p);
}
}
void
tac(FILE *fp, const char *s)
{
char buf[4096];
struct line *p = NULL;
while (fgets(buf, sizeof(buf), fp)) {
p = push(p, buf);
}
if (!feof(fp)) {
warn("error reading %s", s);
}
consume(p);
}
int
main(int argc, char **argv)
{
FILE *fp;
int i, retv = 0;
if (argc == 1) {
tac(stdin, "<stdin>");
} else {
for (i=1; i<argc; i++) {
fp = fopen(argv[i], "r");
if (!fp) {
warn("could not open %s", argv[i]);
retv = 1;
continue;
}
tac(fp, argv[i]);
fclose(fp);
}
}
return retv;
}