xref: /freebsd/usr.bin/paste/paste.c (revision 32cd3ee5901ea33d41ff550e5f40ce743c8d4165)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  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/types.h>
36 #include <sys/queue.h>
37 #include <sys/capsicum.h>
38 
39 #include <capsicum_helpers.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <locale.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <wchar.h>
50 
51 #include <libcasper.h>
52 #include <casper/cap_fileargs.h>
53 
54 static wchar_t *delim;
55 static int delimcnt;
56 
57 static int parallel(char **, fileargs_t *);
58 static int sequential(char **, fileargs_t *);
59 static int tr(wchar_t *);
60 static void usage(void) __dead2;
61 
62 static wchar_t tab[] = L"\t";
63 
64 int
65 main(int argc, char *argv[])
66 {
67 	int ch, rval, seq;
68 	wchar_t *warg;
69 	const char *arg;
70 	size_t len;
71 	fileargs_t *fa;
72 	cap_rights_t rights;
73 
74 	setlocale(LC_CTYPE, "");
75 
76 	seq = 0;
77 	while ((ch = getopt(argc, argv, "d:s")) != -1)
78 		switch(ch) {
79 		case 'd':
80 			arg = optarg;
81 			len = mbsrtowcs(NULL, &arg, 0, NULL);
82 			if (len == (size_t)-1)
83 				err(1, "delimiters");
84 			warg = malloc((len + 1) * sizeof(*warg));
85 			if (warg == NULL)
86 				err(1, NULL);
87 			arg = optarg;
88 			len = mbsrtowcs(warg, &arg, len + 1, NULL);
89 			if (len == (size_t)-1)
90 				err(1, "delimiters");
91 			delimcnt = tr(delim = warg);
92 			break;
93 		case 's':
94 			seq = 1;
95 			break;
96 		case '?':
97 		default:
98 			usage();
99 		}
100 	argc -= optind;
101 	argv += optind;
102 
103 	if (*argv == NULL)
104 		usage();
105 	if (!delim) {
106 		delimcnt = 1;
107 		delim = tab;
108 	}
109 
110 	fa = fileargs_init(argc, argv, O_RDONLY, 0,
111 	    cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_FCNTL), FA_OPEN);
112 	if (fa == NULL)
113 		err(1, "unable to open system.fileargs service");
114 
115 	caph_cache_catpages();
116 	if (caph_enter_casper() < 0)
117 		err(1, "unable to enter capability mode");
118 
119 	rval = seq ? sequential(argv, fa) : parallel(argv, fa);
120 
121 	fileargs_free(fa);
122 	exit(rval);
123 }
124 
125 typedef struct _list {
126 	STAILQ_ENTRY(_list) entries;
127 	FILE *fp;
128 	int cnt;
129 	char *name;
130 } LIST;
131 
132 static STAILQ_HEAD(head, _list) lh;
133 
134 static int
135 parallel(char **argv, fileargs_t *fa)
136 {
137 	LIST *lp;
138 	int cnt;
139 	wint_t ich;
140 	wchar_t ch;
141 	char *p;
142 	int opencnt, output;
143 
144 	STAILQ_INIT(&lh);
145 
146 	for (cnt = 0; (p = *argv); ++argv, ++cnt) {
147 		if ((lp = malloc(sizeof(LIST))) == NULL)
148 			err(1, NULL);
149 		if (p[0] == '-' && !p[1])
150 			lp->fp = stdin;
151 		else if (!(lp->fp = fileargs_fopen(fa, p, "r")))
152 			err(1, "%s", p);
153 		lp->cnt = cnt;
154 		lp->name = p;
155 
156 		STAILQ_INSERT_TAIL(&lh, lp, entries);
157 	}
158 
159 	for (opencnt = cnt; opencnt;) {
160 		output = 0;
161 		STAILQ_FOREACH(lp, &lh, entries) {
162 			if (!lp->fp) {
163 				if (output && lp->cnt &&
164 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
165 					putwchar(ch);
166 				continue;
167 			}
168 			if ((ich = getwc(lp->fp)) == WEOF) {
169 				if (!--opencnt)
170 					break;
171 				lp->fp = NULL;
172 				if (output && lp->cnt &&
173 				    (ch = delim[(lp->cnt - 1) % delimcnt]))
174 					putwchar(ch);
175 				continue;
176 			}
177 			/*
178 			 * make sure that we don't print any delimiters
179 			 * unless there's a non-empty file.
180 			 */
181 			if (!output) {
182 				output = 1;
183 				for (cnt = 0; cnt < lp->cnt; ++cnt)
184 					if ((ch = delim[cnt % delimcnt]))
185 						putwchar(ch);
186 			} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
187 				putwchar(ch);
188 			if (ich == '\n')
189 				continue;
190 			do {
191 				putwchar(ich);
192 			} while ((ich = getwc(lp->fp)) != WEOF && ich != '\n');
193 		}
194 		if (output)
195 			putwchar('\n');
196 	}
197 
198 	return (0);
199 }
200 
201 static int
202 sequential(char **argv, fileargs_t *fa)
203 {
204 	FILE *fp;
205 	int cnt, failed, needdelim;
206 	wint_t ch;
207 	char *p;
208 
209 	failed = 0;
210 	for (; (p = *argv); ++argv) {
211 		if (p[0] == '-' && !p[1])
212 			fp = stdin;
213 		else if (!(fp = fileargs_fopen(fa, p, "r"))) {
214 			warn("%s", p);
215 			failed = 1;
216 			continue;
217 		}
218 		cnt = needdelim = 0;
219 		while ((ch = getwc(fp)) != WEOF) {
220 			if (needdelim) {
221 				needdelim = 0;
222 				if (delim[cnt] != '\0')
223 					putwchar(delim[cnt]);
224 				if (++cnt == delimcnt)
225 					cnt = 0;
226 			}
227 			if (ch != '\n')
228 				putwchar(ch);
229 			else
230 				needdelim = 1;
231 		}
232 		if (needdelim)
233 			putwchar('\n');
234 		if (fp != stdin)
235 			(void)fclose(fp);
236 	}
237 
238 	return (failed != 0);
239 }
240 
241 static int
242 tr(wchar_t *arg)
243 {
244 	int cnt;
245 	wchar_t ch, *p;
246 
247 	for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
248 		if (ch == '\\')
249 			switch(ch = *p++) {
250 			case 'n':
251 				*arg = '\n';
252 				break;
253 			case 't':
254 				*arg = '\t';
255 				break;
256 			case '0':
257 				*arg = '\0';
258 				break;
259 			default:
260 				*arg = ch;
261 				break;
262 			}
263 		else
264 			*arg = ch;
265 
266 	if (!cnt)
267 		errx(1, "no delimiters specified");
268 	return(cnt);
269 }
270 
271 static void
272 usage(void)
273 {
274 	(void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");
275 	exit(1);
276 }
277