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#define FOR_cat
51#define FORCE_FLAGS
52#include "toys.h"
53
54static void do_cat(int fd, char *name)
55{
56 int i, len, size=(toys.optflags & FLAG_u) ? 1 : sizeof(toybuf);
57
58 for(;;) {
59 len = read(fd, toybuf, size);
60 if (len < 0) {
61 toys.exitval = EXIT_FAILURE;
62 perror_msg_raw(name);
63 }
64 if (len < 1) break;
65 if ((CFG_CAT_V || CFG_CATV) && (toys.optflags&~FLAG_u)) {
66 for (i=0; i<len; i++) {
67 char c=toybuf[i];
68
69 if (c > 126 && (toys.optflags & FLAG_v)) {
70 if (c > 127) {
71 printf("M-");
72 c -= 128;
73 }
74 if (c == 127) {
75 printf("^?");
76 continue;
77 }
78 }
79 if (c < 32) {
80 if (c == 10) {
81 if (toys.optflags & FLAG_e) xputc('$');
82 } else if (toys.optflags & (c==9 ? FLAG_t : FLAG_v)) {
83 printf("^%c", c+'@');
84 continue;
85 }
86 }
87 xputc(c);
88 }
89 } else xwrite(1, toybuf, len);
90 }
91}
92
93void cat_main(void)
94{
95 loopfiles(toys.optargs, do_cat);
96}
97
98void catv_main(void)
99{
100 toys.optflags ^= FLAG_v;
101 loopfiles(toys.optargs, do_cat);
102}
103