blob: e182039155e861b039c5153cfca3aba204aadb28 [file] [log] [blame]
Rob Landley8abbee42006-05-31 19:36:04 +00001/* 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
13#include "busybox.h"
Rob Landley8abbee42006-05-31 19:36:04 +000014
15int catv_main(int argc, char **argv)
16{
17 int retval = EXIT_SUCCESS, fd, flags;
18
19 flags = bb_getopt_ulflags(argc, argv, "etv");
20 flags ^= 4;
21
22 // Loop through files.
23
24 argv += optind;
25 do {
26 // Read from stdin if there's nothing else to do.
27
28 fd = 0;
Rob Landleyd921b2e2006-08-03 15:41:12 +000029 if (*argv && 0>(fd = xopen(*argv, O_RDONLY))) retval = EXIT_FAILURE;
Rob Landley8abbee42006-05-31 19:36:04 +000030 else for(;;) {
31 int i, res;
32
33 res = read(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1));
34 if (res < 0) retval = EXIT_FAILURE;
35 if (res <1) break;
36 for (i=0; i<res; i++) {
37 char c=bb_common_bufsiz1[i];
38
39 if (c > 126 && (flags & 4)) {
40 if (c == 127) {
41 printf("^?");
42 continue;
43 } else {
44 printf("M-");
45 c -= 128;
46 }
47 }
48 if (c < 32) {
49 if (c == 10) {
50 if (flags & 1) putchar('$');
51 } else if (flags & (c==9 ? 2 : 4)) {
52 printf("^%c", c+'@');
53 continue;
54 }
55 }
56 putchar(c);
57 }
58 }
59 if (ENABLE_FEATURE_CLEAN_UP && fd) close(fd);
60 } while (*++argv);
61
62 return retval;
63}