Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/cat Add an option to read with a different buffer size, ...



details:   https://anonhg.NetBSD.org/src/rev/10754d5a74c6
branches:  trunk
changeset: 782637:10754d5a74c6
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Nov 10 16:18:41 2012 +0000

description:
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.

diffstat:

 bin/cat/cat.1 |  10 ++++++++--
 bin/cat/cat.c |  37 +++++++++++++++++++++----------------
 2 files changed, 29 insertions(+), 18 deletions(-)

diffs (136 lines):

diff -r 471224eae766 -r 10754d5a74c6 bin/cat/cat.1
--- a/bin/cat/cat.1     Sat Nov 10 15:59:58 2012 +0000
+++ b/bin/cat/cat.1     Sat Nov 10 16:18:41 2012 +0000
@@ -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 @@
 .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
diff -r 471224eae766 -r 10754d5a74c6 bin/cat/cat.c
--- a/bin/cat/cat.c     Sat Nov 10 15:59:58 2012 +0000
+++ b/bin/cat/cat.c     Sat Nov 10 16:18:41 2012 +0000
@@ -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 @@
 #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 @@
 #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 @@
        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 @@
                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 @@
                raw_args(argv);
        if (fclose(stdout))
                err(EXIT_FAILURE, "stdout");
-       return (rval);
+       return rval;
 }
 
 void
@@ -286,7 +289,6 @@
 {
        static char *buf;
        static char fb_buf[BUFSIZ];
-       static size_t bsize;
 
        ssize_t nr, nw, off;
        int wfd;
@@ -295,13 +297,16 @@
        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;
+               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;
                }
        }



Home | Main Index | Thread Index | Old Index