Mercurial > hg > toybox
comparison toys/posix/head.c @ 694:786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
The actual code should be the same afterward, this is just cosmetic refactoring.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 13 Nov 2012 17:14:08 -0600 |
parents | 7e846e281e38 |
children | 6cc69be43c42 |
comparison
equal
deleted
inserted
replaced
693:4a5a250e0633 | 694:786841fdb1e0 |
---|---|
1 /* vi: set sw=4 ts=4: | 1 /* head.c - copy first lines from input to stdout. |
2 * | |
3 * head.c - copy first lines from input to stdout. | |
4 * | 2 * |
5 * Copyright 2006 Timothy Elliott <tle@holymonkey.com> | 3 * Copyright 2006 Timothy Elliott <tle@holymonkey.com> |
6 * | 4 * |
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/head.html | 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/head.html |
8 | 6 |
9 USE_HEAD(NEWTOY(head, "n#<0=10", TOYFLAG_BIN)) | 7 USE_HEAD(NEWTOY(head, "n#<0=10", TOYFLAG_BIN)) |
10 | 8 |
11 config HEAD | 9 config HEAD |
12 bool "head" | 10 bool "head" |
13 default y | 11 default y |
14 help | 12 help |
15 usage: head [-n number] [file...] | 13 usage: head [-n number] [file...] |
16 | 14 |
17 Copy first lines from files to stdout. If no files listed, copy from | 15 Copy first lines from files to stdout. If no files listed, copy from |
18 stdin. Filename "-" is a synonym for stdin. | 16 stdin. Filename "-" is a synonym for stdin. |
19 | 17 |
20 -n Number of lines to copy. | 18 -n Number of lines to copy. |
21 */ | 19 */ |
22 | 20 |
23 #define FOR_head | 21 #define FOR_head |
24 #include "toys.h" | 22 #include "toys.h" |
25 | 23 |
26 GLOBALS( | 24 GLOBALS( |
27 long lines; | 25 long lines; |
28 int file_no; | 26 int file_no; |
29 ) | 27 ) |
30 | 28 |
31 static void do_head(int fd, char *name) | 29 static void do_head(int fd, char *name) |
32 { | 30 { |
33 int i, len, lines=TT.lines, size=sizeof(toybuf); | 31 int i, len, lines=TT.lines, size=sizeof(toybuf); |
34 | 32 |
35 if (toys.optc > 1) { | 33 if (toys.optc > 1) { |
36 // Print an extra newline for all but the first file | 34 // Print an extra newline for all but the first file |
37 if (TT.file_no++) xprintf("\n"); | 35 if (TT.file_no++) xprintf("\n"); |
38 xprintf("==> %s <==\n", name); | 36 xprintf("==> %s <==\n", name); |
39 xflush(); | 37 xflush(); |
40 } | 38 } |
41 | 39 |
42 while (lines) { | 40 while (lines) { |
43 len = read(fd, toybuf, size); | 41 len = read(fd, toybuf, size); |
44 if (len<0) { | 42 if (len<0) { |
45 perror_msg("%s",name); | 43 perror_msg("%s",name); |
46 toys.exitval = EXIT_FAILURE; | 44 toys.exitval = EXIT_FAILURE; |
47 } | 45 } |
48 if (len<1) break; | 46 if (len<1) break; |
49 | |
50 for(i=0; i<len;) | |
51 if (toybuf[i++] == '\n' && !--lines) break; | |
52 | 47 |
53 xwrite(1, toybuf, i); | 48 for(i=0; i<len;) if (toybuf[i++] == '\n' && !--lines) break; |
54 } | 49 |
50 xwrite(1, toybuf, i); | |
51 } | |
55 } | 52 } |
56 | 53 |
57 void head_main(void) | 54 void head_main(void) |
58 { | 55 { |
59 loopfiles(toys.optargs, do_head); | 56 loopfiles(toys.optargs, do_head); |
60 } | 57 } |