Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 6 Dec 2000 05:02:35 -0500 (EST)
From:      Chris Richards <richards+bsd@CS.Princeton.EDU>
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/23321: [PATCH] reduce redundant code in /bin/cat
Message-ID:  <200012061002.eB6A2Zf50552@weeta.princeton.edu>
Resent-Message-ID: <200012061010.eB6AA1g04011@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         23321
>Category:       bin
>Synopsis:       [PATCH] reduce redundant code in /bin/cat
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Dec 06 02:10:00 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     Chris Richards
>Release:        FreeBSD 4.2-STABLE i386
>Organization:
>Environment:

	

>Description:

	The code for iterating over argv and emitting the
corresponding files should not depend on whether cat is in cooked or
raw mode.  However the current code uses, depending on the mode, one
of two nearly identical functions for that purpose.

>How-To-Repeat:

	

>Fix:

	Summary: Use the raw-mode function for iterating over argv as
the basis for a new, generic iterator, which takes as a parameter a
pointer to either the cooked- or raw-mode function for emitting a
file.  The cooked-mode file emitter is thus altered to accept an fd
and to obtain the necessary buffered stream using fdopen().

	A patch follows.

Index: cat.c
===================================================================
RCS file: /home/ncvs/src/bin/cat/cat.c,v
retrieving revision 1.15
diff -u -r1.15 cat.c
--- cat.c	2000/04/14 21:01:35	1.15
+++ cat.c	2000/12/06 09:54:33
@@ -63,10 +63,9 @@
 int rval;
 const char *filename;
 
-void cook_args __P((char *argv[]));
-void cook_buf __P((FILE *));
+void cat_args __P((char *argv[], void (*cat)(int)));
+void cook_buf __P((int));
 int main __P((int argc, char *argv[]));
-void raw_args __P((char *argv[]));
 void raw_cat __P((int));
 
 int
@@ -109,46 +108,26 @@
 	argv += optind;
 
 	if (bflag || eflag || nflag || sflag || tflag || vflag)
-		cook_args(argv);
+		cat_args(argv, cook_buf);
 	else
-		raw_args(argv);
+		cat_args(argv, raw_cat);
 	if (fclose(stdout))
 		err(1, "stdout");
 	exit(rval);
 }
 
 void
-cook_args(argv)
-	char **argv;
+cook_buf(fd)
+	register int fd;
 {
 	register FILE *fp;
-
-	fp = stdin;
-	filename = "stdin";
-	do {
-		if (*argv) {
-			if (!strcmp(*argv, "-"))
-				fp = stdin;
-			else if ((fp = fopen(*argv, "r")) == NULL) {
-				warn("%s", *argv);
-				rval = 1;
-				++argv;
-				continue;
-			}
-			filename = *argv++;
-		}
-		cook_buf(fp);
-		if (fp != stdin)
-			(void)fclose(fp);
-	} while (*argv);
-}
-
-void
-cook_buf(fp)
-	register FILE *fp;
-{
 	register int ch, gobble, line, prev;
 
+	if ((fp = fdopen(fd, "r")) == NULL) {
+		warn("%s", filename);
+		rval = 1;
+		return;
+	}
 	line = gobble = 0;
 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
 		if (prev == '\n') {
@@ -205,11 +184,14 @@
 	}
 	if (ferror(stdout))
 		err(1, "stdout");
+	if (fd != fileno(stdin))
+		(void)fclose(fp);
 }
 
 void
-raw_args(argv)
+cat_args(argv, cat)
 	char **argv;
+	void (*cat)(int);
 {
 	register int fd;
 
@@ -227,7 +209,7 @@
 			}
 			filename = *argv++;
 		}
-		raw_cat(fd);
+		cat(fd);
 		if (fd != fileno(stdin))
 			(void)close(fd);
 	} while (*argv);

>Release-Note:
>Audit-Trail:
>Unformatted:


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200012061002.eB6A2Zf50552>