18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1989, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes * Adam S. Moskowitz of Menlo Consulting.
99b50d902SRodney W. Grimes *
109b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes * are met:
139b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes * without specific prior written permission.
219b50d902SRodney W. Grimes *
229b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes * SUCH DAMAGE.
339b50d902SRodney W. Grimes */
349b50d902SRodney W. Grimes
359b50d902SRodney W. Grimes #include <sys/types.h>
3678552e79SMark Murray
371e133604SPhilippe Charnier #include <err.h>
38821df508SXin LI #include <errno.h>
39821df508SXin LI #include <limits.h>
400f10c7bbSTim J. Robbins #include <locale.h>
419b50d902SRodney W. Grimes #include <stdio.h>
42c93bd87aSJohn Birrell #include <stdlib.h>
43821df508SXin LI #include <string.h>
441e133604SPhilippe Charnier #include <unistd.h>
450f10c7bbSTim J. Robbins #include <wchar.h>
469b50d902SRodney W. Grimes
47e7b33384SEd Schouten static wchar_t *delim;
48e7b33384SEd Schouten static int delimcnt;
499b50d902SRodney W. Grimes
50e7b33384SEd Schouten static int parallel(char **);
51e7b33384SEd Schouten static int sequential(char **);
52e7b33384SEd Schouten static int tr(wchar_t *);
53*1a7ac2bdSAlfonso Gregory static void usage(void) __dead2;
541e133604SPhilippe Charnier
55e7b33384SEd Schouten static wchar_t tab[] = L"\t";
5678552e79SMark Murray
571e133604SPhilippe Charnier int
main(int argc,char * argv[])5878552e79SMark Murray main(int argc, char *argv[])
599b50d902SRodney W. Grimes {
600968654cSTim J. Robbins int ch, rval, seq;
610f10c7bbSTim J. Robbins wchar_t *warg;
620f10c7bbSTim J. Robbins const char *arg;
630f10c7bbSTim J. Robbins size_t len;
640f10c7bbSTim J. Robbins
650f10c7bbSTim J. Robbins setlocale(LC_CTYPE, "");
669b50d902SRodney W. Grimes
679b50d902SRodney W. Grimes seq = 0;
681c8af878SWarner Losh while ((ch = getopt(argc, argv, "d:s")) != -1)
699b50d902SRodney W. Grimes switch(ch) {
709b50d902SRodney W. Grimes case 'd':
710f10c7bbSTim J. Robbins arg = optarg;
720f10c7bbSTim J. Robbins len = mbsrtowcs(NULL, &arg, 0, NULL);
730f10c7bbSTim J. Robbins if (len == (size_t)-1)
740f10c7bbSTim J. Robbins err(1, "delimiters");
750f10c7bbSTim J. Robbins warg = malloc((len + 1) * sizeof(*warg));
760f10c7bbSTim J. Robbins if (warg == NULL)
770f10c7bbSTim J. Robbins err(1, NULL);
780f10c7bbSTim J. Robbins arg = optarg;
790f10c7bbSTim J. Robbins len = mbsrtowcs(warg, &arg, len + 1, NULL);
800f10c7bbSTim J. Robbins if (len == (size_t)-1)
810f10c7bbSTim J. Robbins err(1, "delimiters");
820f10c7bbSTim J. Robbins delimcnt = tr(delim = warg);
839b50d902SRodney W. Grimes break;
849b50d902SRodney W. Grimes case 's':
859b50d902SRodney W. Grimes seq = 1;
869b50d902SRodney W. Grimes break;
879b50d902SRodney W. Grimes case '?':
889b50d902SRodney W. Grimes default:
899b50d902SRodney W. Grimes usage();
909b50d902SRodney W. Grimes }
919b50d902SRodney W. Grimes argc -= optind;
929b50d902SRodney W. Grimes argv += optind;
939b50d902SRodney W. Grimes
94d345176dSMike Barcroft if (*argv == NULL)
95d345176dSMike Barcroft usage();
969b50d902SRodney W. Grimes if (!delim) {
979b50d902SRodney W. Grimes delimcnt = 1;
9878552e79SMark Murray delim = tab;
999b50d902SRodney W. Grimes }
1009b50d902SRodney W. Grimes
1019b50d902SRodney W. Grimes if (seq)
1020968654cSTim J. Robbins rval = sequential(argv);
1039b50d902SRodney W. Grimes else
1040968654cSTim J. Robbins rval = parallel(argv);
1050968654cSTim J. Robbins exit(rval);
1069b50d902SRodney W. Grimes }
1079b50d902SRodney W. Grimes
1089b50d902SRodney W. Grimes typedef struct _list {
1099b50d902SRodney W. Grimes struct _list *next;
1109b50d902SRodney W. Grimes FILE *fp;
1119b50d902SRodney W. Grimes int cnt;
1129b50d902SRodney W. Grimes char *name;
1139b50d902SRodney W. Grimes } LIST;
1149b50d902SRodney W. Grimes
115e7b33384SEd Schouten static int
parallel(char ** argv)11678552e79SMark Murray parallel(char **argv)
1179b50d902SRodney W. Grimes {
11878552e79SMark Murray LIST *lp;
119d4286259STim J. Robbins int cnt;
1200f10c7bbSTim J. Robbins wint_t ich;
1210f10c7bbSTim J. Robbins wchar_t ch;
1220f10c7bbSTim J. Robbins char *p;
1239b50d902SRodney W. Grimes LIST *head, *tmp;
1249b50d902SRodney W. Grimes int opencnt, output;
1259b50d902SRodney W. Grimes
126b2eeeae0SPhilippe Charnier for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) {
12747bca8b0SJuli Mallett if ((lp = malloc(sizeof(LIST))) == NULL)
12847bca8b0SJuli Mallett err(1, NULL);
1299b50d902SRodney W. Grimes if (p[0] == '-' && !p[1])
1309b50d902SRodney W. Grimes lp->fp = stdin;
1311e133604SPhilippe Charnier else if (!(lp->fp = fopen(p, "r")))
1321e133604SPhilippe Charnier err(1, "%s", p);
1339b50d902SRodney W. Grimes lp->next = NULL;
1349b50d902SRodney W. Grimes lp->cnt = cnt;
1359b50d902SRodney W. Grimes lp->name = p;
1369b50d902SRodney W. Grimes if (!head)
1379b50d902SRodney W. Grimes head = tmp = lp;
1389b50d902SRodney W. Grimes else {
1399b50d902SRodney W. Grimes tmp->next = lp;
1409b50d902SRodney W. Grimes tmp = lp;
1419b50d902SRodney W. Grimes }
1429b50d902SRodney W. Grimes }
1439b50d902SRodney W. Grimes
1449b50d902SRodney W. Grimes for (opencnt = cnt; opencnt;) {
1459b50d902SRodney W. Grimes for (output = 0, lp = head; lp; lp = lp->next) {
1469b50d902SRodney W. Grimes if (!lp->fp) {
1479b50d902SRodney W. Grimes if (output && lp->cnt &&
1489b50d902SRodney W. Grimes (ch = delim[(lp->cnt - 1) % delimcnt]))
1490f10c7bbSTim J. Robbins putwchar(ch);
1509b50d902SRodney W. Grimes continue;
1519b50d902SRodney W. Grimes }
1520f10c7bbSTim J. Robbins if ((ich = getwc(lp->fp)) == WEOF) {
1539b50d902SRodney W. Grimes if (!--opencnt)
1549b50d902SRodney W. Grimes break;
1559b50d902SRodney W. Grimes lp->fp = NULL;
1569b50d902SRodney W. Grimes if (output && lp->cnt &&
1579b50d902SRodney W. Grimes (ch = delim[(lp->cnt - 1) % delimcnt]))
1580f10c7bbSTim J. Robbins putwchar(ch);
1599b50d902SRodney W. Grimes continue;
1609b50d902SRodney W. Grimes }
1619b50d902SRodney W. Grimes /*
1629b50d902SRodney W. Grimes * make sure that we don't print any delimiters
1639b50d902SRodney W. Grimes * unless there's a non-empty file.
1649b50d902SRodney W. Grimes */
1659b50d902SRodney W. Grimes if (!output) {
1669b50d902SRodney W. Grimes output = 1;
1679b50d902SRodney W. Grimes for (cnt = 0; cnt < lp->cnt; ++cnt)
1681e133604SPhilippe Charnier if ((ch = delim[cnt % delimcnt]))
1690f10c7bbSTim J. Robbins putwchar(ch);
1701e133604SPhilippe Charnier } else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
1710f10c7bbSTim J. Robbins putwchar(ch);
1720f10c7bbSTim J. Robbins if (ich == '\n')
1730f10c7bbSTim J. Robbins continue;
1740f10c7bbSTim J. Robbins do {
1750f10c7bbSTim J. Robbins putwchar(ich);
1760f10c7bbSTim J. Robbins } while ((ich = getwc(lp->fp)) != WEOF && ich != '\n');
1779b50d902SRodney W. Grimes }
1789b50d902SRodney W. Grimes if (output)
1790f10c7bbSTim J. Robbins putwchar('\n');
1809b50d902SRodney W. Grimes }
1810968654cSTim J. Robbins
1820968654cSTim J. Robbins return (0);
1839b50d902SRodney W. Grimes }
1849b50d902SRodney W. Grimes
185e7b33384SEd Schouten static int
sequential(char ** argv)18678552e79SMark Murray sequential(char **argv)
1879b50d902SRodney W. Grimes {
18878552e79SMark Murray FILE *fp;
189d4286259STim J. Robbins int cnt, failed, needdelim;
1900f10c7bbSTim J. Robbins wint_t ch;
1910f10c7bbSTim J. Robbins char *p;
1929b50d902SRodney W. Grimes
1930968654cSTim J. Robbins failed = 0;
1941e133604SPhilippe Charnier for (; (p = *argv); ++argv) {
1959b50d902SRodney W. Grimes if (p[0] == '-' && !p[1])
1969b50d902SRodney W. Grimes fp = stdin;
1979b50d902SRodney W. Grimes else if (!(fp = fopen(p, "r"))) {
1981e133604SPhilippe Charnier warn("%s", p);
1990968654cSTim J. Robbins failed = 1;
2009b50d902SRodney W. Grimes continue;
2019b50d902SRodney W. Grimes }
202ce78cbf9STim J. Robbins cnt = needdelim = 0;
2030f10c7bbSTim J. Robbins while ((ch = getwc(fp)) != WEOF) {
204ce78cbf9STim J. Robbins if (needdelim) {
205ce78cbf9STim J. Robbins needdelim = 0;
206ce78cbf9STim J. Robbins if (delim[cnt] != '\0')
2070f10c7bbSTim J. Robbins putwchar(delim[cnt]);
208ce78cbf9STim J. Robbins if (++cnt == delimcnt)
2099b50d902SRodney W. Grimes cnt = 0;
2109b50d902SRodney W. Grimes }
2110f10c7bbSTim J. Robbins if (ch != '\n')
2120f10c7bbSTim J. Robbins putwchar(ch);
2130f10c7bbSTim J. Robbins else
214ce78cbf9STim J. Robbins needdelim = 1;
2159b50d902SRodney W. Grimes }
216ce78cbf9STim J. Robbins if (needdelim)
2170f10c7bbSTim J. Robbins putwchar('\n');
2189b50d902SRodney W. Grimes if (fp != stdin)
2199b50d902SRodney W. Grimes (void)fclose(fp);
2209b50d902SRodney W. Grimes }
2210968654cSTim J. Robbins
2220968654cSTim J. Robbins return (failed != 0);
2239b50d902SRodney W. Grimes }
2249b50d902SRodney W. Grimes
225e7b33384SEd Schouten static int
tr(wchar_t * arg)2260f10c7bbSTim J. Robbins tr(wchar_t *arg)
2279b50d902SRodney W. Grimes {
22878552e79SMark Murray int cnt;
2290f10c7bbSTim J. Robbins wchar_t ch, *p;
2309b50d902SRodney W. Grimes
2319b50d902SRodney W. Grimes for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt)
2329b50d902SRodney W. Grimes if (ch == '\\')
2339b50d902SRodney W. Grimes switch(ch = *p++) {
2349b50d902SRodney W. Grimes case 'n':
2359b50d902SRodney W. Grimes *arg = '\n';
2369b50d902SRodney W. Grimes break;
2379b50d902SRodney W. Grimes case 't':
2389b50d902SRodney W. Grimes *arg = '\t';
2399b50d902SRodney W. Grimes break;
2409b50d902SRodney W. Grimes case '0':
2419b50d902SRodney W. Grimes *arg = '\0';
2429b50d902SRodney W. Grimes break;
2439b50d902SRodney W. Grimes default:
2449b50d902SRodney W. Grimes *arg = ch;
2459b50d902SRodney W. Grimes break;
2469b50d902SRodney W. Grimes } else
2479b50d902SRodney W. Grimes *arg = ch;
2489b50d902SRodney W. Grimes
2491e133604SPhilippe Charnier if (!cnt)
2501e133604SPhilippe Charnier errx(1, "no delimiters specified");
2519b50d902SRodney W. Grimes return(cnt);
2529b50d902SRodney W. Grimes }
2539b50d902SRodney W. Grimes
2541e133604SPhilippe Charnier static void
usage(void)25578552e79SMark Murray usage(void)
2569b50d902SRodney W. Grimes {
2571e133604SPhilippe Charnier (void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");
2589b50d902SRodney W. Grimes exit(1);
2599b50d902SRodney W. Grimes }
260