source: MondoRescue/branches/2.2.9/mindi-busybox/coreutils/catv.c@ 3320

Last change on this file since 3320 was 3320, checked in by Bruno Cornec, 9 years ago
  • Re-add (thanks git BTW) the 2.2.9 branch which had been destroyed in the move to 3.0
  • Property svn:eol-style set to native
File size: 1.5 KB
Line 
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#include "libbb.h"
14
15int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
16int catv_main(int argc UNUSED_PARAM, char **argv)
17{
18 int retval = EXIT_SUCCESS;
19 int fd;
20 unsigned flags;
21
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;
28
29 /* Read from stdin if there's nothing else to do. */
30 if (!argv[0])
31 *--argv = (char*)"-";
32 do {
33 fd = open_or_warn_stdin(*argv);
34 if (fd < 0) {
35 retval = EXIT_FAILURE;
36 continue;
37 }
38 for (;;) {
39 int i, res;
40
41#define read_buf bb_common_bufsiz1
42 res = read(fd, read_buf, COMMON_BUFSIZE);
43 if (res < 0)
44 retval = EXIT_FAILURE;
45 if (res < 1)
46 break;
47 for (i = 0; i < res; i++) {
48 unsigned char c = read_buf[i];
49
50 if (c > 126 && (flags & CATV_OPT_v)) {
51 if (c == 127) {
52 printf("^?");
53 continue;
54 }
55 printf("M-");
56 c -= 128;
57 }
58 if (c < 32) {
59 if (c == 10) {
60 if (flags & CATV_OPT_e)
61 bb_putchar('$');
62 } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
63 printf("^%c", c+'@');
64 continue;
65 }
66 }
67 bb_putchar(c);
68 }
69 }
70 if (ENABLE_FEATURE_CLEAN_UP && fd)
71 close(fd);
72 } while (*++argv);
73
74 fflush_stdout_and_exit(retval);
75}
Note: See TracBrowser for help on using the repository browser.