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>
31 /* If nonzero, append to output files rather than truncating them. */
34 /* If nonzero, ignore interrupts. */
35 static int ignore_interrupts;
37 /* The name that this program was run with. */
40 static struct option long_options[] =
42 {"append", 0, NULL, 'a'},
43 {"ignore-interrupts", 0, NULL, 'i'},
55 program_name = argv[0];
57 ignore_interrupts = 0;
59 while ((optc = getopt_long (argc, argv, "ai", long_options, (int *) 0))
68 ignore_interrupts = 1;
72 Usage: %s [-ai] [--append] [--ignore-interrupts] [file...]\n",
78 if (ignore_interrupts)
81 struct sigaction sigact;
83 sigact.sa_handler = SIG_IGN;
84 sigemptyset (&sigact.sa_mask);
86 sigaction (SIGINT, &sigact, NULL);
88 #else /* !_POSIX_VERSION */
89 signal (SIGINT, SIG_IGN);
90 #endif /* _POSIX_VERSION */
92 errs = tee (argc - optind, &argv[optind]);
94 error (1, errno, "standard input");
96 error (1, errno, "standard output");
100 /* Copy the standard input into each of the NFILES files in FILES
101 and into the standard output.
102 Return 0 if successful, 1 if any errors occur. */
111 register int bytes_read, i, ret = 0, mode;
114 descriptors = (int *) xmalloc (nfiles * sizeof (int));
115 mode = O_WRONLY | O_CREAT;
121 for (i = 0; i < nfiles; i++)
123 descriptors[i] = open (files[i], mode, 0666);
124 if (descriptors[i] == -1)
126 error (0, errno, "%s", files[i]);
131 while ((bytes_read = read (0, buffer, sizeof buffer)) > 0)
133 xwrite (1, buffer, bytes_read);
134 for (i = 0; i < nfiles; i++)
135 if (descriptors[i] != -1)
136 xwrite (descriptors[i], buffer, bytes_read);
138 if (bytes_read == -1)
140 error (0, errno, "read error");
144 for (i = 0; i < nfiles; i++)
145 if (descriptors[i] != -1 && close (descriptors[i]) == -1)
147 error (0, errno, "%s", files[i]);