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 #ifndef lint 361e133604SPhilippe Charnier static const char copyright[] = 379b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 389b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 399b50d902SRodney W. Grimes #endif /* not lint */ 409b50d902SRodney W. Grimes 411e133604SPhilippe Charnier #if 0 42d345176dSMike Barcroft #ifndef lint 439b50d902SRodney W. Grimes static char sccsid[] = "@(#)paste.c 8.1 (Berkeley) 6/6/93"; 449b50d902SRodney W. Grimes #endif /* not lint */ 45d345176dSMike Barcroft #endif 46d345176dSMike Barcroft 47d345176dSMike Barcroft #include <sys/cdefs.h> 48d345176dSMike Barcroft __FBSDID("$FreeBSD$"); 499b50d902SRodney W. Grimes 509b50d902SRodney W. Grimes #include <sys/types.h> 5178552e79SMark Murray 521e133604SPhilippe Charnier #include <err.h> 53821df508SXin LI #include <errno.h> 54821df508SXin LI #include <limits.h> 550f10c7bbSTim J. Robbins #include <locale.h> 569b50d902SRodney W. Grimes #include <stdio.h> 57c93bd87aSJohn Birrell #include <stdlib.h> 58821df508SXin LI #include <string.h> 591e133604SPhilippe Charnier #include <unistd.h> 600f10c7bbSTim J. Robbins #include <wchar.h> 619b50d902SRodney W. Grimes 62e7b33384SEd Schouten static wchar_t *delim; 63e7b33384SEd Schouten static int delimcnt; 649b50d902SRodney W. Grimes 65e7b33384SEd Schouten static int parallel(char **); 66e7b33384SEd Schouten static int sequential(char **); 67e7b33384SEd Schouten static int tr(wchar_t *); 681a7ac2bdSAlfonso Gregory static void usage(void) __dead2; 691e133604SPhilippe Charnier 70e7b33384SEd Schouten static wchar_t tab[] = L"\t"; 7178552e79SMark Murray 721e133604SPhilippe Charnier int 7378552e79SMark Murray main(int argc, char *argv[]) 749b50d902SRodney W. Grimes { 750968654cSTim J. Robbins int ch, rval, seq; 760f10c7bbSTim J. Robbins wchar_t *warg; 770f10c7bbSTim J. Robbins const char *arg; 780f10c7bbSTim J. Robbins size_t len; 790f10c7bbSTim J. Robbins 800f10c7bbSTim J. Robbins setlocale(LC_CTYPE, ""); 819b50d902SRodney W. Grimes 829b50d902SRodney W. Grimes seq = 0; 831c8af878SWarner Losh while ((ch = getopt(argc, argv, "d:s")) != -1) 849b50d902SRodney W. Grimes switch(ch) { 859b50d902SRodney W. Grimes case 'd': 860f10c7bbSTim J. Robbins arg = optarg; 870f10c7bbSTim J. Robbins len = mbsrtowcs(NULL, &arg, 0, NULL); 880f10c7bbSTim J. Robbins if (len == (size_t)-1) 890f10c7bbSTim J. Robbins err(1, "delimiters"); 900f10c7bbSTim J. Robbins warg = malloc((len + 1) * sizeof(*warg)); 910f10c7bbSTim J. Robbins if (warg == NULL) 920f10c7bbSTim J. Robbins err(1, NULL); 930f10c7bbSTim J. Robbins arg = optarg; 940f10c7bbSTim J. Robbins len = mbsrtowcs(warg, &arg, len + 1, NULL); 950f10c7bbSTim J. Robbins if (len == (size_t)-1) 960f10c7bbSTim J. Robbins err(1, "delimiters"); 970f10c7bbSTim J. Robbins delimcnt = tr(delim = warg); 989b50d902SRodney W. Grimes break; 999b50d902SRodney W. Grimes case 's': 1009b50d902SRodney W. Grimes seq = 1; 1019b50d902SRodney W. Grimes break; 1029b50d902SRodney W. Grimes case '?': 1039b50d902SRodney W. Grimes default: 1049b50d902SRodney W. Grimes usage(); 1059b50d902SRodney W. Grimes } 1069b50d902SRodney W. Grimes argc -= optind; 1079b50d902SRodney W. Grimes argv += optind; 1089b50d902SRodney W. Grimes 109d345176dSMike Barcroft if (*argv == NULL) 110d345176dSMike Barcroft usage(); 1119b50d902SRodney W. Grimes if (!delim) { 1129b50d902SRodney W. Grimes delimcnt = 1; 11378552e79SMark Murray delim = tab; 1149b50d902SRodney W. Grimes } 1159b50d902SRodney W. Grimes 1169b50d902SRodney W. Grimes if (seq) 1170968654cSTim J. Robbins rval = sequential(argv); 1189b50d902SRodney W. Grimes else 1190968654cSTim J. Robbins rval = parallel(argv); 1200968654cSTim J. Robbins exit(rval); 1219b50d902SRodney W. Grimes } 1229b50d902SRodney W. Grimes 1239b50d902SRodney W. Grimes typedef struct _list { 1249b50d902SRodney W. Grimes struct _list *next; 1259b50d902SRodney W. Grimes FILE *fp; 1269b50d902SRodney W. Grimes int cnt; 1279b50d902SRodney W. Grimes char *name; 1289b50d902SRodney W. Grimes } LIST; 1299b50d902SRodney W. Grimes 130e7b33384SEd Schouten static int 13178552e79SMark Murray parallel(char **argv) 1329b50d902SRodney W. Grimes { 13378552e79SMark Murray LIST *lp; 134d4286259STim J. Robbins int cnt; 1350f10c7bbSTim J. Robbins wint_t ich; 1360f10c7bbSTim J. Robbins wchar_t ch; 1370f10c7bbSTim J. Robbins char *p; 1389b50d902SRodney W. Grimes LIST *head, *tmp; 1399b50d902SRodney W. Grimes int opencnt, output; 1409b50d902SRodney W. Grimes 141b2eeeae0SPhilippe Charnier for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) { 14247bca8b0SJuli Mallett if ((lp = malloc(sizeof(LIST))) == NULL) 14347bca8b0SJuli Mallett err(1, NULL); 1449b50d902SRodney W. Grimes if (p[0] == '-' && !p[1]) 1459b50d902SRodney W. Grimes lp->fp = stdin; 1461e133604SPhilippe Charnier else if (!(lp->fp = fopen(p, "r"))) 1471e133604SPhilippe Charnier err(1, "%s", p); 1489b50d902SRodney W. Grimes lp->next = NULL; 1499b50d902SRodney W. Grimes lp->cnt = cnt; 1509b50d902SRodney W. Grimes lp->name = p; 1519b50d902SRodney W. Grimes if (!head) 1529b50d902SRodney W. Grimes head = tmp = lp; 1539b50d902SRodney W. Grimes else { 1549b50d902SRodney W. Grimes tmp->next = lp; 1559b50d902SRodney W. Grimes tmp = lp; 1569b50d902SRodney W. Grimes } 1579b50d902SRodney W. Grimes } 1589b50d902SRodney W. Grimes 1599b50d902SRodney W. Grimes for (opencnt = cnt; opencnt;) { 1609b50d902SRodney W. Grimes for (output = 0, lp = head; lp; lp = lp->next) { 1619b50d902SRodney W. Grimes if (!lp->fp) { 1629b50d902SRodney W. Grimes if (output && lp->cnt && 1639b50d902SRodney W. Grimes (ch = delim[(lp->cnt - 1) % delimcnt])) 1640f10c7bbSTim J. Robbins putwchar(ch); 1659b50d902SRodney W. Grimes continue; 1669b50d902SRodney W. Grimes } 1670f10c7bbSTim J. Robbins if ((ich = getwc(lp->fp)) == WEOF) { 1689b50d902SRodney W. Grimes if (!--opencnt) 1699b50d902SRodney W. Grimes break; 1709b50d902SRodney W. Grimes lp->fp = NULL; 1719b50d902SRodney W. Grimes if (output && lp->cnt && 1729b50d902SRodney W. Grimes (ch = delim[(lp->cnt - 1) % delimcnt])) 1730f10c7bbSTim J. Robbins putwchar(ch); 1749b50d902SRodney W. Grimes continue; 1759b50d902SRodney W. Grimes } 1769b50d902SRodney W. Grimes /* 1779b50d902SRodney W. Grimes * make sure that we don't print any delimiters 1789b50d902SRodney W. Grimes * unless there's a non-empty file. 1799b50d902SRodney W. Grimes */ 1809b50d902SRodney W. Grimes if (!output) { 1819b50d902SRodney W. Grimes output = 1; 1829b50d902SRodney W. Grimes for (cnt = 0; cnt < lp->cnt; ++cnt) 1831e133604SPhilippe Charnier if ((ch = delim[cnt % delimcnt])) 1840f10c7bbSTim J. Robbins putwchar(ch); 1851e133604SPhilippe Charnier } else if ((ch = delim[(lp->cnt - 1) % delimcnt])) 1860f10c7bbSTim J. Robbins putwchar(ch); 1870f10c7bbSTim J. Robbins if (ich == '\n') 1880f10c7bbSTim J. Robbins continue; 1890f10c7bbSTim J. Robbins do { 1900f10c7bbSTim J. Robbins putwchar(ich); 1910f10c7bbSTim J. Robbins } while ((ich = getwc(lp->fp)) != WEOF && ich != '\n'); 1929b50d902SRodney W. Grimes } 1939b50d902SRodney W. Grimes if (output) 1940f10c7bbSTim J. Robbins putwchar('\n'); 1959b50d902SRodney W. Grimes } 1960968654cSTim J. Robbins 1970968654cSTim J. Robbins return (0); 1989b50d902SRodney W. Grimes } 1999b50d902SRodney W. Grimes 200e7b33384SEd Schouten static int 20178552e79SMark Murray sequential(char **argv) 2029b50d902SRodney W. Grimes { 20378552e79SMark Murray FILE *fp; 204d4286259STim J. Robbins int cnt, failed, needdelim; 2050f10c7bbSTim J. Robbins wint_t ch; 2060f10c7bbSTim J. Robbins char *p; 2079b50d902SRodney W. Grimes 2080968654cSTim J. Robbins failed = 0; 2091e133604SPhilippe Charnier for (; (p = *argv); ++argv) { 2109b50d902SRodney W. Grimes if (p[0] == '-' && !p[1]) 2119b50d902SRodney W. Grimes fp = stdin; 2129b50d902SRodney W. Grimes else if (!(fp = fopen(p, "r"))) { 2131e133604SPhilippe Charnier warn("%s", p); 2140968654cSTim J. Robbins failed = 1; 2159b50d902SRodney W. Grimes continue; 2169b50d902SRodney W. Grimes } 217ce78cbf9STim J. Robbins cnt = needdelim = 0; 2180f10c7bbSTim J. Robbins while ((ch = getwc(fp)) != WEOF) { 219ce78cbf9STim J. Robbins if (needdelim) { 220ce78cbf9STim J. Robbins needdelim = 0; 221ce78cbf9STim J. Robbins if (delim[cnt] != '\0') 2220f10c7bbSTim J. Robbins putwchar(delim[cnt]); 223ce78cbf9STim J. Robbins if (++cnt == delimcnt) 2249b50d902SRodney W. Grimes cnt = 0; 2259b50d902SRodney W. Grimes } 2260f10c7bbSTim J. Robbins if (ch != '\n') 2270f10c7bbSTim J. Robbins putwchar(ch); 2280f10c7bbSTim J. Robbins else 229ce78cbf9STim J. Robbins needdelim = 1; 2309b50d902SRodney W. Grimes } 231ce78cbf9STim J. Robbins if (needdelim) 2320f10c7bbSTim J. Robbins putwchar('\n'); 2339b50d902SRodney W. Grimes if (fp != stdin) 2349b50d902SRodney W. Grimes (void)fclose(fp); 2359b50d902SRodney W. Grimes } 2360968654cSTim J. Robbins 2370968654cSTim J. Robbins return (failed != 0); 2389b50d902SRodney W. Grimes } 2399b50d902SRodney W. Grimes 240e7b33384SEd Schouten static int 2410f10c7bbSTim J. Robbins tr(wchar_t *arg) 2429b50d902SRodney W. Grimes { 24378552e79SMark Murray int cnt; 2440f10c7bbSTim J. Robbins wchar_t ch, *p; 2459b50d902SRodney W. Grimes 2469b50d902SRodney W. Grimes for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt) 2479b50d902SRodney W. Grimes if (ch == '\\') 2489b50d902SRodney W. Grimes switch(ch = *p++) { 2499b50d902SRodney W. Grimes case 'n': 2509b50d902SRodney W. Grimes *arg = '\n'; 2519b50d902SRodney W. Grimes break; 2529b50d902SRodney W. Grimes case 't': 2539b50d902SRodney W. Grimes *arg = '\t'; 2549b50d902SRodney W. Grimes break; 2559b50d902SRodney W. Grimes case '0': 2569b50d902SRodney W. Grimes *arg = '\0'; 2579b50d902SRodney W. Grimes break; 2589b50d902SRodney W. Grimes default: 2599b50d902SRodney W. Grimes *arg = ch; 2609b50d902SRodney W. Grimes break; 2619b50d902SRodney W. Grimes } else 2629b50d902SRodney W. Grimes *arg = ch; 2639b50d902SRodney W. Grimes 2641e133604SPhilippe Charnier if (!cnt) 2651e133604SPhilippe Charnier errx(1, "no delimiters specified"); 2669b50d902SRodney W. Grimes return(cnt); 2679b50d902SRodney W. Grimes } 2689b50d902SRodney W. Grimes 2691e133604SPhilippe Charnier static void 27078552e79SMark Murray usage(void) 2719b50d902SRodney W. Grimes { 2721e133604SPhilippe Charnier (void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n"); 2739b50d902SRodney W. Grimes exit(1); 2749b50d902SRodney W. Grimes } 275