Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/catv.c
Parent Directory | Revision Log
Revision 816 -
(hide annotations)
(download)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 1567 byte(s)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 1567 byte(s)
-updated to busybox-1.13.4
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * cat -v implementation for busybox | ||
4 | * | ||
5 | * Copyright (C) 2006 Rob Landley <rob@landley.net> | ||
6 | * | ||
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | /* See "Cat -v considered harmful" at | ||
11 | * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */ | ||
12 | |||
13 | niro | 816 | #include "libbb.h" |
14 | niro | 532 | |
15 | niro | 816 | int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
16 | int catv_main(int argc UNUSED_PARAM, char **argv) | ||
17 | niro | 532 | { |
18 | niro | 816 | int retval = EXIT_SUCCESS; |
19 | int fd; | ||
20 | niro | 532 | unsigned flags; |
21 | |||
22 | niro | 816 | flags = getopt32(argv, "etv"); |
23 | niro | 532 | #define CATV_OPT_e (1<<0) |
24 | #define CATV_OPT_t (1<<1) | ||
25 | #define CATV_OPT_v (1<<2) | ||
26 | flags ^= CATV_OPT_v; | ||
27 | niro | 816 | argv += optind; |
28 | niro | 532 | |
29 | niro | 816 | /* Read from stdin if there's nothing else to do. */ |
30 | if (!argv[0]) | ||
31 | *--argv = (char*)"-"; | ||
32 | niro | 532 | do { |
33 | niro | 816 | fd = open_or_warn_stdin(*argv); |
34 | if (fd < 0) { | ||
35 | niro | 532 | retval = EXIT_FAILURE; |
36 | niro | 816 | continue; |
37 | } | ||
38 | for (;;) { | ||
39 | niro | 532 | int i, res; |
40 | |||
41 | niro | 816 | #define read_buf bb_common_bufsiz1 |
42 | res = read(fd, read_buf, COMMON_BUFSIZE); | ||
43 | niro | 532 | if (res < 0) |
44 | retval = EXIT_FAILURE; | ||
45 | if (res < 1) | ||
46 | break; | ||
47 | for (i = 0; i < res; i++) { | ||
48 | niro | 816 | unsigned char c = read_buf[i]; |
49 | niro | 532 | |
50 | if (c > 126 && (flags & CATV_OPT_v)) { | ||
51 | if (c == 127) { | ||
52 | printf("^?"); | ||
53 | continue; | ||
54 | } | ||
55 | niro | 816 | printf("M-"); |
56 | c -= 128; | ||
57 | niro | 532 | } |
58 | if (c < 32) { | ||
59 | if (c == 10) { | ||
60 | if (flags & CATV_OPT_e) | ||
61 | niro | 816 | bb_putchar('$'); |
62 | niro | 532 | } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) { |
63 | printf("^%c", c+'@'); | ||
64 | continue; | ||
65 | } | ||
66 | } | ||
67 | niro | 816 | bb_putchar(c); |
68 | niro | 532 | } |
69 | } | ||
70 | if (ENABLE_FEATURE_CLEAN_UP && fd) | ||
71 | close(fd); | ||
72 | } while (*++argv); | ||
73 | |||
74 | fflush_stdout_and_exit(retval); | ||
75 | } |