xref: /dragonfly/usr.bin/paste/paste.c (revision 36a3d1d6)
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.3 2004/12/15 23:11:06 cpressey 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 	tmp = NULL;
115 	for (cnt = 0, head = NULL; (p = *argv); ++argv, ++cnt) {
116 		if ((lp = malloc(sizeof(LIST))) == NULL)
117 			err(1, NULL);
118 		if (p[0] == '-' && !p[1])
119 			lp->fp = stdin;
120 		else if (!(lp->fp = fopen(p, "r")))
121 			err(1, "%s", p);
122 		lp->next = NULL;
123 		lp->cnt = cnt;
124 		lp->name = p;
125 		if (!head)
126 			head = tmp = lp;
127 		else {
128 			tmp->next = lp;
129 			tmp = lp;
130 		}
131 	}
132 
133 	for (opencnt = cnt; opencnt;) {
134 		for (output = 0, lp = head; lp; lp = lp->next) {
135 			if (!lp->fp) {
136 				if (output && lp->cnt &&
137 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
138 					putchar(ch);
139 				continue;
140 			}
141 			if ((buf = fgetln(lp->fp, &len)) == NULL) {
142 				if (!--opencnt)
143 					break;
144 				lp->fp = NULL;
145 				if (output && lp->cnt &&
146 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
147 					putchar(ch);
148 				continue;
149 			}
150 			/*
151 			 * make sure that we don't print any delimiters
152 			 * unless there's a non-empty file.
153 			 */
154 			if (!output) {
155 				output = 1;
156 				for (cnt = 0; cnt < lp->cnt; ++cnt)
157 					if ((ch = delim[cnt % delimcnt]))
158 						putchar(ch);
159 			} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
160 				putchar(ch);
161 			if (buf[len - 1] == '\n')
162 				len--;
163 			fwrite(buf, 1, len, stdout);
164 		}
165 		if (output)
166 			putchar('\n');
167 	}
168 
169 	return (0);
170 }
171 
172 int
173 sequential(char **argv)
174 {
175 	FILE *fp;
176 	int cnt, failed, needdelim;
177 	char *buf, *p;
178 	size_t len;
179 
180 	failed = 0;
181 	for (; (p = *argv); ++argv) {
182 		if (p[0] == '-' && !p[1])
183 			fp = stdin;
184 		else if (!(fp = fopen(p, "r"))) {
185 			warn("%s", p);
186 			failed = 1;
187 			continue;
188 		}
189 		cnt = needdelim = 0;
190 		while ((buf = fgetln(fp, &len)) != NULL) {
191 			if (needdelim) {
192 				needdelim = 0;
193 				if (delim[cnt] != '\0')
194 					putchar(delim[cnt]);
195 				if (++cnt == delimcnt)
196 					cnt = 0;
197 			}
198 			if (buf[len - 1] == '\n')
199 				len--;
200 			fwrite(buf, 1, len, stdout);
201 			needdelim = 1;
202 		}
203 		if (needdelim)
204 			putchar('\n');
205 		if (fp != stdin)
206 			(void)fclose(fp);
207 	}
208 
209 	return (failed != 0);
210 }
211 
212 int
213 tr(char *arg)
214 {
215 	int cnt;
216 	char ch, *p;
217 
218 	for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
219 		if (ch == '\\')
220 			switch(ch = *p++) {
221 			case 'n':
222 				*arg = '\n';
223 				break;
224 			case 't':
225 				*arg = '\t';
226 				break;
227 			case '0':
228 				*arg = '\0';
229 				break;
230 			default:
231 				*arg = ch;
232 				break;
233 		} else
234 			*arg = ch;
235 
236 	if (!cnt)
237 		errx(1, "no delimiters specified");
238 	return(cnt);
239 }
240 
241 static void
242 usage(void)
243 {
244 	(void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");
245 	exit(1);
246 }
247