35
|
1 /* vi: set sw=4 ts=4: */
|
|
2 /*
|
|
3 * cat -v implementation for toybox
|
|
4 *
|
|
5 * Copyright (C) 2006 Rob Landley <rob@landley.net>
|
|
6 */
|
|
7
|
|
8 /* See "Cat -v considered harmful" at
|
|
9 * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */
|
|
10
|
|
11 #include "toys.h"
|
|
12
|
|
13 int catv_main(void)
|
|
14 {
|
|
15 int retval = 0, fd;
|
|
16 char **argv = toys.optargs;
|
|
17
|
|
18 toys.optflags^=4;
|
|
19
|
|
20 // Loop through files.
|
|
21
|
|
22 do {
|
|
23 // Read from stdin if there's nothing else to do.
|
|
24
|
|
25 fd = 0;
|
|
26 if (*argv && 0>(fd = xopen(*argv, O_RDONLY, 0))) retval = EXIT_FAILURE;
|
|
27 else for(;;) {
|
|
28 int i, res;
|
|
29
|
|
30 res = reread(fd, toybuf, sizeof(toybuf));
|
|
31 if (res < 0) retval = EXIT_FAILURE;
|
|
32 if (res < 1) break;
|
|
33 for (i=0; i<res; i++) {
|
|
34 char c=toybuf[i];
|
|
35
|
|
36 if (c > 126 && (toys.optflags & 4)) {
|
|
37 if (c == 127) {
|
|
38 printf("^?");
|
|
39 continue;
|
|
40 } else {
|
|
41 printf("M-");
|
|
42 c -= 128;
|
|
43 }
|
|
44 }
|
|
45 if (c < 32) {
|
|
46 if (c == 10) {
|
|
47 if (toys.optflags & 1) putchar('$');
|
|
48 } else if (toys.optflags & (c==9 ? 2 : 4)) {
|
|
49 printf("^%c", c+'@');
|
|
50 continue;
|
|
51 }
|
|
52 }
|
|
53 putchar(c);
|
|
54 }
|
|
55 }
|
|
56 if (CFG_TOYS_FREE && fd) close(fd);
|
|
57 } while (*++argv);
|
|
58
|
|
59 return retval;
|
|
60 }
|