Support for -c, byte count.
This commit is contained in:
parent
aa9278ffa9
commit
ef0e2ea4c7
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=24665
@ -40,11 +40,14 @@
|
|||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm head
|
.Nm head
|
||||||
.Op Fl n Ar count
|
.Op Fl n Ar count
|
||||||
|
.Op Fl c Ar bytes
|
||||||
.Op Ar file ...
|
.Op Ar file ...
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
This filter displays the first
|
This filter displays the first
|
||||||
.Ar count
|
.Ar count
|
||||||
lines of each of the specified files, or of the standard input if no
|
lines or
|
||||||
|
.Ar bytes
|
||||||
|
of each of the specified files, or of the standard input if no
|
||||||
files are specified.
|
files are specified.
|
||||||
If
|
If
|
||||||
.Ar count
|
.Ar count
|
||||||
|
@ -58,6 +58,7 @@ static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
|
|||||||
|
|
||||||
void err __P((int, const char *, ...));
|
void err __P((int, const char *, ...));
|
||||||
void head __P((FILE *, int));
|
void head __P((FILE *, int));
|
||||||
|
void head_bytes __P((FILE *, int));
|
||||||
void obsolete __P((char *[]));
|
void obsolete __P((char *[]));
|
||||||
void usage __P((void));
|
void usage __P((void));
|
||||||
|
|
||||||
@ -70,13 +71,17 @@ main(argc, argv)
|
|||||||
{
|
{
|
||||||
register int ch;
|
register int ch;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
int first, linecnt;
|
int first, linecnt = -1, bytecnt = -1;
|
||||||
char *ep;
|
char *ep;
|
||||||
|
|
||||||
obsolete(argv);
|
obsolete(argv);
|
||||||
linecnt = 10;
|
while ((ch = getopt(argc, argv, "n:c:")) != -1)
|
||||||
while ((ch = getopt(argc, argv, "n:")) != -1)
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
|
case 'c':
|
||||||
|
bytecnt = strtol(optarg, &ep, 10);
|
||||||
|
if (*ep || bytecnt <= 0)
|
||||||
|
err(1, "illegal byte count -- %s", optarg);
|
||||||
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
linecnt = strtol(optarg, &ep, 10);
|
linecnt = strtol(optarg, &ep, 10);
|
||||||
if (*ep || linecnt <= 0)
|
if (*ep || linecnt <= 0)
|
||||||
@ -89,7 +94,11 @@ main(argc, argv)
|
|||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (*argv)
|
if (linecnt != -1 && bytecnt != -1)
|
||||||
|
err(1, "can't combine line and byte counts");
|
||||||
|
if (linecnt == -1 )
|
||||||
|
linecnt = 10;
|
||||||
|
if (*argv) {
|
||||||
for (first = 1; *argv; ++argv) {
|
for (first = 1; *argv; ++argv) {
|
||||||
if ((fp = fopen(*argv, "r")) == NULL) {
|
if ((fp = fopen(*argv, "r")) == NULL) {
|
||||||
err(0, "%s: %s", *argv, strerror(errno));
|
err(0, "%s: %s", *argv, strerror(errno));
|
||||||
@ -100,11 +109,18 @@ main(argc, argv)
|
|||||||
first ? "" : "\n", *argv);
|
first ? "" : "\n", *argv);
|
||||||
first = 0;
|
first = 0;
|
||||||
}
|
}
|
||||||
head(fp, linecnt);
|
if (bytecnt == -1)
|
||||||
|
head(fp, linecnt);
|
||||||
|
else
|
||||||
|
head_bytes(fp, bytecnt);
|
||||||
(void)fclose(fp);
|
(void)fclose(fp);
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
|
else if (bytecnt == -1)
|
||||||
head(stdin, linecnt);
|
head(stdin, linecnt);
|
||||||
|
else
|
||||||
|
head_bytes(stdin, bytecnt);
|
||||||
|
|
||||||
exit(eval);
|
exit(eval);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,13 +139,35 @@ head(fp, cnt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
head_bytes(fp, cnt)
|
||||||
|
FILE *fp;
|
||||||
|
register int cnt;
|
||||||
|
{
|
||||||
|
char buf[4096];
|
||||||
|
register int readlen;
|
||||||
|
|
||||||
|
while (cnt) {
|
||||||
|
if (cnt < sizeof(buf))
|
||||||
|
readlen = cnt;
|
||||||
|
else
|
||||||
|
readlen = sizeof(buf);
|
||||||
|
readlen = fread(buf, sizeof(char), readlen, fp);
|
||||||
|
if (readlen == EOF)
|
||||||
|
break;
|
||||||
|
if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
|
||||||
|
err(1, "stdout: %s", strerror(errno));
|
||||||
|
cnt -= readlen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
obsolete(argv)
|
obsolete(argv)
|
||||||
char *argv[];
|
char *argv[];
|
||||||
{
|
{
|
||||||
char *ap;
|
char *ap;
|
||||||
|
|
||||||
while (ap = *++argv) {
|
while ((ap = *++argv)) {
|
||||||
/* Return if "--" or not "-[0-9]*". */
|
/* Return if "--" or not "-[0-9]*". */
|
||||||
if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
|
if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user