changeset 185:29e2051296fd

Add loopfiles() function, make catv use it.
author Rob Landley <rob@landley.net>
date Thu, 29 Nov 2007 17:49:50 -0600
parents 2d0de0d21e80
children 25447caf1b4b
files lib/lib.c lib/lib.h toys/catv.c
diffstat 3 files changed, 61 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- 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);
+}
--- 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 {
--- 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<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;
+				}
+			}
+			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<res; i++) {
-				char c=toybuf[i];
+	loopfiles(toys.optargs, do_catv);
 
-				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;
-					}
-				}
-				putchar(c);
-			}
-		}
-		if (CFG_TOYBOX_FREE && fd) close(fd);
-	} while (*++argv);
-
-	return retval;
+	return toys.exitval;
 }