From richards@weeta.princeton.edu  Wed Dec  6 02:02:36 2000
Return-Path: <richards@weeta.princeton.edu>
Received: from weeta.princeton.edu (richards.student.Princeton.EDU [140.180.155.233])
	by hub.freebsd.org (Postfix) with ESMTP id EA25C37B401
	for <FreeBSD-gnats-submit@freebsd.org>; Wed,  6 Dec 2000 02:02:35 -0800 (PST)
Received: (from richards@localhost)
	by weeta.princeton.edu (8.11.1/8.11.1) id eB6A2Zf50552;
	Wed, 6 Dec 2000 05:02:35 -0500 (EST)
	(envelope-from richards)
Message-Id: <200012061002.eB6A2Zf50552@weeta.princeton.edu>
Date: Wed, 6 Dec 2000 05:02:35 -0500 (EST)
From: Chris Richards <richards+bsd@CS.Princeton.EDU>
Sender: richards@weeta.princeton.edu
Reply-To: Chris Richards <richards+bsd@CS.Princeton.EDU>
To: FreeBSD-gnats-submit@freebsd.org
Subject: [PATCH] reduce redundant code in /bin/cat
X-Send-Pr-Version: 3.2

>Number:         23321
>Category:       bin
>Synopsis:       [PATCH] reduce redundant code in /bin/cat
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Dec 06 02:10:00 PST 2000
>Closed-Date:    Sun Nov 18 07:35:52 PST 2001
>Last-Modified:  Sun Nov 18 07:43:14 PST 2001
>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:
State-Changed-From-To: open->closed 
State-Changed-By: iedowse 
State-Changed-When: Sun Nov 18 07:35:52 PST 2001 
State-Changed-Why:  

A similar change has already been made in revision 1.17 of cat.c. 
(it also has the benefit that we now have a function called cook_cat 
in the source tree :-) 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=23321 
>Unformatted:
