From 3ffdef958d5a35bc106da6430f9ddbc512afe7ca Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Tue, 28 Dec 1993 23:46:12 +0000 Subject: . --- src/tee.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/tee.c') 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]); -- cgit v1.2.3-54-g00ecf