summaryrefslogtreecommitdiff
path: root/src/tee.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1993-12-28 23:46:12 +0000
committerJim Meyering <jim@meyering.net>1993-12-28 23:46:12 +0000
commit3ffdef958d5a35bc106da6430f9ddbc512afe7ca (patch)
treebb3121adaecb59ed7eb32120358cd34ddbb7573f /src/tee.c
parent0f5e2eba7cb629b5ef8958f2b65079ae7967dd13 (diff)
downloadcoreutils-3ffdef958d5a35bc106da6430f9ddbc512afe7ca.tar.xz
.
Diffstat (limited to 'src/tee.c')
-rw-r--r--src/tee.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/tee.c b/src/tee.c
index f592ae788..1444b2a01 100644
--- a/src/tee.c
+++ b/src/tee.c
@@ -172,7 +172,17 @@ tee (nfiles, files)
else
mode |= O_TRUNC;
- for (i = 0; i < nfiles; i++)
+ /* Move all the names `up' one in the argv array to make room for
+ the entry for standard output. This writes into argv[argc]. */
+ for (i = nfiles; i >= 1; i--)
+ files[i] = files[i - 1];
+
+ /* In the array of NFILES + 1 descriptors, make
+ the first one correspond to standard output. */
+ descriptors[0] = 1;
+ files[0] = "standard output";
+
+ for (i = 1; i <= nfiles; i++)
{
descriptors[i] = open (files[i], mode, 0666);
if (descriptors[i] == -1)
@@ -182,12 +192,6 @@ tee (nfiles, files)
}
}
- /* In the array of NFILES + 1 descriptors, make
- the last one correspond to standard output. */
- descriptors[nfiles] = 1;
- /* This writes into argv[argc]. */
- files[nfiles] = "standard output";
-
while (1)
{
bytes_read = read (0, buffer, sizeof buffer);
@@ -199,14 +203,16 @@ tee (nfiles, files)
break;
/* Write to all NFILES + 1 descriptors.
- Standard output is the last one. */
+ Standard output is the first one. */
for (i = 0; i <= nfiles; i++)
{
if (descriptors[i] != -1
&& safe_write (descriptors[i], buffer, bytes_read))
{
error (0, errno, "%s", files[i]);
- close (descriptors[i]);
+ /* Don't close stdout. That's done in main. */
+ if (descriptors[i] != 1)
+ close (descriptors[i]);
descriptors[i] = -1;
ret = 1;
}
@@ -219,7 +225,8 @@ tee (nfiles, files)
ret = 1;
}
- for (i = 0; i < nfiles; i++)
+ /* Close the files, but not standard output. */
+ for (i = 1; i <= nfiles; i++)
if (descriptors[i] != -1 && close (descriptors[i]) != 0)
{
error (0, errno, "%s", files[i]);