2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Adam S. Moskowitz of Menlo Consulting.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)paste.c 8.1 (Berkeley) 6/6/93";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/types.h>
60 static wchar_t *delim;
63 static int parallel(char **);
64 static int sequential(char **);
65 static int tr(wchar_t *);
66 static void usage(void);
68 static wchar_t tab[] = L"\t";
71 main(int argc, char *argv[])
78 setlocale(LC_CTYPE, "");
81 while ((ch = getopt(argc, argv, "d:s")) != -1)
85 len = mbsrtowcs(NULL, &arg, 0, NULL);
86 if (len == (size_t)-1)
88 warg = malloc((len + 1) * sizeof(*warg));
92 len = mbsrtowcs(warg, &arg, len + 1, NULL);
93 if (len == (size_t)-1)
95 delimcnt = tr(delim = warg);
115 rval = sequential(argv);
117 rval = parallel(argv);
121 typedef struct _list {
129 parallel(char **argv)
139 for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) {
140 if ((lp = malloc(sizeof(LIST))) == NULL)
142 if (p[0] == '-' && !p[1])
144 else if (!(lp->fp = fopen(p, "r")))
157 for (opencnt = cnt; opencnt;) {
158 for (output = 0, lp = head; lp; lp = lp->next) {
160 if (output && lp->cnt &&
161 (ch = delim[(lp->cnt - 1) % delimcnt]))
165 if ((ich = getwc(lp->fp)) == WEOF) {
169 if (output && lp->cnt &&
170 (ch = delim[(lp->cnt - 1) % delimcnt]))
175 * make sure that we don't print any delimiters
176 * unless there's a non-empty file.
180 for (cnt = 0; cnt < lp->cnt; ++cnt)
181 if ((ch = delim[cnt % delimcnt]))
183 } else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
189 } while ((ich = getwc(lp->fp)) != WEOF && ich != '\n');
199 sequential(char **argv)
202 int cnt, failed, needdelim;
207 for (; (p = *argv); ++argv) {
208 if (p[0] == '-' && !p[1])
210 else if (!(fp = fopen(p, "r"))) {
216 while ((ch = getwc(fp)) != WEOF) {
219 if (delim[cnt] != '\0')
220 putwchar(delim[cnt]);
221 if (++cnt == delimcnt)
235 return (failed != 0);
244 for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
263 errx(1, "no delimiters specified");
270 (void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");