annotate src/tee/tee.c @ 1662:ac6d15d91466 v7.2a.014

updated for version 7.2a-014
author vimboss
date Fri, 04 Jul 2008 16:51:55 +0000
parents 3fc0f57ecb91
children 6b5ce5161d6d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 /* vim:set ts=4 sw=4:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 * Copyright (c) 1996, Paul Slootman
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 * Author: Paul Slootman
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 * (paul@wurtel.hobby.nl, paul@murphy.nl, paulS@toecompst.nl)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 * This source code is released into the public domain. It is provided on an
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 * as-is basis and no responsibility is accepted for its failure to perform
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 * as expected. It is worth at least as much as you paid for it!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 * tee.c - pipe fitting
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 * tee reads stdin, and writes what it reads to each of the specified
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 * files. The primary reason of existence for this version is a quick
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 * and dirty implementation to distribute with Vim, to make one of the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 * most useful features of Vim possible on OS/2: quickfix.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 * Of course, not using tee but instead redirecting make's output directly
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 * into a temp file and then processing that is possible, but if we have a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 * system capable of correctly piping (unlike DOS, for example), why not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 * use it as well as possible? This tee should also work on other systems,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 * but it's not been tested there, only on OS/2.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 * tee is also available in the GNU shellutils package, which is available
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 * precompiled for OS/2. That one probably works better.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 #include <unistd.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 #include <malloc.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 #include <stdio.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 void usage(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 fprintf(stderr,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 "tee usage:\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 \ttee [-a] file ... file_n\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 \n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 \t-a\tappend to files instead of truncating\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 \nTee reads its input, and writes to each of the specified files,\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 as well as to the standard output.\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 \n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 This version supplied with Vim 4.2 to make ':make' possible.\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 For a more complete and stable version, consider getting\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 [a port of] the GNU shellutils package.\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 ");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 * fread only returns when count is read or at EOF.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 * We could use fgets, but I want to be able to handle binary blubber.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 myfread(char *buf, int elsize /*ignored*/, int max, FILE *fp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 int c;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 int n = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 while ((n < max) && ((c = getchar()) != EOF))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 *(buf++) = c;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 n++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 if (c == '\n' || c == '\r')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 return n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 main(int argc, char *argv[])
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 int append = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 int numfiles;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 int opt;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 int maxfiles;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 FILE **filepointers;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 char buf[BUFSIZ];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 int n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 extern int optind;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 while ((opt = getopt(argc, argv, "a")) != EOF)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 switch (opt)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 case 'a': append++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 default: usage();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 exit(2);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 numfiles = argc - optind;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 if (numfiles == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 fprintf(stderr, "doesn't make much sense using tee without any file name arguments...\n");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 usage();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 exit(2);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 maxfiles = sysconf(_SC_OPEN_MAX); /* or fill in 10 or so */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 if (maxfiles < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 maxfiles = 10;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 if (numfiles + 3 > maxfiles) /* +3 accounts for stdin, out, err */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 fprintf(stderr, "Sorry, there is a limit of max %d files.\n", maxfiles - 3);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 filepointers = calloc(numfiles, sizeof(FILE *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 if (filepointers == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 fprintf(stderr, "Error allocating memory for %d files\n", numfiles);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 for (i = 0; i < numfiles; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 filepointers[i] = fopen(argv[i+optind], append ? "ab" : "wb");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 if (filepointers[i] == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 fprintf(stderr, "Can't open \"%s\"\n", argv[i+optind]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 _fsetmode(stdin, "b");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 fflush(stdout); /* needed for _fsetmode(stdout) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 _fsetmode(stdout, "b");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 while ((n = myfread(buf, sizeof(char), sizeof(buf), stdin)) > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 fwrite(buf, sizeof(char), n, stdout);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 fflush(stdout);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 for (i = 0; i < numfiles; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 if (filepointers[i] &&
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 fwrite(buf, sizeof(char), n, filepointers[i]) != n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 fprintf(stderr, "Error writing to file \"%s\"\n", argv[i+optind]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 fclose(filepointers[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 filepointers[i] = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 for (i = 0; i < numfiles; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 if (filepointers[i])
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 fclose(filepointers[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 exit(0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 }