annotate toys/posix/tail.c @ 1563:3d32f9523584 draft

Fix "tail -c 10" segfault spotted by Lukasz Szpakowski. Once we've read through the initial TT.bytes backlog we discard the extra data, meaning we adjust the remaining amount each time so the overflow is zero bytes. We were doing the adjustment right, but not zeroing out the overflow counter after we did so.
author Rob Landley <rob@landley.net>
date Wed, 19 Nov 2014 14:29:53 -0600
parents 1d996b0a11c0
children fa8f0a5dfc11
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;
1563
3d32f9523584 Fix "tail -c 10" segfault spotted by Lukasz Szpakowski.
Rob Landley <rob@landley.net>
parents: 1536
diff changeset
167 bytes = 0;
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
168 }
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
169 } 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
170 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
171 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
172
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 // 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
174 // 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
175 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
176 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
177 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
178
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 if (lines > 0) {
1536
1d996b0a11c0 Fix use after free error spotted by ?ukasz Szpakowski.
Rob Landley <rob@landley.net>
parents: 1059
diff changeset
180 char c;
1d996b0a11c0 Fix use after free error spotted by ?ukasz Szpakowski.
Rob Landley <rob@landley.net>
parents: 1059
diff changeset
181
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
182 do {
1536
1d996b0a11c0 Fix use after free error spotted by ?ukasz Szpakowski.
Rob Landley <rob@landley.net>
parents: 1059
diff changeset
183 c = *list->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
184 if (!--(list->len)) free(dlist_pop(&list));
1536
1d996b0a11c0 Fix use after free error spotted by ?ukasz Szpakowski.
Rob Landley <rob@landley.net>
parents: 1059
diff changeset
185 else list->data++;
1d996b0a11c0 Fix use after free error spotted by ?ukasz Szpakowski.
Rob Landley <rob@landley.net>
parents: 1059
diff changeset
186 } while (c != '\n');
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
187 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
188 }
1034
54c9d4a0d46e tail: Some fixes
Felix Janda <felix.janda@posteo.de>
parents: 714
diff changeset
189 }
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
190 }
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 }
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
192
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
193 // 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
194 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
195
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
196 // 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
197 } 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
198 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
199
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
200 // 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
201 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
202 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
203 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
204 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
205 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
206 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
207 }
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
208 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
209 }
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
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 // -f support: cache name/descriptor
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
212 }
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
213
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
214 void tail_main(void)
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
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 // 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
217 if (!(toys.optflags&(FLAG_n|FLAG_c))) TT.lines = -10;
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
218
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
219 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
220
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
221 // do -f stuff
494
eebfb11e84db Add tail.
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
222 }