aboutsummaryrefslogtreecommitdiff
path: root/coreutils/catv.c
diff options
context:
space:
mode:
authorlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-05-31 19:36:04 +0000
committerlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-05-31 19:36:04 +0000
commita2853d585026e02d02eee78e7743767ff3813c18 (patch)
tree35f25489cf48f7a80e3545472a84d534ef9915a3 /coreutils/catv.c
parentc32928b3fb4be5215474d943aeb8f3ac8e1adc40 (diff)
downloadbusybox-w32-a2853d585026e02d02eee78e7743767ff3813c18.tar.gz
busybox-w32-a2853d585026e02d02eee78e7743767ff3813c18.tar.bz2
busybox-w32-a2853d585026e02d02eee78e7743767ff3813c18.zip
Add catv (separate applet instead of cat -v). Also cleanup cat.c comments,
the following of which (from cat.c) belongs in svn history instead of the source code: /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) * * This is a new implementation of 'cat' which aims to be SUSv3 compliant. * * Changes from the previous implementation include: * 1) Multiple '-' args are accepted as required by SUSv3. The previous * implementation would close stdin and segfault on a subsequent '-'. * 2) The '-u' options is required by SUSv3. Note that the specified * behavior for '-u' is done by default, so all we need do is accept * the option. */ git-svn-id: svn://busybox.net/trunk/busybox@15251 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils/catv.c')
-rw-r--r--coreutils/catv.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/coreutils/catv.c b/coreutils/catv.c
new file mode 100644
index 000000000..dd4aa44e3
--- /dev/null
+++ b/coreutils/catv.c
@@ -0,0 +1,65 @@
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 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}