annotate toys/head.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents f4cc0a2aa440
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
2 *
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
3 * head.c - copy first lines from input to stdout.
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
4 *
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
5 * Copyright 2006 Timothy Elliott <tle@holymonkey.com>
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
6 *
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/head.html
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
8
445
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
9 USE_HEAD(NEWTOY(head, "n#<0=10", TOYFLAG_BIN))
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
10
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
11 config HEAD
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
12 bool "head"
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
13 default y
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
14 help
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
15 usage: head [-n number] [file...]
445
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
16
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
17 Copy first lines from files to stdout. If no files listed, copy from
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
18 stdin. Filename "-" is a synonym for stdin.
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
19
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
20 -n Number of lines to copy.
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
21 */
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
22
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
23 #include "toys.h"
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
24
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
25 DEFINE_GLOBALS(
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
26 long lines;
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
27 int file_no;
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
28 )
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
29
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
30 #define TT this.head
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
31
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
32 static void do_head(int fd, char *name)
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
33 {
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
34 int i, len, lines=TT.lines, size=sizeof(toybuf);
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
35
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
36 if (toys.optc > 1) {
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
37 // Print an extra newline for all but the first file
451
f4cc0a2aa440 Add tests for head
Timothy Elliott <tle@holymonkey.com>
parents: 445
diff changeset
38 if (TT.file_no++) xprintf("\n");
445
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
39 xprintf("==> %s <==\n", name);
451
f4cc0a2aa440 Add tests for head
Timothy Elliott <tle@holymonkey.com>
parents: 445
diff changeset
40 xflush();
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
41 }
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
42
445
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
43 while (lines) {
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
44 len = read(fd, toybuf, size);
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
45 if (len<0) {
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
46 perror_msg("%s",name);
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
47 toys.exitval = EXIT_FAILURE;
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
48 }
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
49 if (len<1) break;
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
50
445
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
51 for(i=0; i<len;)
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
52 if (toybuf[i++] == '\n' && !--lines) break;
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
53
2c47a9c0c619 Cleanups for head.
Rob Landley <rob@landley.net>
parents: 444
diff changeset
54 xwrite(1, toybuf, i);
444
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
55 }
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
56 }
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
57
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
58 void head_main(void)
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
59 {
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
60 loopfiles(toys.optargs, do_head);
d3ca5e15e457 Implement head
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
61 }