1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 1985, 1990, 1991 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
21 #include <sys/types.h>
32 /* If nonzero, append to output files rather than truncating them. */
35 /* If nonzero, ignore interrupts. */
36 static int ignore_interrupts;
38 /* The name that this program was run with. */
41 static struct option const long_options[] =
43 {"append", 0, NULL, 'a'},
44 {"ignore-interrupts", 0, NULL, 'i'},
56 program_name = argv[0];
58 ignore_interrupts = 0;
60 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
69 ignore_interrupts = 1;
73 Usage: %s [-ai] [--append] [--ignore-interrupts] [file...]\n",
79 if (ignore_interrupts)
82 struct sigaction sigact;
84 sigact.sa_handler = SIG_IGN;
85 sigemptyset (&sigact.sa_mask);
87 sigaction (SIGINT, &sigact, NULL);
89 #else /* !_POSIX_VERSION */
90 signal (SIGINT, SIG_IGN);
91 #endif /* _POSIX_VERSION */
93 errs = tee (argc - optind, &argv[optind]);
95 error (1, errno, "standard input");
97 error (1, errno, "standard output");
101 /* Copy the standard input into each of the NFILES files in FILES
102 and into the standard output.
103 Return 0 if successful, 1 if any errors occur. */
112 register int bytes_read, i, ret = 0, mode;
115 descriptors = (int *) xmalloc (nfiles * sizeof (int));
116 mode = O_WRONLY | O_CREAT;
122 for (i = 0; i < nfiles; i++)
124 descriptors[i] = open (files[i], mode, 0666);
125 if (descriptors[i] == -1)
127 error (0, errno, "%s", files[i]);
132 while ((bytes_read = read (0, buffer, sizeof buffer)) > 0)
134 xwrite (1, buffer, bytes_read);
135 for (i = 0; i < nfiles; i++)
136 if (descriptors[i] != -1)
137 xwrite (descriptors[i], buffer, bytes_read);
139 if (bytes_read == -1)
141 error (0, errno, "read error");
145 for (i = 0; i < nfiles; i++)
146 if (descriptors[i] != -1 && close (descriptors[i]) == -1)
148 error (0, errno, "%s", files[i]);