aboutsummaryrefslogtreecommitdiff
path: root/coreutils/catv.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/catv.c')
-rw-r--r--coreutils/catv.c44
1 files changed, 25 insertions, 19 deletions
diff --git a/coreutils/catv.c b/coreutils/catv.c
index e18203915..f8229c20e 100644
--- a/coreutils/catv.c
+++ b/coreutils/catv.c
@@ -14,49 +14,55 @@
14 14
15int catv_main(int argc, char **argv) 15int catv_main(int argc, char **argv)
16{ 16{
17 int retval = EXIT_SUCCESS, fd, flags; 17 int retval = EXIT_SUCCESS, fd;
18 unsigned long flags;
18 19
19 flags = bb_getopt_ulflags(argc, argv, "etv"); 20 flags = bb_getopt_ulflags(argc, argv, "etv");
20 flags ^= 4; 21#define CATV_OPT_e (1<<0)
21 22#define CATV_OPT_t (1<<1)
22 // Loop through files. 23#define CATV_OPT_v (1<<2)
24 flags ^= CATV_OPT_v;
23 25
24 argv += optind; 26 argv += optind;
25 do { 27 do {
26 // Read from stdin if there's nothing else to do. 28 /* Read from stdin if there's nothing else to do. */
27
28 fd = 0; 29 fd = 0;
29 if (*argv && 0>(fd = xopen(*argv, O_RDONLY))) retval = EXIT_FAILURE; 30 if (*argv && 0 > (fd = xopen(*argv, O_RDONLY)))
30 else for(;;) { 31 retval = EXIT_FAILURE;
32 else for (;;) {
31 int i, res; 33 int i, res;
32 34
33 res = read(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1)); 35 res = read(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1));
34 if (res < 0) retval = EXIT_FAILURE; 36 if (res < 0)
35 if (res <1) break; 37 retval = EXIT_FAILURE;
36 for (i=0; i<res; i++) { 38 if (res < 1)
37 char c=bb_common_bufsiz1[i]; 39 break;
40 for (i = 0; i < res; i++) {
41 char c = bb_common_bufsiz1[i];
38 42
39 if (c > 126 && (flags & 4)) { 43 if (c > 126 && (flags & CATV_OPT_v)) {
40 if (c == 127) { 44 if (c == 127) {
41 printf("^?"); 45 bb_printf("^?");
42 continue; 46 continue;
43 } else { 47 } else {
44 printf("M-"); 48 bb_printf("M-");
45 c -= 128; 49 c -= 128;
46 } 50 }
47 } 51 }
48 if (c < 32) { 52 if (c < 32) {
49 if (c == 10) { 53 if (c == 10) {
50 if (flags & 1) putchar('$'); 54 if (flags & CATV_OPT_e)
51 } else if (flags & (c==9 ? 2 : 4)) { 55 putchar('$');
52 printf("^%c", c+'@'); 56 } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
57 bb_printf("^%c", c+'@');
53 continue; 58 continue;
54 } 59 }
55 } 60 }
56 putchar(c); 61 putchar(c);
57 } 62 }
58 } 63 }
59 if (ENABLE_FEATURE_CLEAN_UP && fd) close(fd); 64 if (ENABLE_FEATURE_CLEAN_UP && fd)
65 close(fd);
60 } while (*++argv); 66 } while (*++argv);
61 67
62 return retval; 68 return retval;