aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2007-11-29 17:49:50 -0600
committerRob Landley <rob@landley.net>2007-11-29 17:49:50 -0600
commit7634b55f27ad483ec634ba9defac93872e3a329f (patch)
tree5d1a64072f77577d2d231d91872a1195ba0fd9c9 /toys
parentf7bb55bb14128a2b9367e2e089e9930c456f801f (diff)
downloadtoybox-7634b55f27ad483ec634ba9defac93872e3a329f.tar.gz
Add loopfiles() function, make catv use it.
Diffstat (limited to 'toys')
-rw-r--r--toys/catv.c76
1 files changed, 35 insertions, 41 deletions
diff --git a/toys/catv.c b/toys/catv.c
index 161ca514..74242010 100644
--- a/toys/catv.c
+++ b/toys/catv.c
@@ -10,51 +10,45 @@
#include "toys.h"
-int catv_main(void)
-{
- int retval = 0, fd;
- char **argv = toys.optargs;
-
- toys.optflags^=4;
+// Callback function for loopfiles()
- // Loop through files.
-
- do {
- // Read from stdin if there's nothing else to do.
-
- fd = 0;
- if (*argv && 0>(fd = xopen(*argv, O_RDONLY))) retval = EXIT_FAILURE;
- else for(;;) {
- int i, res;
-
- res = read(fd, toybuf, sizeof(toybuf));
- if (res < 0) retval = EXIT_FAILURE;
- if (res < 1) break;
- for (i=0; i<res; i++) {
- char c=toybuf[i];
-
- if (c > 126 && (toys.optflags & 4)) {
- if (c == 127) {
- printf("^?");
- continue;
- } else {
- printf("M-");
- c -= 128;
- }
+void do_catv(int fd, char *name)
+{
+ for(;;) {
+ int i, len;
+
+ len = read(fd, toybuf, sizeof(toybuf));
+ if (len < 0) toys.exitval = EXIT_FAILURE;
+ if (len < 1) break;
+ for (i=0; i<len; i++) {
+ char c=toybuf[i];
+
+ if (c > 126 && (toys.optflags & 4)) {
+ if (c == 127) {
+ printf("^?");
+ continue;
+ } else {
+ printf("M-");
+ c -= 128;
}
- if (c < 32) {
- if (c == 10) {
- if (toys.optflags & 1) putchar('$');
- } else if (toys.optflags & (c==9 ? 2 : 4)) {
- printf("^%c", c+'@');
- continue;
- }
+ }
+ if (c < 32) {
+ if (c == 10) {
+ if (toys.optflags & 1) putchar('$');
+ } else if (toys.optflags & (c==9 ? 2 : 4)) {
+ printf("^%c", c+'@');
+ continue;
}
- putchar(c);
}
+ putchar(c);
}
- if (CFG_TOYBOX_FREE && fd) close(fd);
- } while (*++argv);
+ }
+}
+
+int catv_main(void)
+{
+ toys.optflags^=4;
+ loopfiles(toys.optargs, do_catv);
- return retval;
+ return toys.exitval;
}