annotate toys/catv.c @ 90:7c77c6ec17ee

Add "make defconfig". Modify global options to start with CONFIG_TOYBOX_.
author Rob Landley <rob@landley.net>
date Wed, 31 Jan 2007 14:37:01 -0500
parents 69efffcacd70
children 6c7836dbc16e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4: */
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 /*
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * cat -v implementation for toybox
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright (C) 2006 Rob Landley <rob@landley.net>
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 */
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 /* See "Cat -v considered harmful" at
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 #include "toys.h"
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
12
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 int catv_main(void)
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 int retval = 0, fd;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 char **argv = toys.optargs;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 toys.optflags^=4;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 // Loop through files.
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 do {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 // Read from stdin if there's nothing else to do.
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 fd = 0;
49
bb4c38853a7d xopen() wants 2 arguments unless you're creating a file, in which case you
Rob Landley <rob@landley.net>
parents: 35
diff changeset
26 if (*argv && 0>(fd = xopen(*argv, O_RDONLY))) retval = EXIT_FAILURE;
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 else for(;;) {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 int i, res;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
29
63
69efffcacd70 Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
Rob Landley <rob@landley.net>
parents: 49
diff changeset
30 res = read(fd, toybuf, sizeof(toybuf));
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 if (res < 0) retval = EXIT_FAILURE;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 if (res < 1) break;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 for (i=0; i<res; i++) {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 char c=toybuf[i];
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 if (c > 126 && (toys.optflags & 4)) {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 if (c == 127) {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 printf("^?");
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 continue;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 } else {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 printf("M-");
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 c -= 128;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 }
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 }
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 if (c < 32) {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 if (c == 10) {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 if (toys.optflags & 1) putchar('$');
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 } else if (toys.optflags & (c==9 ? 2 : 4)) {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 printf("^%c", c+'@');
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 continue;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 }
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 }
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 putchar(c);
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 }
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 }
90
7c77c6ec17ee Add "make defconfig". Modify global options to start with CONFIG_TOYBOX_.
Rob Landley <rob@landley.net>
parents: 63
diff changeset
56 if (CFG_TOYBOX_FREE && fd) close(fd);
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 } while (*++argv);
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
58
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 return retval;
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 }