annotate toys/posix/tail.c @ 1183:0752b2d58909 draft

Rename xmsprintf() to just xmprintf(). Partly because there's no supplied target string ala sprintf, and partly because I can never remember what order the m and s go in.
author Rob Landley <rob@landley.net>
date Thu, 16 Jan 2014 09:26:50 -0600
parents ef72a16f4b3a
children 1d996b0a11c0
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 /* tail.c - copy last lines from input to stdout.
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
2 *
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
3 * Copyright 2012 Timothy Elliott <tle@holymonkey.com>
eebfb11e84db Add tail.
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/tail.html
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
6
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
7 USE_TAIL(NEWTOY(tail, "fc-n-[-cn]", TOYFLAG_BIN))
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
8
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
9 config TAIL
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 "tail"
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
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
13 usage: tail [-n|c NUMBER] [-f] [FILE...]
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
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 last 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.
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
17
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
18 -n output the last NUMBER lines (default 10), +X counts from start.
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
19 -c output the last NUMBER bytes, +NUMBER counts from start
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
20 -f follow FILE(s), waiting for more data to be appended
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
21
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
22 config TAIL_SEEK
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
23 bool "tail seek support"
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
24 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
25 depends on TAIL
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 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
27 This version uses lseek, which is faster on large files.
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
28 */
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
29
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
30 #define FOR_tail
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
31 #include "toys.h"
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
32
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
33 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
34 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
35 long bytes;
504
a497beb97eee Add "-" type to optargs and teach tail.c to use it. Tighten up help text, use xzalloc() and xputc() as appropriate.
Rob Landley <rob@landley.net>
parents: 495
diff changeset
36
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
37 int file_no;
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
38 )
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
39
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
40 struct line_list {
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
41 struct line_list *next, *prev;
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
42 char *data;
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
43 int len;
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
44 };
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
45
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
46 static struct line_list *get_chunk(int fd, int len)
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
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 struct line_list *line = xmalloc(sizeof(struct line_list)+len);
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
49
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
50 memset(line, 0, sizeof(struct line_list));
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
51 line->data = ((char *)line) + sizeof(struct line_list);
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
52 line->len = readall(fd, line->data, len);
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
53
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
54 if (line->len < 1) {
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
55 free(line);
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 return 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
57 }
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
58
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
59 return line;
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
60 }
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
61
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
62 static void dump_chunk(void *ptr)
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
63 {
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
64 struct line_list *list = ptr;
1034
54c9d4a0d46e tail: Some fixes
Felix Janda <felix.janda@posteo.de>
parents: 714
diff changeset
65
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
66 xwrite(1, list->data, list->len);
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
67 free(list);
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
68 }
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
69
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
70 // Reading through very large files is slow. Using lseek can speed things
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
71 // up a lot, but isn't applicable to all input (cat | tail).
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
72 // Note: bytes and lines are negative here.
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
73 static int try_lseek(int fd, long bytes, long lines)
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
74 {
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
75 struct line_list *list = 0, *temp;
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
76 int flag = 0, chunk = sizeof(toybuf);
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
77 ssize_t pos = lseek(fd, 0, SEEK_END);
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
78
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
79 // If lseek() doesn't work on this stream, return now.
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
80 if (pos<0) return 0;
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
81
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
82 // Seek to the right spot, output data from there.
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
83 if (bytes) {
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
84 if (lseek(fd, bytes, SEEK_END)<0) lseek(fd, 0, SEEK_SET);
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
85 xsendfile(fd, 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
86 return 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
87 }
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
88
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
89 // Read from end to find enough lines, then output them.
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
90
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
91 bytes = pos;
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
92 while (lines && pos) {
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
93 int offset;
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
94
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
95 // Read in next chunk from end of file
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
96 if (chunk>pos) chunk = pos;
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
97 pos -= chunk;
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
98 if (pos != lseek(fd, pos, SEEK_SET)) {
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
99 perror_msg("seek failed");
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
100 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
101 }
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
102 if (!(temp = get_chunk(fd, chunk))) break;
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
103 temp->next = list;
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
104 list = temp;
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
105
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
106 // Count newlines in this chunk.
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
107 offset = list->len;
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
108 while (offset--) {
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
109 // If the last line ends with a newline, that one doesn't count.
1034
54c9d4a0d46e tail: Some fixes
Felix Janda <felix.janda@posteo.de>
parents: 714
diff changeset
110 if (!flag) flag++;
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
111
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
112 // Start outputting data right after newline
1034
54c9d4a0d46e tail: Some fixes
Felix Janda <felix.janda@posteo.de>
parents: 714
diff changeset
113 else if (list->data[offset] == '\n' && !++lines) {
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
114 offset++;
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
115 list->data += offset;
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
116 list->len -= offset;
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
117
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
118 break;
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
119 }
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
120 }
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
121 }
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
122
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
123 // Output stored data
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
124 llist_traverse(list, dump_chunk);
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
125
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
126 // In case of -f
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
127 lseek(fd, bytes, SEEK_SET);
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
128 return 1;
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
129 }
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
130
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
131 // Called for each file listed on command line, and/or stdin
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
132 static void do_tail(int fd, char *name)
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
133 {
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
134 long bytes = TT.bytes, lines = TT.lines;
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
135 int linepop = 1;
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
136
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
137 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
138 if (TT.file_no++) xputc('\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
139 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
140 }
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
141
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
142 // Are we measuring from the end of the file?
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
143
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
144 if (bytes<0 || lines<0) {
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
145 struct line_list *list = 0, *new;
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
146
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
147 // The slow codepath is always needed, and can handle all input,
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
148 // so make lseek support optional.
1034
54c9d4a0d46e tail: Some fixes
Felix Janda <felix.janda@posteo.de>
parents: 714
diff changeset
149 if (CFG_TAIL_SEEK && try_lseek(fd, bytes, lines)) return;
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
150
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
151 // Read data until we run out, keep a trailing buffer
1034
54c9d4a0d46e tail: Some fixes
Felix Janda <felix.janda@posteo.de>
parents: 714
diff changeset
152 for (;;) {
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
153 // Read next page of data, appending to linked list in order
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
154 if (!(new = get_chunk(fd, sizeof(toybuf)))) break;
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
155 dlist_add_nomalloc((void *)&list, (void *)new);
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
156
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
157 // If tracing bytes, add until we have enough, discarding overflow.
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
158 if (TT.bytes) {
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
159 bytes += new->len;
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
160 if (bytes > 0) {
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
161 while (list->len <= bytes) {
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
162 bytes -= list->len;
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
163 free(dlist_pop(&list));
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
164 }
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
165 list->data += bytes;
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
166 list->len -= bytes;
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
167 }
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
168 } else {
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
169 int len = new->len, count;
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
170 char *try = new->data;
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
171
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
172 // First character _after_ a newline starts a new line, which
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
173 // works even if file doesn't end with a newline
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
174 for (count=0; count<len; count++) {
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
175 if (linepop) lines++;
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
176 linepop = try[count] == '\n';
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
177
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
178 if (lines > 0) {
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
179 do {
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
180 if (!--(list->len)) free(dlist_pop(&list));
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
181 } while (*(list->data++) != '\n');
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
182 lines--;
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
183 }
1034
54c9d4a0d46e tail: Some fixes
Felix Janda <felix.janda@posteo.de>
parents: 714
diff changeset
184 }
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
185 }
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
186 }
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
187
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
188 // Output/free the buffer.
1059
ef72a16f4b3a Redo tail closer to the original design. Add more tests for large data sets. (Still no -f support yet.)
Rob Landley <rob@landley.net>
parents: 1034
diff changeset
189 llist_traverse(list, dump_chunk);
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
190
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
191 // Measuring from the beginning of the 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
192 } else for (;;) {
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
193 int len, offset = 0;
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
194
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
195 // Error while reading does not exit. Error writing does.
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
196 len = read(fd, toybuf, sizeof(toybuf));
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
197 if (len<1) 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
198 while (bytes > 1 || lines > 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
199 bytes--;
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
200 if (toybuf[offset++] == '\n') 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
201 if (offset >= len) 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
202 }
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
203 if (offset<len) xwrite(1, toybuf+offset, len-offset);
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
204 }
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
205
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
206 // -f support: cache name/descriptor
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
207 }
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
208
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
209 void tail_main(void)
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
210 {
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
211 // if nothing specified, default -n to -10
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
212 if (!(toys.optflags&(FLAG_n|FLAG_c))) TT.lines = -10;
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
213
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
214 loopfiles(toys.optargs, do_tail);
540
c2f39708a4c4 Redo tail to use optargs and optionally support lseek. Add support to optargs and llist.c, plus add a test suite entry. Still no -f support though.
Rob Landley <rob@landley.net>
parents: 504
diff changeset
215
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
216 // do -f stuff
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
217 }