annotate toys/tee.c @ 308:52600eee8dd6

Add "tee" command.
author Rob Landley <rob@landley.net>
date Thu, 03 Jul 2008 19:19:00 -0500
parents
children 32c7b6af5b29
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 *
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/tee.html
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,
52600eee8dd6 Add "tee" command.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 O_RDWR|O_CREAT|((toys.optflags&1)?O_APPEND:O_TRUNC), do_tee_open);
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 }