Rev | Line | |
---|
[821] | 1 | /* 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 |
|
---|
[1765] | 13 | #include "libbb.h"
|
---|
[821] | 14 |
|
---|
[1765] | 15 | int catv_main(int argc, char **argv);
|
---|
[821] | 16 | int catv_main(int argc, char **argv)
|
---|
| 17 | {
|
---|
[1765] | 18 | int retval = EXIT_SUCCESS;
|
---|
| 19 | int fd;
|
---|
| 20 | unsigned flags;
|
---|
[821] | 21 |
|
---|
[1765] | 22 | flags = getopt32(argv, "etv");
|
---|
| 23 | #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 | argv += optind;
|
---|
[821] | 28 |
|
---|
[1765] | 29 | /* Read from stdin if there's nothing else to do. */
|
---|
| 30 | fd = 0;
|
---|
| 31 | if (!argv[0]) {
|
---|
| 32 | argv--;
|
---|
| 33 | goto jump_in;
|
---|
| 34 | }
|
---|
[821] | 35 | do {
|
---|
[1765] | 36 | fd = open_or_warn(*argv, O_RDONLY);
|
---|
| 37 | if (fd < 0) {
|
---|
| 38 | retval = EXIT_FAILURE;
|
---|
| 39 | continue;
|
---|
| 40 | }
|
---|
| 41 | jump_in:
|
---|
| 42 | for (;;) {
|
---|
[821] | 43 | int i, res;
|
---|
| 44 |
|
---|
[1765] | 45 | #define read_buf bb_common_bufsiz1
|
---|
| 46 | res = read(fd, read_buf, COMMON_BUFSIZE);
|
---|
| 47 | if (res < 0)
|
---|
| 48 | retval = EXIT_FAILURE;
|
---|
| 49 | if (res < 1)
|
---|
| 50 | break;
|
---|
| 51 | for (i = 0; i < res; i++) {
|
---|
| 52 | unsigned char c = read_buf[i];
|
---|
[821] | 53 |
|
---|
[1765] | 54 | if (c > 126 && (flags & CATV_OPT_v)) {
|
---|
[821] | 55 | if (c == 127) {
|
---|
| 56 | printf("^?");
|
---|
| 57 | continue;
|
---|
| 58 | }
|
---|
[1765] | 59 | printf("M-");
|
---|
| 60 | c -= 128;
|
---|
[821] | 61 | }
|
---|
| 62 | if (c < 32) {
|
---|
| 63 | if (c == 10) {
|
---|
[1765] | 64 | if (flags & CATV_OPT_e)
|
---|
| 65 | putchar('$');
|
---|
| 66 | } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
|
---|
[821] | 67 | printf("^%c", c+'@');
|
---|
| 68 | continue;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | putchar(c);
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
[1765] | 74 | if (ENABLE_FEATURE_CLEAN_UP && fd)
|
---|
| 75 | close(fd);
|
---|
[821] | 76 | } while (*++argv);
|
---|
| 77 |
|
---|
[1765] | 78 | fflush_stdout_and_exit(retval);
|
---|
[821] | 79 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.