# HG changeset patch # User Rob Landley # Date 1196380190 21600 # Node ID 29e2051296fd13871e3f12a3625408087d0adf7b # Parent 2d0de0d21e80cd13454cc8a09cd58c702e5e439b Add loopfiles() function, make catv use it. diff -r 2d0de0d21e80 -r 29e2051296fd lib/lib.c --- a/lib/lib.c Tue Nov 27 01:44:27 2007 -0600 +++ b/lib/lib.c Thu Nov 29 17:49:50 2007 -0600 @@ -545,3 +545,26 @@ xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); close(fd); } + +// Iterate through an array of files, opening each one (read only) and +// calling a function on that filehandle and name. The special filename +// "-" means stdin. An empty argument list calls function() on stdin. +void loopfiles(char **argv, void (*function)(int fd, char *name)) +{ + int fd; + + // If no arguments, read from stdin. + if (!*argv) function(0, *argv); + else do { + // Filename "-" means read from stdin. + // Inability to open a file prints a warning, but doesn't exit. + + if (!strcmp(*argv,"-")) fd=0; + else if (0>(fd = open(*argv, O_RDONLY))) { + perror_msg("%s",*argv); + toys.exitval = 1; + } + function(fd, *argv); + close(fd); + } while (*++argv); +} diff -r 2d0de0d21e80 -r 29e2051296fd lib/lib.h --- a/lib/lib.h Tue Nov 27 01:44:27 2007 -0600 +++ b/lib/lib.h Thu Nov 29 17:49:50 2007 -0600 @@ -73,6 +73,7 @@ long atolx(char *c); off_t fdlength(int fd); char *xreadlink(char *name); +void loopfiles(char **argv, void (*function)(int fd, char *name)); // getmountlist.c struct mtab_list { diff -r 2d0de0d21e80 -r 29e2051296fd toys/catv.c --- a/toys/catv.c Tue Nov 27 01:44:27 2007 -0600 +++ b/toys/catv.c Thu Nov 29 17:49:50 2007 -0600 @@ -10,51 +10,45 @@ #include "toys.h" +// Callback function for loopfiles() + +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 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; + } + } + putchar(c); + } + } +} + int catv_main(void) { - int retval = 0, fd; - char **argv = toys.optargs; - toys.optflags^=4; - - // 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 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; - } - } - putchar(c); - } - } - if (CFG_TOYBOX_FREE && fd) close(fd); - } while (*++argv); - - return retval; + return toys.exitval; }