busybox: update to 1.23.2
[tomato.git] / release / src / router / busybox / coreutils / catv.c
blob6bb73ba63d5e8341113fafbddf5a7d0b8b13761b
1 /* vi: set sw=4 ts=4: */
2 /*
3 * cat -v implementation for busybox
5 * Copyright (C) 2006 Rob Landley <rob@landley.net>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
10 /* See "Cat -v considered harmful" at
11 * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */
13 //usage:#define catv_trivial_usage
14 //usage: "[-etv] [FILE]..."
15 //usage:#define catv_full_usage "\n\n"
16 //usage: "Display nonprinting characters as ^x or M-x\n"
17 //usage: "\n -e End each line with $"
18 //usage: "\n -t Show tabs as ^I"
19 //usage: "\n -v Don't use ^x or M-x escapes"
21 #include "libbb.h"
23 #define CATV_OPT_e (1<<0)
24 #define CATV_OPT_t (1<<1)
25 #define CATV_OPT_v (1<<2)
26 struct BUG_const_mismatch {
27 char BUG_const_mismatch[
28 CATV_OPT_e == VISIBLE_ENDLINE && CATV_OPT_t == VISIBLE_SHOW_TABS
29 ? 1 : -1
33 int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
34 int catv_main(int argc UNUSED_PARAM, char **argv)
36 int retval = EXIT_SUCCESS;
37 int fd;
38 unsigned opts;
39 opts = getopt32(argv, "etv");
40 argv += optind;
41 #if 0 /* These consts match, we can just pass "opts" to visible() */
42 if (opts & CATV_OPT_e)
43 flags |= VISIBLE_ENDLINE;
44 if (opts & CATV_OPT_t)
45 flags |= VISIBLE_SHOW_TABS;
46 #endif
48 /* Read from stdin if there's nothing else to do. */
49 if (!argv[0])
50 *--argv = (char*)"-";
51 do {
52 fd = open_or_warn_stdin(*argv);
53 if (fd < 0) {
54 retval = EXIT_FAILURE;
55 continue;
57 for (;;) {
58 int i, res;
60 #define read_buf bb_common_bufsiz1
61 res = read(fd, read_buf, COMMON_BUFSIZE);
62 if (res < 0)
63 retval = EXIT_FAILURE;
64 if (res <= 0)
65 break;
66 for (i = 0; i < res; i++) {
67 unsigned char c = read_buf[i];
68 if (opts & CATV_OPT_v) {
69 putchar(c);
70 } else {
71 char buf[sizeof("M-^c")];
72 visible(c, buf, opts);
73 fputs(buf, stdout);
77 if (ENABLE_FEATURE_CLEAN_UP && fd)
78 close(fd);
79 } while (*++argv);
81 fflush_stdout_and_exit(retval);