1 /* head.c - copy first lines from input to stdout.
3 * Copyright 2006 Timothy Elliott <tle@holymonkey.com>
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/head.html
7 USE_HEAD(NEWTOY(head, "?n#<0=10", TOYFLAG_BIN))
13 usage: head [-n number] [file...]
15 Copy first lines from files to stdout. If no files listed, copy from
16 stdin. Filename "-" is a synonym for stdin.
18 -n Number of lines to copy.
29 static void do_head(int fd, char *name)
31 int i, len, lines=TT.lines, size=sizeof(toybuf);
34 // Print an extra newline for all but the first file
35 if (TT.file_no++) xprintf("\n");
36 xprintf("==> %s <==\n", name);
41 len = read(fd, toybuf, size);
42 if (len<0) perror_msg("%s",name);
45 for(i=0; i<len;) if (toybuf[i++] == '\n' && !--lines) break;
53 char *arg = *toys.optargs;
55 // handle old "-42" style arguments
56 if (arg && *arg == '-' && arg[1]) {
57 TT.lines = atolx(arg+1);
60 loopfiles(toys.optargs+!!arg, do_head);