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