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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
308
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * tee.c - cat to multiple outputs.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2008 Rob Landley <rob@landley.net>
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 USE_TEE(NEWTOY(tee, "ia", TOYFLAG_BIN))
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config TEE
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "tee"
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default y
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: tee [-ai] [file...]
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
16
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 Copy stdin to each listed file, and also to stdout.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 Filename "-" is a synonym for stdout.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -a append to files.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 -i ignore SIGINT.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 */
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #include "toys.h"
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 void *outputs;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 )
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
30
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 struct fd_list {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 struct fd_list *next;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 int fd;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 };
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
35
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 // Open each output file, saving filehandles to a linked list.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
37
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 static void do_tee_open(int fd, char *name)
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 struct fd_list *temp;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
41
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 temp = xmalloc(sizeof(struct fd_list));
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 temp->next = TT.outputs;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 temp->fd = fd;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 TT.outputs = temp;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 }
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
47
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 void tee_main(void)
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
51
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 // Open output files
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
56
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 for (;;) {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 struct fd_list *fdl;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 int len;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
60
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 // Read data from stdin
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 len = xread(0, toybuf, sizeof(toybuf));
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 if (len<1) break;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
64
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 // Write data to each output file, plus stdout.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 fdl = TT.outputs;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 for (;;) {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 if(len != writeall(fdl ? fdl->fd : 1, toybuf, len)) toys.exitval=1;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 if (!fdl) break;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 fdl = fdl->next;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 }
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 }
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
73
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 }