Index: src/usr.bin/head/head.1 =================================================================== RCS file: /home/ncvs/src/usr.bin/head/head.1,v retrieving revision 1.12 diff -u -r1.12 head.1 --- src/usr.bin/head/head.1 10 Jun 2002 22:59:51 -0000 1.12 +++ src/usr.bin/head/head.1 9 Dec 2003 09:59:56 -0000 @@ -41,6 +41,7 @@ .Sh SYNOPSIS .Nm .Op Fl n Ar count | Fl c Ar bytes +.Op Fl s Ar skipcount .Op Ar .Sh DESCRIPTION This filter displays the first @@ -59,6 +60,14 @@ where .Dq XXX is the name of the file. +.Pp +If +.Ar skipcount +is specified, the +.Nm +utility skips the specified number of lines or bytes before displaying +the requested +.Ar count . .Sh DIAGNOSTICS .Ex -std .Sh SEE ALSO Index: src/usr.bin/head/head.c =================================================================== RCS file: /home/ncvs/src/usr.bin/head/head.c,v retrieving revision 1.18 diff -u -r1.18 head.c --- src/usr.bin/head/head.c 23 Jul 2002 14:39:20 -0000 1.18 +++ src/usr.bin/head/head.c 9 Dec 2003 10:04:46 -0000 @@ -60,8 +60,8 @@ * Bill Joy UCB August 24, 1977 */ -static void head(FILE *, int); -static void head_bytes(FILE *, size_t); +static void head(FILE *, int, int); +static void head_bytes(FILE *, size_t, int); static void obsolete(char *[]); static void usage(void); @@ -70,11 +70,11 @@ { int ch; FILE *fp; - int first, linecnt = -1, bytecnt = -1, eval = 0; + int first, linecnt = -1, bytecnt = -1, eval = 0, skipcnt = 0; char *ep; obsolete(argv); - while ((ch = getopt(argc, argv, "n:c:")) != -1) + while ((ch = getopt(argc, argv, "n:c:s:")) != -1) switch(ch) { case 'c': bytecnt = strtol(optarg, &ep, 10); @@ -86,6 +86,11 @@ if (*ep || linecnt <= 0) errx(1, "illegal line count -- %s", optarg); break; + case 's': + skipcnt = strtol(optarg, &ep, 10); + if (*ep || skipcnt <= 0) + errx(1, "illegal skip count -- %s", optarg); + break; case '?': default: usage(); @@ -109,36 +114,44 @@ first ? "" : "\n", *argv); first = 0; } - if (bytecnt == -1) - head(fp, linecnt); - else - head_bytes(fp, bytecnt); + if (bytecnt == -1) { + head(fp, skipcnt, 0); + head(fp, linecnt, 1); + } else { + head_bytes(fp, skipcnt, 0); + head_bytes(fp, bytecnt, 1); + } (void)fclose(fp); } - } else if (bytecnt == -1) - head(stdin, linecnt); - else - head_bytes(stdin, bytecnt); + } else if (bytecnt == -1) { + head(stdin, skipcnt, 0); + head(stdin, linecnt, 1); + } else { + head_bytes(stdin, skipcnt, 0); + head_bytes(stdin, bytecnt, 1); + } exit(eval); } static void -head(FILE *fp, int cnt) +head(FILE *fp, int cnt, int out) { char *cp; size_t error, readlen; while (cnt && (cp = fgetln(fp, &readlen)) != NULL) { - error = fwrite(cp, sizeof(char), readlen, stdout); - if (error != readlen) - err(1, "stdout"); + if (out) { + error = fwrite(cp, sizeof(char), readlen, stdout); + if (error != readlen) + err(1, "stdout"); + } cnt--; } } static void -head_bytes(FILE *fp, size_t cnt) +head_bytes(FILE *fp, size_t cnt, int out) { char buf[4096]; size_t readlen; @@ -151,8 +164,9 @@ readlen = fread(buf, sizeof(char), readlen, fp); if (readlen == 0) break; - if (fwrite(buf, sizeof(char), readlen, stdout) != readlen) - err(1, "stdout"); + if (out) + if (fwrite(buf, sizeof(char), readlen, stdout) != readlen) + err(1, "stdout"); cnt -= readlen; } }