ls: avoid forward declaration. No code changes
[platform/upstream/busybox.git] / coreutils / catv.c
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 source tree.
8  */
9
10 /* See "Cat -v considered harmful" at
11  * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */
12
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:     "\nOptions:"
18 //usage:     "\n        -e      End each line with $"
19 //usage:     "\n        -t      Show tabs as ^I"
20 //usage:     "\n        -v      Don't use ^x or M-x escapes"
21
22 #include "libbb.h"
23
24 int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
25 int catv_main(int argc UNUSED_PARAM, char **argv)
26 {
27         int retval = EXIT_SUCCESS;
28         int fd;
29         unsigned flags;
30
31         flags = getopt32(argv, "etv");
32 #define CATV_OPT_e (1<<0)
33 #define CATV_OPT_t (1<<1)
34 #define CATV_OPT_v (1<<2)
35         flags ^= CATV_OPT_v;
36         argv += optind;
37
38         /* Read from stdin if there's nothing else to do. */
39         if (!argv[0])
40                 *--argv = (char*)"-";
41         do {
42                 fd = open_or_warn_stdin(*argv);
43                 if (fd < 0) {
44                         retval = EXIT_FAILURE;
45                         continue;
46                 }
47                 for (;;) {
48                         int i, res;
49
50 #define read_buf bb_common_bufsiz1
51                         res = read(fd, read_buf, COMMON_BUFSIZE);
52                         if (res < 0)
53                                 retval = EXIT_FAILURE;
54                         if (res < 1)
55                                 break;
56                         for (i = 0; i < res; i++) {
57                                 unsigned char c = read_buf[i];
58
59                                 if (c > 126 && (flags & CATV_OPT_v)) {
60                                         if (c == 127) {
61                                                 printf("^?");
62                                                 continue;
63                                         }
64                                         printf("M-");
65                                         c -= 128;
66                                 }
67                                 if (c < 32) {
68                                         if (c == 10) {
69                                                 if (flags & CATV_OPT_e)
70                                                         bb_putchar('$');
71                                         } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
72                                                 printf("^%c", c+'@');
73                                                 continue;
74                                         }
75                                 }
76                                 bb_putchar(c);
77                         }
78                 }
79                 if (ENABLE_FEATURE_CLEAN_UP && fd)
80                         close(fd);
81         } while (*++argv);
82
83         fflush_stdout_and_exit(retval);
84 }