Today someone bumped into a port that contained head -c calls.  While
upstream could be prodded to care a bit more about portability, support
for head -c is widespread (GNU coreutils, busybox, FreeBSD, NetBSD) so
I don't see any value in being different.  Plus, tail(1) already has
support for -c.

Comments/ok?

PS: the next diff will remove documentation for the obsolete "-count"
syntax.

Index: head.1
===================================================================
RCS file: /cvs/src/usr.bin/head/head.1,v
retrieving revision 1.23
diff -u -p -r1.23 head.1
--- head.1      25 Oct 2015 21:50:32 -0000      1.23
+++ head.1      9 Mar 2016 20:02:08 -0000
@@ -37,7 +37,7 @@
 .Nd display first few lines of files
 .Sh SYNOPSIS
 .Nm head
-.Op Fl Ar count | Fl n Ar count
+.Op Fl c Ar count | Fl Ar count | Fl n Ar count
 .Op Ar
 .Sh DESCRIPTION
 The
@@ -56,6 +56,12 @@ is omitted, it defaults to 10.
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
+.It Fl c Ar count
+Copy the first
+.Ar count
+bytes of each input file to the standard output.
+.Ar count
+must be a positive decimal integer.
 .It Fl Ar count | Fl n Ar count
 Copy the first
 .Ar count
Index: head.c
===================================================================
RCS file: /cvs/src/usr.bin/head/head.c,v
retrieving revision 1.20
diff -u -p -r1.20 head.c
--- head.c      9 Oct 2015 01:37:07 -0000       1.20
+++ head.c      9 Mar 2016 20:02:08 -0000
@@ -40,7 +40,7 @@
 static void usage(void);
 
 /*
- * head - give the first few lines of a stream or of each of a set of files
+ * head - give the first few bytes/lines of a stream or of each of a set of 
files
  *
  * Bill Joy UCB August 24, 1977
  */
@@ -51,9 +51,9 @@ main(int argc, char *argv[])
        FILE    *fp;
        long    cnt;
        int     ch, firsttime;
-       long    linecnt = 10;
+       long    count = 10;
        char    *p = NULL;
-       int     status = 0;
+       int     dobytes = 0, status = 0;
 
        if (pledge("stdio rpath", NULL) == -1)
                err(1, "pledge");
@@ -66,13 +66,18 @@ main(int argc, char *argv[])
                argv++;
        }
 
-       while ((ch = getopt(argc, argv, "n:")) != -1) {
+       while ((ch = getopt(argc, argv, "c:n:")) != -1) {
                switch (ch) {
+               case 'c':
+                       dobytes = 1;
+                       p = optarg;
+                       break;
                case 'n':
+                       dobytes = 0;
                        p = optarg;
                        break;
                default:
-                       usage();        
+                       usage();
                }
        }
        argc -= optind, argv += optind;
@@ -80,9 +85,10 @@ main(int argc, char *argv[])
        if (p) {
                const char *errstr;
 
-               linecnt = strtonum(p, 1, LONG_MAX, &errstr);
+               count = strtonum(p, 1, LONG_MAX, &errstr);
                if (errstr)
-                       errx(1, "line count %s: %s", errstr, p);
+                       errx(1, "%s count %s: %s", dobytes ? "bytes" : "lines",
+                           errstr, p);
        }
 
        for (firsttime = 1; ; firsttime = 0) {
@@ -105,10 +111,16 @@ main(int argc, char *argv[])
                        }
                        ++argv;
                }
-               for (cnt = linecnt; cnt && !feof(fp); --cnt)
-                       while ((ch = getc(fp)) != EOF)
-                               if (putchar(ch) == '\n')
-                                       break;
+               if (dobytes) {
+                       for (cnt = count; cnt && !feof(fp); --cnt)
+                               if ((ch = getc(fp)) != EOF)
+                                       putchar(ch);
+               } else {
+                       for (cnt = count; cnt && !feof(fp); --cnt)
+                               while ((ch = getc(fp)) != EOF)
+                                       if (putchar(ch) == '\n')
+                                               break;
+               }
                fclose(fp);
        }
        /*NOTREACHED*/
@@ -118,6 +130,7 @@ main(int argc, char *argv[])
 static void
 usage(void)
 {
-       fputs("usage: head [-count | -n count] [file ...]\n", stderr);
+       fputs("usage: head [-c count | -count | -n count] [file ...]\n",
+           stderr);
        exit(1);
 }


-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to