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