xref: /netbsd-src/usr.bin/paste/paste.c (revision f085baab1a6ad97d40f52ec17221e326ddc1c578)
1*f085baabSjoerg /*	$NetBSD: paste.c,v 1.16 2011/09/06 18:24:43 joerg Exp $	*/
25445d018Stls 
361f28255Scgd /*
45445d018Stls  * Copyright (c) 1989, 1993
55445d018Stls  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Adam S. Moskowitz of Menlo Consulting.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
1889aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
359b5a69b5Slukem #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
3798e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
3898e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #ifndef lint
425445d018Stls /*static char sccsid[] = "from: @(#)paste.c	8.1 (Berkeley) 6/6/93";*/
43*f085baabSjoerg __RCSID("$NetBSD: paste.c,v 1.16 2011/09/06 18:24:43 joerg Exp $");
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd #include <sys/types.h>
479b5a69b5Slukem #include <err.h>
4861f28255Scgd #include <errno.h>
4961f28255Scgd #include <limits.h>
5061f28255Scgd #include <stdio.h>
519b5a69b5Slukem #include <stdlib.h>
5261f28255Scgd #include <string.h>
539f66981aSperry #include <unistd.h>
5461f28255Scgd 
55*f085baabSjoerg static void	parallel(int, char **);
56*f085baabSjoerg static void	sequential(char **);
57*f085baabSjoerg static int	tr(char *);
58*f085baabSjoerg __dead static void	usage(void);
599b5a69b5Slukem 
60*f085baabSjoerg static char dflt_delim[] = "\t";
61*f085baabSjoerg static char *delim = dflt_delim;
62*f085baabSjoerg static int delimcnt = 1;
6361f28255Scgd 
649b5a69b5Slukem int
main(int argc,char ** argv)650720992dSdsl main(int argc, char **argv)
6661f28255Scgd {
6761f28255Scgd 	int ch, seq;
6861f28255Scgd 
6961f28255Scgd 	seq = 0;
70ec40cf92Sdsl 	while ((ch = getopt(argc, argv, "d:s")) != -1) {
7161f28255Scgd 		switch (ch) {
7261f28255Scgd 		case 'd':
73ec40cf92Sdsl 			delim = strdup(optarg);
74ec40cf92Sdsl 			delimcnt = tr(delim);
7561f28255Scgd 			break;
7661f28255Scgd 		case 's':
7761f28255Scgd 			seq = 1;
7861f28255Scgd 			break;
7961f28255Scgd 		case '?':
8061f28255Scgd 		default:
8161f28255Scgd 			usage();
8261f28255Scgd 		}
83ec40cf92Sdsl 	}
8461f28255Scgd 	argc -= optind;
8561f28255Scgd 	argv += optind;
8661f28255Scgd 
8761f28255Scgd 	if (seq)
8861f28255Scgd 		sequential(argv);
8961f28255Scgd 	else
90ec40cf92Sdsl 		parallel(argc, argv);
9161f28255Scgd 	exit(0);
9261f28255Scgd }
9361f28255Scgd 
94*f085baabSjoerg static void
parallel(int argc,char ** argv)95ec40cf92Sdsl parallel(int argc, char **argv)
9661f28255Scgd {
97ec40cf92Sdsl 	char ch, *dp, *line;
98ec40cf92Sdsl 	FILE **fpp, *fp;
99ec40cf92Sdsl 	size_t line_len;
100ec40cf92Sdsl 	int cnt, output;
10161f28255Scgd 
102ec40cf92Sdsl 	fpp = calloc(argc, sizeof *fpp);
103ec40cf92Sdsl 	if (fpp == NULL)
104ec40cf92Sdsl 		err(1, "calloc");
105ec40cf92Sdsl 
106ec40cf92Sdsl 	for (cnt = 0; cnt < argc; cnt++) {
107ec40cf92Sdsl 		if (strcmp(argv[cnt], "-") == 0)
108ec40cf92Sdsl 			fpp[cnt] = stdin;
109ec40cf92Sdsl 		else if (!(fpp[cnt] = fopen(argv[cnt], "r")))
110ec40cf92Sdsl 			err(1, "%s", argv[cnt]);
11161f28255Scgd 	}
11261f28255Scgd 
113ec40cf92Sdsl 	for (;;) {
114ec40cf92Sdsl 		/* Start with the NUL at the end of 'delim' ... */
115ec40cf92Sdsl 		dp = delim + delimcnt;
116ec40cf92Sdsl 		output = 0;
117ec40cf92Sdsl 		for (cnt = 0; cnt < argc; cnt++) {
118ec40cf92Sdsl 			fp = fpp[cnt];
119ec40cf92Sdsl 			if (fp == NULL)
120ec40cf92Sdsl 				continue;
121ec40cf92Sdsl 			line = fgetln(fp, &line_len);
122ec40cf92Sdsl 			if (line == NULL) {
123ec40cf92Sdsl 				/* Assume EOF */
124ec40cf92Sdsl 				if (fp != stdin)
125ec40cf92Sdsl 					fclose(fp);
126ec40cf92Sdsl 				fpp[cnt] = NULL;
12761f28255Scgd 				continue;
12861f28255Scgd 			}
129ec40cf92Sdsl 			/* Output enough separators to catch up */
130ec40cf92Sdsl 			do {
131ec40cf92Sdsl 				ch = *dp++;
132ec40cf92Sdsl 				if (ch)
133ec40cf92Sdsl 					putchar(ch);
134ec40cf92Sdsl 				if (dp >= delim + delimcnt)
135ec40cf92Sdsl 					dp = delim;
136ec40cf92Sdsl 			} while (++output <= cnt);
137ec40cf92Sdsl 			/* Remove any trailing newline - check for last line */
138ec40cf92Sdsl 			if (line[line_len - 1] == '\n')
139ec40cf92Sdsl 				line_len--;
14075de0341Srtr 			printf("%.*s", (int)line_len, line);
141ec40cf92Sdsl 		}
142ec40cf92Sdsl 
143ec40cf92Sdsl 		if (!output)
14461f28255Scgd 			break;
145ec40cf92Sdsl 
146ec40cf92Sdsl 		/* Add separators to end of line */
147ec40cf92Sdsl 		while (++output <= cnt) {
148ec40cf92Sdsl 			ch = *dp++;
149ec40cf92Sdsl 			if (ch)
15061f28255Scgd 				putchar(ch);
151ec40cf92Sdsl 			if (dp >= delim + delimcnt)
152ec40cf92Sdsl 				dp = delim;
15361f28255Scgd 		}
15461f28255Scgd 		putchar('\n');
15561f28255Scgd 	}
156ec40cf92Sdsl 
157ec40cf92Sdsl 	free(fpp);
15861f28255Scgd }
15961f28255Scgd 
160*f085baabSjoerg static void
sequential(char ** argv)1610720992dSdsl sequential(char **argv)
16261f28255Scgd {
1639b5a69b5Slukem 	FILE *fp;
1649b5a69b5Slukem 	int cnt;
1659b5a69b5Slukem 	char ch, *p, *dp;
16661f28255Scgd 	char buf[_POSIX2_LINE_MAX + 1];
16761f28255Scgd 
1689b5a69b5Slukem 	for (; (p = *argv) != NULL; ++argv) {
16961f28255Scgd 		if (p[0] == '-' && !p[1])
17061f28255Scgd 			fp = stdin;
17161f28255Scgd 		else if (!(fp = fopen(p, "r"))) {
1729b5a69b5Slukem 			warn("%s", p);
17361f28255Scgd 			continue;
17461f28255Scgd 		}
17561f28255Scgd 		if (fgets(buf, sizeof(buf), fp)) {
17661f28255Scgd 			for (cnt = 0, dp = delim;;) {
1779b5a69b5Slukem 				if (!(p = strchr(buf, '\n')))
1789b5a69b5Slukem 					err(1, "%s: input line too long.",
17961f28255Scgd 					    *argv);
18061f28255Scgd 				*p = '\0';
18161f28255Scgd 				(void)printf("%s", buf);
18261f28255Scgd 				if (!fgets(buf, sizeof(buf), fp))
18361f28255Scgd 					break;
1849b5a69b5Slukem 				if ((ch = *dp++) != 0)
18561f28255Scgd 					putchar(ch);
18661f28255Scgd 				if (++cnt == delimcnt) {
18761f28255Scgd 					dp = delim;
18861f28255Scgd 					cnt = 0;
18961f28255Scgd 				}
19061f28255Scgd 			}
19161f28255Scgd 			putchar('\n');
19261f28255Scgd 		}
19361f28255Scgd 		if (fp != stdin)
19461f28255Scgd 			(void)fclose(fp);
19561f28255Scgd 	}
19661f28255Scgd }
19761f28255Scgd 
198*f085baabSjoerg static int
tr(char * arg)1990720992dSdsl tr(char *arg)
20061f28255Scgd {
2019b5a69b5Slukem 	int cnt;
2029b5a69b5Slukem 	char ch, *p;
20361f28255Scgd 
20461f28255Scgd 	for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
20561f28255Scgd 		if (ch == '\\')
20661f28255Scgd 			switch(ch = *p++) {
20761f28255Scgd 			case 'n':
20861f28255Scgd 				*arg = '\n';
20961f28255Scgd 				break;
21061f28255Scgd 			case 't':
21161f28255Scgd 				*arg = '\t';
21261f28255Scgd 				break;
21361f28255Scgd 			case '0':
21461f28255Scgd 				*arg = '\0';
21561f28255Scgd 				break;
21661f28255Scgd 			default:
21761f28255Scgd 				*arg = ch;
21861f28255Scgd 				break;
21961f28255Scgd 		} else
22061f28255Scgd 			*arg = ch;
22161f28255Scgd 
2229b5a69b5Slukem 	if (!cnt)
2239b5a69b5Slukem 		errx(1, "no delimiters specified.");
2241a6bc86aSdholland 	*arg = '\0';
22561f28255Scgd 	return(cnt);
22661f28255Scgd }
22761f28255Scgd 
228*f085baabSjoerg static void
usage(void)229*f085baabSjoerg usage(void)
23061f28255Scgd {
23161f28255Scgd 	(void)fprintf(stderr, "paste: [-s] [-d delimiters] file ...\n");
23261f28255Scgd 	exit(1);
23361f28255Scgd }
234