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.
This commit is contained in:
christos 2012-11-10 16:18:41 +00:00
parent 8f831e5efb
commit 0002c266f0
2 changed files with 29 additions and 18 deletions

View File

@ -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

View File

@ -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/17 23:35:28 christos Exp $");
#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;
}
}