Module Name:    src
Committed By:   christos
Date:           Sat Nov 10 16:18:41 UTC 2012

Modified Files:
        src/bin/cat: cat.1 cat.c

Log Message:
Add an option to read with a different buffer size, and document the
buffer size we use. This allows us to cat -B 10000000 /proc/<pid>/maps
for example which cannot handle seeking.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/bin/cat/cat.1
cvs rdiff -u -r1.48 -r1.49 src/bin/cat/cat.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/cat/cat.1
diff -u src/bin/cat/cat.1:1.34 src/bin/cat/cat.1:1.35
--- src/bin/cat/cat.1:1.34	Thu Aug  9 03:26:28 2012
+++ src/bin/cat/cat.1	Sat Nov 10 11:18:41 2012
@@ -1,4 +1,4 @@
-.\"	$NetBSD: cat.1,v 1.34 2012/08/09 07:26:28 dholland Exp $
+.\"	$NetBSD: cat.1,v 1.35 2012/11/10 16:18:41 christos Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"     @(#)cat.1	8.3 (Berkeley) 5/2/95
 .\"
-.Dd April 6, 2012
+.Dd November 10, 2012
 .Dt CAT 1
 .Os
 .Sh NAME
@@ -40,6 +40,7 @@
 .Nd concatenate and print files
 .Sh SYNOPSIS
 .Nm
+.Op Fl B Ar bsize
 .Op Fl beflnstuv
 .Op Fl
 .Op Ar
@@ -65,6 +66,11 @@ is just a verbose synonym for
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
+.It Fl B Ar bsize
+Read with a buffer size of
+.Ar bsize
+bytes, instead of the default buffer size which is the blocksize of the
+output file.
 .It Fl b
 Implies the
 .Fl n

Index: src/bin/cat/cat.c
diff -u src/bin/cat/cat.c:1.48 src/bin/cat/cat.c:1.49
--- src/bin/cat/cat.c:1.48	Sat Mar 17 19:35:28 2012
+++ src/bin/cat/cat.c	Sat Nov 10 11:18:41 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: cat.c,v 1.48 2012/03/17 23:35:28 christos Exp $	*/
+/* $NetBSD: cat.c,v 1.49 2012/11/10 16:18:41 christos Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -44,7 +44,7 @@ __COPYRIGHT(
 #if 0
 static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
 #else
-__RCSID("$NetBSD: cat.c,v 1.48 2012/03/17 23:35:28 christos Exp $");
+__RCSID("$NetBSD: cat.c,v 1.49 2012/11/10 16:18:41 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -61,11 +61,11 @@ __RCSID("$NetBSD: cat.c,v 1.48 2012/03/1
 #include <string.h>
 #include <unistd.h>
 
-int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
-int rval;
-const char *filename;
+static int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
+static size_t bsize;
+static int rval;
+static const char *filename;
 
-int main(int, char *[]);
 void cook_args(char *argv[]);
 void cook_buf(FILE *);
 void raw_args(char *argv[]);
@@ -80,8 +80,11 @@ main(int argc, char *argv[])
 	setprogname(argv[0]);
 	(void)setlocale(LC_ALL, "");
 
-	while ((ch = getopt(argc, argv, "beflnstuv")) != -1)
+	while ((ch = getopt(argc, argv, "B:beflnstuv")) != -1)
 		switch (ch) {
+		case 'B':
+			bsize = (size_t)strtol(optarg, NULL, 0);
+			break;
 		case 'b':
 			bflag = nflag = 1;	/* -b implies -n */
 			break;
@@ -112,9 +115,9 @@ main(int argc, char *argv[])
 		default:
 		case '?':
 			(void)fprintf(stderr,
-			    "usage: cat [-beflnstuv] [-] [file ...]\n");
-			exit(EXIT_FAILURE);
-			/* NOTREACHED */
+			    "Usage: %s [-B <bsize>] [-beflnstuv] [-] "
+			    "[file ...]\n", getprogname());
+			return EXIT_FAILURE;
 		}
 	argv += optind;
 
@@ -133,7 +136,7 @@ main(int argc, char *argv[])
 		raw_args(argv);
 	if (fclose(stdout))
 		err(EXIT_FAILURE, "stdout");
-	return (rval);
+	return rval;
 }
 
 void
@@ -286,7 +289,6 @@ raw_cat(int rfd)
 {
 	static char *buf;
 	static char fb_buf[BUFSIZ];
-	static size_t bsize;
 
 	ssize_t nr, nw, off;
 	int wfd;
@@ -295,13 +297,16 @@ raw_cat(int rfd)
 	if (buf == NULL) {
 		struct stat sbuf;
 
-		if (fstat(wfd, &sbuf) == 0 && sbuf.st_blksize > 0 &&
-		    (size_t)sbuf.st_blksize > sizeof(fb_buf)) {
-			bsize = sbuf.st_blksize;
-			buf = malloc(bsize);
+		if (bsize == 0) {
+			if (fstat(wfd, &sbuf) == 0 && sbuf.st_blksize > 0 &&
+			    (size_t)sbuf.st_blksize > sizeof(fb_buf))
+				bsize = sbuf.st_blksize;
 		}
+		if (bsize != 0)
+			buf = malloc(bsize);
 		if (buf == NULL) {
 			bsize = sizeof(fb_buf);
+			warnx("malloc, using %zu buffer", bsize);
 			buf = fb_buf;
 		}
 	}

Reply via email to