annotate src/tee/tee.c @ 29863:62350f19d4ed v9.0.0270

patch 9.0.0270: some values of 'path' and 'tags' invalid in the tiny version Commit: https://github.com/vim/vim/commit/2bd9dbc19fc67395cfa1226dda7326071ab22464 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Aug 25 18:12:06 2022 +0100 patch 9.0.0270: some values of 'path' and 'tags' invalid in the tiny version Problem: Some values of 'path' and 'tags' do not work in the tiny version. Solution: Graduate the +path_extra feature.
author Bram Moolenaar <Bram@vim.org>
date Thu, 25 Aug 2022 19:15:03 +0200
parents 1fe2d79f7309
children
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)
7475
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
7 * Modifications for MSVC: Yasuhiro Matsumoto
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 * This source code is released into the public domain. It is provided on an
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 * as-is basis and no responsibility is accepted for its failure to perform
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 * as expected. It is worth at least as much as you paid for it!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 * tee.c - pipe fitting
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 * tee reads stdin, and writes what it reads to each of the specified
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 * files. The primary reason of existence for this version is a quick
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 * and dirty implementation to distribute with Vim, to make one of the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 * most useful features of Vim possible on OS/2: quickfix.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 * Of course, not using tee but instead redirecting make's output directly
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 * 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
22 * system capable of correctly piping (unlike DOS, for example), why not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 * use it as well as possible? This tee should also work on other systems,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 * but it's not been tested there, only on OS/2.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 * tee is also available in the GNU shellutils package, which is available
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 * precompiled for OS/2. That one probably works better.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29
7475
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
30 #ifndef _MSC_VER
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
31 # include <unistd.h>
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
32 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 #include <malloc.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 #include <stdio.h>
21244
bc7ed647d654 patch 8.2.1173: tee doesn't build on some systems
Bram Moolenaar <Bram@vim.org>
parents: 7475
diff changeset
35 #include <stdlib.h>
bc7ed647d654 patch 8.2.1173: tee doesn't build on some systems
Bram Moolenaar <Bram@vim.org>
parents: 7475
diff changeset
36 #include <string.h>
7475
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
37 #include <fcntl.h>
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
38
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
39 #ifdef _WIN32
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
40 # define sysconf(x) -1
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
41 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 void usage(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 fprintf(stderr,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 "tee usage:\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 \ttee [-a] file ... file_n\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 \n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 \t-a\tappend to files instead of truncating\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 \nTee reads its input, and writes to each of the specified files,\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 as well as to the standard output.\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 \n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 This version supplied with Vim 4.2 to make ':make' possible.\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 For a more complete and stable version, consider getting\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 [a port of] the GNU shellutils package.\n\
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 ");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 * fread only returns when count is read or at EOF.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 * We could use fgets, but I want to be able to handle binary blubber.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 myfread(char *buf, int elsize /*ignored*/, int max, FILE *fp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 int c;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 int n = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 while ((n < max) && ((c = getchar()) != EOF))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 *(buf++) = c;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 n++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 if (c == '\n' || c == '\r')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 return n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80
29088
ba435f6bc1ad patch 8.2.5065: wrong return type for main() in tee.c
Bram Moolenaar <Bram@vim.org>
parents: 21244
diff changeset
81 int
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 main(int argc, char *argv[])
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 int append = 0;
29094
1fe2d79f7309 patch 8.2.5068: gcc 12.1 warning when building tee
Bram Moolenaar <Bram@vim.org>
parents: 29088
diff changeset
85 size_t numfiles;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 int maxfiles;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 FILE **filepointers;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 char buf[BUFSIZ];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 int n;
7475
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
91 int optind = 1;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92
7475
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
93 for (i = 1; i < argc; i++)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 {
7475
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
95 if (argv[i][0] != '-')
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
96 break;
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
97 if (!strcmp(argv[i], "-a"))
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
98 append++;
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
99 else
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
100 usage();
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
101 optind++;
7
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 numfiles = argc - optind;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 if (numfiles == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 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
109 usage();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 exit(2);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 maxfiles = sysconf(_SC_OPEN_MAX); /* or fill in 10 or so */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 if (maxfiles < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 maxfiles = 10;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 if (numfiles + 3 > maxfiles) /* +3 accounts for stdin, out, err */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 fprintf(stderr, "Sorry, there is a limit of max %d files.\n", maxfiles - 3);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 filepointers = calloc(numfiles, sizeof(FILE *));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 if (filepointers == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 {
29094
1fe2d79f7309 patch 8.2.5068: gcc 12.1 warning when building tee
Bram Moolenaar <Bram@vim.org>
parents: 29088
diff changeset
124 fprintf(stderr, "Error allocating memory for %ld files\n",
1fe2d79f7309 patch 8.2.5068: gcc 12.1 warning when building tee
Bram Moolenaar <Bram@vim.org>
parents: 29088
diff changeset
125 (long)numfiles);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 for (i = 0; i < numfiles; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 filepointers[i] = fopen(argv[i+optind], append ? "ab" : "wb");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 if (filepointers[i] == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 fprintf(stderr, "Can't open \"%s\"\n", argv[i+optind]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 exit(1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 }
21244
bc7ed647d654 patch 8.2.1173: tee doesn't build on some systems
Bram Moolenaar <Bram@vim.org>
parents: 7475
diff changeset
137 #ifdef _WIN32
7475
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
138 setmode(fileno(stdin), O_BINARY);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 fflush(stdout); /* needed for _fsetmode(stdout) */
7475
6b5ce5161d6d commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents: 7
diff changeset
140 setmode(fileno(stdout), O_BINARY);
21244
bc7ed647d654 patch 8.2.1173: tee doesn't build on some systems
Bram Moolenaar <Bram@vim.org>
parents: 7475
diff changeset
141 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 while ((n = myfread(buf, sizeof(char), sizeof(buf), stdin)) > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 fwrite(buf, sizeof(char), n, stdout);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 fflush(stdout);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 for (i = 0; i < numfiles; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 if (filepointers[i] &&
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 fwrite(buf, sizeof(char), n, filepointers[i]) != n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 fprintf(stderr, "Error writing to file \"%s\"\n", argv[i+optind]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 fclose(filepointers[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 filepointers[i] = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 for (i = 0; i < numfiles; i++)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 if (filepointers[i])
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 fclose(filepointers[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 exit(0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165 }