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