1 /* vi: set sw=4 ts=4: */
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.
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"
23 int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24 int catv_main(int argc UNUSED_PARAM, char **argv)
26 int retval = EXIT_SUCCESS;
31 opts = getopt32(argv, "etv");
32 #define CATV_OPT_e (1<<0)
33 #define CATV_OPT_t (1<<1)
34 #define CATV_OPT_v (1<<2)
36 if (opts & (CATV_OPT_e | CATV_OPT_t))
38 if (opts & CATV_OPT_e)
39 flags |= VISIBLE_ENDLINE;
40 if (opts & CATV_OPT_t)
41 flags |= VISIBLE_SHOW_TABS;
43 /* Read from stdin if there's nothing else to do. */
47 fd = open_or_warn_stdin(*argv);
49 retval = EXIT_FAILURE;
55 #define read_buf bb_common_bufsiz1
56 res = read(fd, read_buf, COMMON_BUFSIZE);
58 retval = EXIT_FAILURE;
61 for (i = 0; i < res; i++) {
62 unsigned char c = read_buf[i];
63 if (opts & CATV_OPT_v) {
66 char buf[sizeof("M-^c")];
67 visible(c, buf, flags);
72 if (ENABLE_FEATURE_CLEAN_UP && fd)
76 fflush_stdout_and_exit(retval);