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