From owner-freebsd-bugs Wed Dec 6 2:10: 8 2000 From owner-freebsd-bugs@FreeBSD.ORG Wed Dec 6 02:10:01 2000 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21]) by hub.freebsd.org (Postfix) with ESMTP id 3C4B337B401 for ; Wed, 6 Dec 2000 02:10:01 -0800 (PST) Received: (from gnats@localhost) by freefall.freebsd.org (8.11.1/8.11.1) id eB6AA1g04011; Wed, 6 Dec 2000 02:10:01 -0800 (PST) (envelope-from gnats) Resent-Date: Wed, 6 Dec 2000 02:10:01 -0800 (PST) Resent-Message-Id: <200012061010.eB6AA1g04011@freefall.freebsd.org> Resent-From: gnats-admin@FreeBSD.org (GNATS Management) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: gnats-admin@FreeBSD.org, Chris Richards Received: from weeta.princeton.edu (richards.student.Princeton.EDU [140.180.155.233]) by hub.freebsd.org (Postfix) with ESMTP id EA25C37B401 for ; 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 Sender: richards@richards.student.princeton.edu Reply-To: Chris Richards To: FreeBSD-gnats-submit@freebsd.org X-Send-Pr-Version: 3.2 Subject: bin/23321: [PATCH] reduce redundant code in /bin/cat Resent-Sender: gnats@FreeBSD.org Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >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