Mercurial > hg > toybox
annotate toys/posix/head.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
rev | line source |
---|---|
444 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * head.c - copy first lines from input to stdout. | |
4 * | |
5 * Copyright 2006 Timothy Elliott <tle@holymonkey.com> | |
6 * | |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/head.html |
444 | 8 |
445 | 9 USE_HEAD(NEWTOY(head, "n#<0=10", TOYFLAG_BIN)) |
444 | 10 |
11 config HEAD | |
12 bool "head" | |
13 default y | |
14 help | |
15 usage: head [-n number] [file...] | |
445 | 16 |
444 | 17 Copy first lines from files to stdout. If no files listed, copy from |
18 stdin. Filename "-" is a synonym for stdin. | |
19 | |
20 -n Number of lines to copy. | |
21 */ | |
22 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
23 #define FOR_head |
444 | 24 #include "toys.h" |
25 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
26 GLOBALS( |
444 | 27 long lines; |
28 int file_no; | |
29 ) | |
30 | |
31 static void do_head(int fd, char *name) | |
32 { | |
33 int i, len, lines=TT.lines, size=sizeof(toybuf); | |
34 | |
35 if (toys.optc > 1) { | |
36 // Print an extra newline for all but the first file | |
451 | 37 if (TT.file_no++) xprintf("\n"); |
445 | 38 xprintf("==> %s <==\n", name); |
451 | 39 xflush(); |
444 | 40 } |
41 | |
445 | 42 while (lines) { |
444 | 43 len = read(fd, toybuf, size); |
44 if (len<0) { | |
45 perror_msg("%s",name); | |
46 toys.exitval = EXIT_FAILURE; | |
47 } | |
48 if (len<1) break; | |
49 | |
445 | 50 for(i=0; i<len;) |
51 if (toybuf[i++] == '\n' && !--lines) break; | |
52 | |
53 xwrite(1, toybuf, i); | |
444 | 54 } |
55 } | |
56 | |
57 void head_main(void) | |
58 { | |
59 loopfiles(toys.optargs, do_head); | |
60 } |