annotate toys/posix/tee.c @ 656:6df4ccc0acbe

Regularize command headers, update links to standards documents.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 18:08:51 -0500
parents 2986aa63a021
children 7e846e281e38
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 #include "toys.h"
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
25
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 DEFINE_GLOBALS(
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 void *outputs;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 )
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 #define TT this.tee
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 struct fd_list {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 struct fd_list *next;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 int fd;
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 // Open each output file, saving filehandles to a linked list.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
38
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 static void do_tee_open(int fd, char *name)
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 struct fd_list *temp;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
42
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 temp = xmalloc(sizeof(struct fd_list));
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 temp->next = TT.outputs;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 temp->fd = fd;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 TT.outputs = temp;
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 void tee_main(void)
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 if (toys.optflags&2) signal(SIGINT, SIG_IGN);
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
52
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 // Open output files
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 loopfiles_rw(toys.optargs,
395
32c7b6af5b29 Whitespace/wordwrap changes.
Rob Landley <rob@landley.net>
parents: 308
diff changeset
55 O_RDWR|O_CREAT|((toys.optflags&1)?O_APPEND:O_TRUNC), 0666, 0,
32c7b6af5b29 Whitespace/wordwrap changes.
Rob Landley <rob@landley.net>
parents: 308
diff changeset
56 do_tee_open);
308
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 for (;;) {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 struct fd_list *fdl;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 int len;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
61
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 // Read data from stdin
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 len = xread(0, toybuf, sizeof(toybuf));
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 if (len<1) break;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
65
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 // Write data to each output file, plus stdout.
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 fdl = TT.outputs;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 for (;;) {
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 if(len != writeall(fdl ? fdl->fd : 1, toybuf, len)) toys.exitval=1;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 if (!fdl) break;
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 fdl = fdl->next;
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
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 }