xref: /openbsd-src/usr.bin/paste/paste.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /*	$OpenBSD: paste.c,v 1.23 2018/01/02 06:56:41 guenther 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 	if (pledge("stdio rpath", NULL) == -1)
61 		err(1, "pledge");
62 
63 	seq = 0;
64 	while ((ch = getopt(argc, argv, "d:s")) != -1) {
65 		switch (ch) {
66 		case 'd':
67 			delimcnt = tr(delim = optarg);
68 			break;
69 		case 's':
70 			seq = 1;
71 			break;
72 		case '?':
73 		default:
74 			usage();
75 		}
76 	}
77 	argc -= optind;
78 	argv += optind;
79 
80 	if (argc == 0)
81 		usage();
82 
83 	if (!delim) {
84 		delimcnt = 1;
85 		delim = "\t";
86 	}
87 
88 	if (seq)
89 		sequential(argv);
90 	else
91 		parallel(argv);
92 	exit(0);
93 }
94 
95 struct list {
96 	SIMPLEQ_ENTRY(list) entries;
97 	FILE *fp;
98 	int cnt;
99 	char *name;
100 };
101 
102 void
103 parallel(char **argv)
104 {
105 	SIMPLEQ_HEAD(, list) head = SIMPLEQ_HEAD_INITIALIZER(head);
106 	struct list *lp;
107 	int cnt;
108 	char ch, *p;
109 	int opencnt, output;
110 	char *buf, *lbuf;
111 	size_t len;
112 
113 	for (cnt = 0; (p = *argv); ++argv, ++cnt) {
114 		if (!(lp = malloc(sizeof(struct list))))
115 			err(1, "malloc");
116 
117 		if (p[0] == '-' && !p[1])
118 			lp->fp = stdin;
119 		else if (!(lp->fp = fopen(p, "r")))
120 			err(1, "%s", p);
121 		lp->cnt = cnt;
122 		lp->name = p;
123 		SIMPLEQ_INSERT_TAIL(&head, lp, entries);
124 	}
125 
126 	for (opencnt = cnt; opencnt;) {
127 		output = 0;
128 		SIMPLEQ_FOREACH(lp, &head, entries) {
129 			lbuf = NULL;
130 			if (!lp->fp) {
131 				if (output && lp->cnt &&
132 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
133 					putchar(ch);
134 				continue;
135 			}
136 			if (!(buf = fgetln(lp->fp, &len))) {
137 				if (!--opencnt)
138 					break;
139 				if (lp->fp != stdin)
140 					(void)fclose(lp->fp);
141 				lp->fp = NULL;
142 				if (output && lp->cnt &&
143 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
144 					putchar(ch);
145 				continue;
146 			}
147 			if (buf[len - 1] == '\n')
148 				buf[len - 1] = '\0';
149 			else {
150 				if ((lbuf = malloc(len + 1)) == NULL)
151 					err(1, "malloc");
152 				memcpy(lbuf, buf, len);
153 				lbuf[len] = '\0';
154 				buf = lbuf;
155 			}
156 			/*
157 			 * make sure that we don't print any delimiters
158 			 * unless there's a non-empty file.
159 			 */
160 			if (!output) {
161 				output = 1;
162 				for (cnt = 0; cnt < lp->cnt; ++cnt)
163 					if ((ch = delim[cnt % delimcnt]))
164 						putchar(ch);
165 			} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
166 				putchar(ch);
167 			(void)printf("%s", buf);
168 			if (lbuf)
169 				free(lbuf);
170 		}
171 		if (output)
172 			putchar('\n');
173 	}
174 }
175 
176 void
177 sequential(char **argv)
178 {
179 	FILE *fp;
180 	int cnt;
181 	char ch, *p, *dp;
182 	char *buf, *lbuf;
183 	size_t len;
184 
185 	for (; (p = *argv); ++argv) {
186 		lbuf = NULL;
187 		if (p[0] == '-' && !p[1])
188 			fp = stdin;
189 		else if (!(fp = fopen(p, "r"))) {
190 			warn("%s", p);
191 			continue;
192 		}
193 		if ((buf = fgetln(fp, &len))) {
194 			for (cnt = 0, dp = delim;;) {
195 				if (buf[len - 1] == '\n')
196 					buf[len - 1] = '\0';
197 				else {
198 					if ((lbuf = malloc(len + 1)) == NULL)
199 						err(1, "malloc");
200 					memcpy(lbuf, buf, len);
201 					lbuf[len] = '\0';
202 					buf = lbuf;
203 				}
204 				(void)printf("%s", buf);
205 				if (!(buf = fgetln(fp, &len)))
206 					break;
207 				if ((ch = *dp++))
208 					putchar(ch);
209 				if (++cnt == delimcnt) {
210 					dp = delim;
211 					cnt = 0;
212 				}
213 			}
214 			putchar('\n');
215 		}
216 		if (fp != stdin)
217 			(void)fclose(fp);
218 		free(lbuf);
219 	}
220 }
221 
222 int
223 tr(char *arg)
224 {
225 	int cnt;
226 	char ch, *p;
227 
228 	for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt) {
229 		if (ch == '\\') {
230 			switch (ch = *p++) {
231 			case 'n':
232 				*arg = '\n';
233 				break;
234 			case 't':
235 				*arg = '\t';
236 				break;
237 			case '0':
238 				*arg = '\0';
239 				break;
240 			default:
241 				*arg = ch;
242 				break;
243 			}
244 		} else
245 			*arg = ch;
246 	}
247 
248 	if (!cnt)
249 		errx(1, "no delimiters specified");
250 	return (cnt);
251 }
252 
253 void
254 usage(void)
255 {
256 	extern char *__progname;
257 	(void)fprintf(stderr, "usage: %s [-s] [-d list] file ...\n",
258 	    __progname);
259 	exit(1);
260 }
261