annotate toys/posix/head.c @ 700:ad6bff0c9169

Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
author Rob Landley <rob@landley.net>
date Fri, 16 Nov 2012 15:36:01 -0600
parents 786841fdb1e0
children 6cc69be43c42
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* head.c - copy first lines from input to stdout.
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
2 *
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
3 * Copyright 2006 Timothy Elliott <tle@holymonkey.com>
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
4 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/head.html
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
6
445
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
7 USE_HEAD(NEWTOY(head, "n#<0=10", TOYFLAG_BIN))
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
8
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
9 config HEAD
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
10 bool "head"
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
11 default y
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
12 help
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
13 usage: head [-n number] [file...]
445
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
14
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
15 Copy first lines from files to stdout. If no files listed, copy from
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
16 stdin. Filename "-" is a synonym for stdin.
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
17
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
18 -n Number of lines to copy.
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
19 */
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
20
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
21 #define FOR_head
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
22 #include "toys.h"
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
23
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
24 GLOBALS(
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
25 long lines;
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
26 int file_no;
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
27 )
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
28
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
29 static void do_head(int fd, char *name)
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
30 {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
31 int i, len, lines=TT.lines, size=sizeof(toybuf);
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
32
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
33 if (toys.optc > 1) {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
34 // Print an extra newline for all but the first file
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
35 if (TT.file_no++) xprintf("\n");
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
36 xprintf("==> %s <==\n", name);
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
37 xflush();
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
38 }
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
39
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
40 while (lines) {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
41 len = read(fd, toybuf, size);
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
42 if (len<0) {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
43 perror_msg("%s",name);
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
44 toys.exitval = EXIT_FAILURE;
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
45 }
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
46 if (len<1) break;
445
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
47
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
48 for(i=0; i<len;) if (toybuf[i++] == '\n' && !--lines) break;
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
49
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
50 xwrite(1, toybuf, i);
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
51 }
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
52 }
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
53
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
54 void head_main(void)
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
55 {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
56 loopfiles(toys.optargs, do_head);
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
57 }