Add loopfiles() function, make catv use it.
authorRob Landley <rob@landley.net>
Thu, 29 Nov 2007 23:49:50 +0000 (17:49 -0600)
committerRob Landley <rob@landley.net>
Thu, 29 Nov 2007 23:49:50 +0000 (17:49 -0600)
lib/lib.c
lib/lib.h
toys/catv.c

index 5d01efc..6aaf7b0 100644 (file)
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -545,3 +545,26 @@ void xpidfile(char *name)
        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);
+}
index 67c6dac..963abb6 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -73,6 +73,7 @@ char *itoa(int n);
 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 {
index 161ca51..7424201 100644 (file)
 
 #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;
 }