comparison toys/posix/tee.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children c0ef9b7976f0
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* tee.c - cat to multiple outputs.
2 *
3 * tee.c - cat to multiple outputs.
4 * 2 *
5 * Copyright 2008 Rob Landley <rob@landley.net> 3 * Copyright 2008 Rob Landley <rob@landley.net>
6 * 4 *
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/tee.html 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/tee.html
8 6
9 USE_TEE(NEWTOY(tee, "ia", TOYFLAG_BIN)) 7 USE_TEE(NEWTOY(tee, "ia", TOYFLAG_BIN))
10 8
11 config TEE 9 config TEE
12 bool "tee" 10 bool "tee"
13 default y 11 default y
14 help 12 help
15 usage: tee [-ai] [file...] 13 usage: tee [-ai] [file...]
16 14
17 Copy stdin to each listed file, and also to stdout. 15 Copy stdin to each listed file, and also to stdout.
18 Filename "-" is a synonym for stdout. 16 Filename "-" is a synonym for stdout.
19 17
20 -a append to files. 18 -a append to files.
21 -i ignore SIGINT. 19 -i ignore SIGINT.
22 */ 20 */
23 21
24 #define FOR_tee 22 #define FOR_tee
25 #include "toys.h" 23 #include "toys.h"
26 24
27 GLOBALS( 25 GLOBALS(
28 void *outputs; 26 void *outputs;
29 ) 27 )
30 28
31 struct fd_list { 29 struct fd_list {
32 struct fd_list *next; 30 struct fd_list *next;
33 int fd; 31 int fd;
34 }; 32 };
35 33
36 // Open each output file, saving filehandles to a linked list. 34 // Open each output file, saving filehandles to a linked list.
37 35
38 static void do_tee_open(int fd, char *name) 36 static void do_tee_open(int fd, char *name)
39 { 37 {
40 struct fd_list *temp; 38 struct fd_list *temp;
41 39
42 temp = xmalloc(sizeof(struct fd_list)); 40 temp = xmalloc(sizeof(struct fd_list));
43 temp->next = TT.outputs; 41 temp->next = TT.outputs;
44 temp->fd = fd; 42 temp->fd = fd;
45 TT.outputs = temp; 43 TT.outputs = temp;
46 } 44 }
47 45
48 void tee_main(void) 46 void tee_main(void)
49 { 47 {
50 if (toys.optflags & FLAG_i) signal(SIGINT, SIG_IGN); 48 if (toys.optflags & FLAG_i) signal(SIGINT, SIG_IGN);
51 49
52 // Open output files 50 // Open output files
53 loopfiles_rw(toys.optargs, 51 loopfiles_rw(toys.optargs,
54 O_RDWR|O_CREAT|((toys.optflags & FLAG_a)?O_APPEND:O_TRUNC), 52 O_RDWR|O_CREAT|((toys.optflags & FLAG_a)?O_APPEND:O_TRUNC),
55 0666, 0, do_tee_open); 53 0666, 0, do_tee_open);
56 54
55 for (;;) {
56 struct fd_list *fdl;
57 int len;
58
59 // Read data from stdin
60 len = xread(0, toybuf, sizeof(toybuf));
61 if (len<1) break;
62
63 // Write data to each output file, plus stdout.
64 fdl = TT.outputs;
57 for (;;) { 65 for (;;) {
58 struct fd_list *fdl; 66 if(len != writeall(fdl ? fdl->fd : 1, toybuf, len)) toys.exitval=1;
59 int len; 67 if (!fdl) break;
60 68 fdl = fdl->next;
61 // Read data from stdin
62 len = xread(0, toybuf, sizeof(toybuf));
63 if (len<1) break;
64
65 // Write data to each output file, plus stdout.
66 fdl = TT.outputs;
67 for (;;) {
68 if(len != writeall(fdl ? fdl->fd : 1, toybuf, len)) toys.exitval=1;
69 if (!fdl) break;
70 fdl = fdl->next;
71 }
72 } 69 }
73 70 }
74 } 71 }