Mercurial > hg > toybox
annotate toys/posix/tee.c @ 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.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
rev | line source |
---|---|
308 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * tee.c - cat to multiple outputs. | |
4 * | |
5 * Copyright 2008 Rob Landley <rob@landley.net> | |
6 * | |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/tee.html |
308 | 8 |
9 USE_TEE(NEWTOY(tee, "ia", TOYFLAG_BIN)) | |
10 | |
11 config TEE | |
12 bool "tee" | |
13 default y | |
14 help | |
15 usage: tee [-ai] [file...] | |
16 | |
17 Copy stdin to each listed file, and also to stdout. | |
18 Filename "-" is a synonym for stdout. | |
19 | |
20 -a append to files. | |
21 -i ignore SIGINT. | |
22 */ | |
23 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
24 #define FOR_tee |
308 | 25 #include "toys.h" |
26 | |
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
|
27 GLOBALS( |
308 | 28 void *outputs; |
29 ) | |
30 | |
31 struct fd_list { | |
32 struct fd_list *next; | |
33 int fd; | |
34 }; | |
35 | |
36 // Open each output file, saving filehandles to a linked list. | |
37 | |
38 static void do_tee_open(int fd, char *name) | |
39 { | |
40 struct fd_list *temp; | |
41 | |
42 temp = xmalloc(sizeof(struct fd_list)); | |
43 temp->next = TT.outputs; | |
44 temp->fd = fd; | |
45 TT.outputs = temp; | |
46 } | |
47 | |
48 void tee_main(void) | |
49 { | |
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
|
50 if (toys.optflags & FLAG_i) signal(SIGINT, SIG_IGN); |
308 | 51 |
52 // Open output files | |
53 loopfiles_rw(toys.optargs, | |
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
|
54 O_RDWR|O_CREAT|((toys.optflags & FLAG_a)?O_APPEND:O_TRUNC), |
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
|
55 0666, 0, do_tee_open); |
308 | 56 |
57 for (;;) { | |
58 struct fd_list *fdl; | |
59 int len; | |
60 | |
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 } | |
73 | |
74 } |