Mercurial > vim
annotate src/tee/tee.c @ 20261:aafedd368f40 v8.2.0686
patch 8.2.0686: formatoptions not sufficiently tested
Commit: https://github.com/vim/vim/commit/2eaeaf3c317a5145fb0bc926411561d50883019b
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun May 3 16:04:43 2020 +0200
patch 8.2.0686: formatoptions not sufficiently tested
Problem: Formatoptions not sufficiently tested.
Solution: Add a few more tests. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/6031)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 03 May 2020 16:15:03 +0200 |
parents | 6b5ce5161d6d |
children | bc7ed647d654 |
rev | line source |
---|---|
7 | 1 /* vim:set ts=4 sw=4: |
2 * | |
3 * Copyright (c) 1996, Paul Slootman | |
4 * | |
5 * Author: Paul Slootman | |
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 | 8 * |
9 * This source code is released into the public domain. It is provided on an | |
10 * as-is basis and no responsibility is accepted for its failure to perform | |
11 * as expected. It is worth at least as much as you paid for it! | |
12 * | |
13 * tee.c - pipe fitting | |
14 * | |
15 * tee reads stdin, and writes what it reads to each of the specified | |
16 * files. The primary reason of existence for this version is a quick | |
17 * and dirty implementation to distribute with Vim, to make one of the | |
18 * most useful features of Vim possible on OS/2: quickfix. | |
19 * | |
20 * Of course, not using tee but instead redirecting make's output directly | |
21 * into a temp file and then processing that is possible, but if we have a | |
22 * system capable of correctly piping (unlike DOS, for example), why not | |
23 * use it as well as possible? This tee should also work on other systems, | |
24 * but it's not been tested there, only on OS/2. | |
25 * | |
26 * tee is also available in the GNU shellutils package, which is available | |
27 * precompiled for OS/2. That one probably works better. | |
28 */ | |
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 | 33 #include <malloc.h> |
34 #include <stdio.h> | |
7475
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
35 #include <fcntl.h> |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
36 |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
37 #ifdef _WIN32 |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
38 # define sysconf(x) -1 |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
39 #endif |
7 | 40 |
41 void usage(void) | |
42 { | |
43 fprintf(stderr, | |
44 "tee usage:\n\ | |
45 \ttee [-a] file ... file_n\n\ | |
46 \n\ | |
47 \t-a\tappend to files instead of truncating\n\ | |
48 \nTee reads its input, and writes to each of the specified files,\n\ | |
49 as well as to the standard output.\n\ | |
50 \n\ | |
51 This version supplied with Vim 4.2 to make ':make' possible.\n\ | |
52 For a more complete and stable version, consider getting\n\ | |
53 [a port of] the GNU shellutils package.\n\ | |
54 "); | |
55 } | |
56 | |
57 /* | |
58 * fread only returns when count is read or at EOF. | |
59 * We could use fgets, but I want to be able to handle binary blubber. | |
60 */ | |
61 | |
62 int | |
63 myfread(char *buf, int elsize /*ignored*/, int max, FILE *fp) | |
64 { | |
65 int c; | |
66 int n = 0; | |
67 | |
68 while ((n < max) && ((c = getchar()) != EOF)) | |
69 { | |
70 *(buf++) = c; | |
71 n++; | |
72 if (c == '\n' || c == '\r') | |
73 break; | |
74 } | |
75 return n; | |
76 } | |
77 | |
78 | |
79 void | |
80 main(int argc, char *argv[]) | |
81 { | |
82 int append = 0; | |
83 int numfiles; | |
84 int opt; | |
85 int maxfiles; | |
86 FILE **filepointers; | |
87 int i; | |
88 char buf[BUFSIZ]; | |
89 int n; | |
7475
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
90 int optind = 1; |
7 | 91 |
7475
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
92 for (i = 1; i < argc; i++) |
7 | 93 { |
7475
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
94 if (argv[i][0] != '-') |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
95 break; |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
96 if (!strcmp(argv[i], "-a")) |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
97 append++; |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
98 else |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
99 usage(); |
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
100 optind++; |
7 | 101 } |
102 | |
103 numfiles = argc - optind; | |
104 | |
105 if (numfiles == 0) | |
106 { | |
107 fprintf(stderr, "doesn't make much sense using tee without any file name arguments...\n"); | |
108 usage(); | |
109 exit(2); | |
110 } | |
111 | |
112 maxfiles = sysconf(_SC_OPEN_MAX); /* or fill in 10 or so */ | |
113 if (maxfiles < 0) | |
114 maxfiles = 10; | |
115 if (numfiles + 3 > maxfiles) /* +3 accounts for stdin, out, err */ | |
116 { | |
117 fprintf(stderr, "Sorry, there is a limit of max %d files.\n", maxfiles - 3); | |
118 exit(1); | |
119 } | |
120 filepointers = calloc(numfiles, sizeof(FILE *)); | |
121 if (filepointers == NULL) | |
122 { | |
123 fprintf(stderr, "Error allocating memory for %d files\n", numfiles); | |
124 exit(1); | |
125 } | |
126 for (i = 0; i < numfiles; i++) | |
127 { | |
128 filepointers[i] = fopen(argv[i+optind], append ? "ab" : "wb"); | |
129 if (filepointers[i] == NULL) | |
130 { | |
131 fprintf(stderr, "Can't open \"%s\"\n", argv[i+optind]); | |
132 exit(1); | |
133 } | |
134 } | |
7475
6b5ce5161d6d
commit https://github.com/vim/vim/commit/24db72958fc91bd067c7d60a4990d09a6f295b48
Christian Brabandt <cb@256bit.org>
parents:
7
diff
changeset
|
135 setmode(fileno(stdin), O_BINARY); |
7 | 136 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
|
137 setmode(fileno(stdout), O_BINARY); |
7 | 138 |
139 while ((n = myfread(buf, sizeof(char), sizeof(buf), stdin)) > 0) | |
140 { | |
141 fwrite(buf, sizeof(char), n, stdout); | |
142 fflush(stdout); | |
143 for (i = 0; i < numfiles; i++) | |
144 { | |
145 if (filepointers[i] && | |
146 fwrite(buf, sizeof(char), n, filepointers[i]) != n) | |
147 { | |
148 fprintf(stderr, "Error writing to file \"%s\"\n", argv[i+optind]); | |
149 fclose(filepointers[i]); | |
150 filepointers[i] = NULL; | |
151 } | |
152 } | |
153 } | |
154 for (i = 0; i < numfiles; i++) | |
155 { | |
156 if (filepointers[i]) | |
157 fclose(filepointers[i]); | |
158 } | |
159 | |
160 exit(0); | |
161 } |