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