diff options
author | Jim Meyering <jim@meyering.net> | 1993-12-28 23:22:03 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1993-12-28 23:22:03 +0000 |
commit | 0f5e2eba7cb629b5ef8958f2b65079ae7967dd13 (patch) | |
tree | 2da1740f175f222cddabae9f6ef6b44a333a4d53 | |
parent | 704a7eba4a17dd28daa9eecace2c741e3401c0bc (diff) | |
download | coreutils-0f5e2eba7cb629b5ef8958f2b65079ae7967dd13.tar.xz |
.
-rw-r--r-- | src/tee.c | 34 |
1 files changed, 25 insertions, 9 deletions
@@ -38,7 +38,7 @@ char *xmalloc (); void error (); -void xwrite (); +int safe_write (); static int tee (); @@ -164,8 +164,8 @@ tee (nfiles, files) char buffer[BUFSIZ]; register int bytes_read, i, ret = 0, mode; - if (nfiles) - descriptors = (int *) xmalloc (nfiles * sizeof (int)); + descriptors = (int *) xmalloc ((nfiles + 1) * sizeof (int)); + mode = O_WRONLY | O_CREAT; if (append) mode |= O_APPEND; @@ -182,6 +182,12 @@ 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); @@ -191,11 +197,22 @@ tee (nfiles, files) #endif if (bytes_read <= 0) break; - xwrite (1, buffer, bytes_read); - for (i = 0; i < nfiles; i++) - if (descriptors[i] != -1) - xwrite (descriptors[i], buffer, bytes_read); + + /* Write to all NFILES + 1 descriptors. + Standard output is the last 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]); + descriptors[i] = -1; + ret = 1; + } + } } + if (bytes_read == -1) { error (0, errno, "read error"); @@ -209,8 +226,7 @@ tee (nfiles, files) ret = 1; } - if (nfiles) - free (descriptors); + free (descriptors); return ret; } |