paste.c revision 1.2 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Adam S. Moskowitz of Menlo Consulting.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1 cgd char copyright[] =
39 1.1 cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
40 1.1 cgd All rights reserved.\n";
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.2 mycroft /*static char sccsid[] = "from: @(#)paste.c 5.7 (Berkeley) 10/30/90";*/
45 1.2 mycroft static char rcsid[] = "$Id: paste.c,v 1.2 1993/08/01 18:10:14 mycroft Exp $";
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd #include <sys/types.h>
49 1.1 cgd #include <errno.h>
50 1.1 cgd #include <limits.h>
51 1.1 cgd #include <stdio.h>
52 1.1 cgd #include <string.h>
53 1.1 cgd
54 1.1 cgd char *delim;
55 1.1 cgd int delimcnt;
56 1.1 cgd
57 1.1 cgd main(argc, argv)
58 1.1 cgd int argc;
59 1.1 cgd char **argv;
60 1.1 cgd {
61 1.1 cgd extern char *optarg;
62 1.1 cgd extern int optind;
63 1.1 cgd int ch, seq;
64 1.1 cgd
65 1.1 cgd seq = 0;
66 1.1 cgd while ((ch = getopt(argc, argv, "d:s")) != EOF)
67 1.1 cgd switch(ch) {
68 1.1 cgd case 'd':
69 1.1 cgd delimcnt = tr(delim = optarg);
70 1.1 cgd break;
71 1.1 cgd case 's':
72 1.1 cgd seq = 1;
73 1.1 cgd break;
74 1.1 cgd case '?':
75 1.1 cgd default:
76 1.1 cgd usage();
77 1.1 cgd }
78 1.1 cgd argc -= optind;
79 1.1 cgd argv += optind;
80 1.1 cgd
81 1.1 cgd if (!delim) {
82 1.1 cgd delimcnt = 1;
83 1.1 cgd delim = "\t";
84 1.1 cgd }
85 1.1 cgd
86 1.1 cgd if (seq)
87 1.1 cgd sequential(argv);
88 1.1 cgd else
89 1.1 cgd parallel(argv);
90 1.1 cgd exit(0);
91 1.1 cgd }
92 1.1 cgd
93 1.1 cgd typedef struct _list {
94 1.1 cgd struct _list *next;
95 1.1 cgd FILE *fp;
96 1.1 cgd int cnt;
97 1.1 cgd char *name;
98 1.1 cgd } LIST;
99 1.1 cgd
100 1.1 cgd parallel(argv)
101 1.1 cgd char **argv;
102 1.1 cgd {
103 1.1 cgd register LIST *lp;
104 1.1 cgd register int cnt;
105 1.1 cgd register char ch, *p;
106 1.1 cgd LIST *head, *tmp;
107 1.1 cgd int opencnt, output;
108 1.1 cgd char buf[_POSIX2_LINE_MAX + 1], *malloc();
109 1.1 cgd
110 1.1 cgd for (cnt = 0, head = NULL; p = *argv; ++argv, ++cnt) {
111 1.1 cgd if (!(lp = (LIST *)malloc((u_int)sizeof(LIST)))) {
112 1.1 cgd (void)fprintf(stderr, "paste: %s.\n", strerror(ENOMEM));
113 1.1 cgd exit(1);
114 1.1 cgd }
115 1.1 cgd if (p[0] == '-' && !p[1])
116 1.1 cgd lp->fp = stdin;
117 1.1 cgd else if (!(lp->fp = fopen(p, "r"))) {
118 1.1 cgd (void)fprintf(stderr, "paste: %s: %s.\n", p,
119 1.1 cgd strerror(errno));
120 1.1 cgd exit(1);
121 1.1 cgd }
122 1.1 cgd lp->next = NULL;
123 1.1 cgd lp->cnt = cnt;
124 1.1 cgd lp->name = p;
125 1.1 cgd if (!head)
126 1.1 cgd head = tmp = lp;
127 1.1 cgd else {
128 1.1 cgd tmp->next = lp;
129 1.1 cgd tmp = lp;
130 1.1 cgd }
131 1.1 cgd }
132 1.1 cgd
133 1.1 cgd for (opencnt = cnt; opencnt;) {
134 1.1 cgd for (output = 0, lp = head; lp; lp = lp->next) {
135 1.1 cgd if (!lp->fp) {
136 1.1 cgd if (output && lp->cnt &&
137 1.1 cgd (ch = delim[(lp->cnt - 1) % delimcnt]))
138 1.1 cgd putchar(ch);
139 1.1 cgd continue;
140 1.1 cgd }
141 1.1 cgd if (!fgets(buf, sizeof(buf), lp->fp)) {
142 1.1 cgd if (!--opencnt)
143 1.1 cgd break;
144 1.1 cgd lp->fp = NULL;
145 1.1 cgd if (output && lp->cnt &&
146 1.1 cgd (ch = delim[(lp->cnt - 1) % delimcnt]))
147 1.1 cgd putchar(ch);
148 1.1 cgd continue;
149 1.1 cgd }
150 1.1 cgd if (!(p = index(buf, '\n'))) {
151 1.1 cgd (void)fprintf(stderr,
152 1.1 cgd "paste: %s: input line too long.\n",
153 1.1 cgd lp->name);
154 1.1 cgd exit(1);
155 1.1 cgd }
156 1.1 cgd *p = '\0';
157 1.1 cgd /*
158 1.1 cgd * make sure that we don't print any delimiters
159 1.1 cgd * unless there's a non-empty file.
160 1.1 cgd */
161 1.1 cgd if (!output) {
162 1.1 cgd output = 1;
163 1.1 cgd for (cnt = 0; cnt < lp->cnt; ++cnt)
164 1.1 cgd if (ch = delim[cnt % delimcnt])
165 1.1 cgd putchar(ch);
166 1.1 cgd } else if (ch = delim[(lp->cnt - 1) % delimcnt])
167 1.1 cgd putchar(ch);
168 1.1 cgd (void)printf("%s", buf);
169 1.1 cgd }
170 1.1 cgd if (output)
171 1.1 cgd putchar('\n');
172 1.1 cgd }
173 1.1 cgd }
174 1.1 cgd
175 1.1 cgd sequential(argv)
176 1.1 cgd char **argv;
177 1.1 cgd {
178 1.1 cgd register FILE *fp;
179 1.1 cgd register int cnt;
180 1.1 cgd register char ch, *p, *dp;
181 1.1 cgd char buf[_POSIX2_LINE_MAX + 1];
182 1.1 cgd
183 1.1 cgd for (; p = *argv; ++argv) {
184 1.1 cgd if (p[0] == '-' && !p[1])
185 1.1 cgd fp = stdin;
186 1.1 cgd else if (!(fp = fopen(p, "r"))) {
187 1.1 cgd (void)fprintf(stderr, "paste: %s: %s.\n", p,
188 1.1 cgd strerror(errno));
189 1.1 cgd continue;
190 1.1 cgd }
191 1.1 cgd if (fgets(buf, sizeof(buf), fp)) {
192 1.1 cgd for (cnt = 0, dp = delim;;) {
193 1.1 cgd if (!(p = index(buf, '\n'))) {
194 1.1 cgd (void)fprintf(stderr,
195 1.1 cgd "paste: %s: input line too long.\n",
196 1.1 cgd *argv);
197 1.1 cgd exit(1);
198 1.1 cgd }
199 1.1 cgd *p = '\0';
200 1.1 cgd (void)printf("%s", buf);
201 1.1 cgd if (!fgets(buf, sizeof(buf), fp))
202 1.1 cgd break;
203 1.1 cgd if (ch = *dp++)
204 1.1 cgd putchar(ch);
205 1.1 cgd if (++cnt == delimcnt) {
206 1.1 cgd dp = delim;
207 1.1 cgd cnt = 0;
208 1.1 cgd }
209 1.1 cgd }
210 1.1 cgd putchar('\n');
211 1.1 cgd }
212 1.1 cgd if (fp != stdin)
213 1.1 cgd (void)fclose(fp);
214 1.1 cgd }
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd tr(arg)
218 1.1 cgd char *arg;
219 1.1 cgd {
220 1.1 cgd register int cnt;
221 1.1 cgd register char ch, *p;
222 1.1 cgd
223 1.1 cgd for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
224 1.1 cgd if (ch == '\\')
225 1.1 cgd switch(ch = *p++) {
226 1.1 cgd case 'n':
227 1.1 cgd *arg = '\n';
228 1.1 cgd break;
229 1.1 cgd case 't':
230 1.1 cgd *arg = '\t';
231 1.1 cgd break;
232 1.1 cgd case '0':
233 1.1 cgd *arg = '\0';
234 1.1 cgd break;
235 1.1 cgd default:
236 1.1 cgd *arg = ch;
237 1.1 cgd break;
238 1.1 cgd } else
239 1.1 cgd *arg = ch;
240 1.1 cgd
241 1.1 cgd if (!cnt) {
242 1.1 cgd (void)fprintf(stderr, "paste: no delimiters specified.\n");
243 1.1 cgd exit(1);
244 1.1 cgd }
245 1.1 cgd return(cnt);
246 1.1 cgd }
247 1.1 cgd
248 1.1 cgd usage()
249 1.1 cgd {
250 1.1 cgd (void)fprintf(stderr, "paste: [-s] [-d delimiters] file ...\n");
251 1.1 cgd exit(1);
252 1.1 cgd }
253