19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1989, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 69b50d902SRodney W. Grimes * Adam S. Moskowitz of Menlo Consulting. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 169b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 179b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 189b50d902SRodney W. Grimes * without specific prior written permission. 199b50d902SRodney W. Grimes * 209b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 219b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 249b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309b50d902SRodney W. Grimes * SUCH DAMAGE. 319b50d902SRodney W. Grimes */ 329b50d902SRodney W. Grimes 339b50d902SRodney W. Grimes #ifndef lint 341e133604SPhilippe Charnier static const char copyright[] = 359b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 369b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 379b50d902SRodney W. Grimes #endif /* not lint */ 389b50d902SRodney W. Grimes 391e133604SPhilippe Charnier #if 0 40d345176dSMike Barcroft #ifndef lint 419b50d902SRodney W. Grimes static char sccsid[] = "@(#)paste.c 8.1 (Berkeley) 6/6/93"; 429b50d902SRodney W. Grimes #endif /* not lint */ 43d345176dSMike Barcroft #endif 44d345176dSMike Barcroft 45d345176dSMike Barcroft #include <sys/cdefs.h> 46d345176dSMike Barcroft __FBSDID("$FreeBSD$"); 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <sys/types.h> 4978552e79SMark Murray 501e133604SPhilippe Charnier #include <err.h> 51821df508SXin LI #include <errno.h> 52821df508SXin LI #include <limits.h> 530f10c7bbSTim J. Robbins #include <locale.h> 549b50d902SRodney W. Grimes #include <stdio.h> 55c93bd87aSJohn Birrell #include <stdlib.h> 56821df508SXin LI #include <string.h> 571e133604SPhilippe Charnier #include <unistd.h> 580f10c7bbSTim J. Robbins #include <wchar.h> 599b50d902SRodney W. Grimes 60e7b33384SEd Schouten static wchar_t *delim; 61e7b33384SEd Schouten static int delimcnt; 629b50d902SRodney W. Grimes 63e7b33384SEd Schouten static int parallel(char **); 64e7b33384SEd Schouten static int sequential(char **); 65e7b33384SEd Schouten static int tr(wchar_t *); 66d3cb5dedSWarner Losh static void usage(void); 671e133604SPhilippe Charnier 68e7b33384SEd Schouten static wchar_t tab[] = L"\t"; 6978552e79SMark Murray 701e133604SPhilippe Charnier int 7178552e79SMark Murray main(int argc, char *argv[]) 729b50d902SRodney W. Grimes { 730968654cSTim J. Robbins int ch, rval, seq; 740f10c7bbSTim J. Robbins wchar_t *warg; 750f10c7bbSTim J. Robbins const char *arg; 760f10c7bbSTim J. Robbins size_t len; 770f10c7bbSTim J. Robbins 780f10c7bbSTim J. Robbins setlocale(LC_CTYPE, ""); 799b50d902SRodney W. Grimes 809b50d902SRodney W. Grimes seq = 0; 811c8af878SWarner Losh while ((ch = getopt(argc, argv, "d:s")) != -1) 829b50d902SRodney W. Grimes switch(ch) { 839b50d902SRodney W. Grimes case 'd': 840f10c7bbSTim J. Robbins arg = optarg; 850f10c7bbSTim J. Robbins len = mbsrtowcs(NULL, &arg, 0, NULL); 860f10c7bbSTim J. Robbins if (len == (size_t)-1) 870f10c7bbSTim J. Robbins err(1, "delimiters"); 880f10c7bbSTim J. Robbins warg = malloc((len + 1) * sizeof(*warg)); 890f10c7bbSTim J. Robbins if (warg == NULL) 900f10c7bbSTim J. Robbins err(1, NULL); 910f10c7bbSTim J. Robbins arg = optarg; 920f10c7bbSTim J. Robbins len = mbsrtowcs(warg, &arg, len + 1, NULL); 930f10c7bbSTim J. Robbins if (len == (size_t)-1) 940f10c7bbSTim J. Robbins err(1, "delimiters"); 950f10c7bbSTim J. Robbins delimcnt = tr(delim = warg); 969b50d902SRodney W. Grimes break; 979b50d902SRodney W. Grimes case 's': 989b50d902SRodney W. Grimes seq = 1; 999b50d902SRodney W. Grimes break; 1009b50d902SRodney W. Grimes case '?': 1019b50d902SRodney W. Grimes default: 1029b50d902SRodney W. Grimes usage(); 1039b50d902SRodney W. Grimes } 1049b50d902SRodney W. Grimes argc -= optind; 1059b50d902SRodney W. Grimes argv += optind; 1069b50d902SRodney W. Grimes 107d345176dSMike Barcroft if (*argv == NULL) 108d345176dSMike Barcroft usage(); 1099b50d902SRodney W. Grimes if (!delim) { 1109b50d902SRodney W. Grimes delimcnt = 1; 11178552e79SMark Murray delim = tab; 1129b50d902SRodney W. Grimes } 1139b50d902SRodney W. Grimes 1149b50d902SRodney W. Grimes if (seq) 1150968654cSTim J. Robbins rval = sequential(argv); 1169b50d902SRodney W. Grimes else 1170968654cSTim J. Robbins rval = parallel(argv); 1180968654cSTim J. Robbins exit(rval); 1199b50d902SRodney W. Grimes } 1209b50d902SRodney W. Grimes 1219b50d902SRodney W. Grimes typedef struct _list { 1229b50d902SRodney W. Grimes struct _list *next; 1239b50d902SRodney W. Grimes FILE *fp; 1249b50d902SRodney W. Grimes int cnt; 1259b50d902SRodney W. Grimes char *name; 1269b50d902SRodney W. Grimes } LIST; 1279b50d902SRodney W. Grimes 128e7b33384SEd Schouten static int 12978552e79SMark Murray parallel(char **argv) 1309b50d902SRodney W. Grimes { 13178552e79SMark Murray LIST *lp; 132d4286259STim J. Robbins int cnt; 1330f10c7bbSTim J. Robbins wint_t ich; 1340f10c7bbSTim J. Robbins wchar_t ch; 1350f10c7bbSTim J. Robbins char *p; 1369b50d902SRodney W. Grimes LIST *head, *tmp; 1379b50d902SRodney W. Grimes int opencnt, output; 1389b50d902SRodney W. Grimes 139b2eeeae0SPhilippe Charnier for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) { 14047bca8b0SJuli Mallett if ((lp = malloc(sizeof(LIST))) == NULL) 14147bca8b0SJuli Mallett err(1, NULL); 1429b50d902SRodney W. Grimes if (p[0] == '-' && !p[1]) 1439b50d902SRodney W. Grimes lp->fp = stdin; 1441e133604SPhilippe Charnier else if (!(lp->fp = fopen(p, "r"))) 1451e133604SPhilippe Charnier err(1, "%s", p); 1469b50d902SRodney W. Grimes lp->next = NULL; 1479b50d902SRodney W. Grimes lp->cnt = cnt; 1489b50d902SRodney W. Grimes lp->name = p; 1499b50d902SRodney W. Grimes if (!head) 1509b50d902SRodney W. Grimes head = tmp = lp; 1519b50d902SRodney W. Grimes else { 1529b50d902SRodney W. Grimes tmp->next = lp; 1539b50d902SRodney W. Grimes tmp = lp; 1549b50d902SRodney W. Grimes } 1559b50d902SRodney W. Grimes } 1569b50d902SRodney W. Grimes 1579b50d902SRodney W. Grimes for (opencnt = cnt; opencnt;) { 1589b50d902SRodney W. Grimes for (output = 0, lp = head; lp; lp = lp->next) { 1599b50d902SRodney W. Grimes if (!lp->fp) { 1609b50d902SRodney W. Grimes if (output && lp->cnt && 1619b50d902SRodney W. Grimes (ch = delim[(lp->cnt - 1) % delimcnt])) 1620f10c7bbSTim J. Robbins putwchar(ch); 1639b50d902SRodney W. Grimes continue; 1649b50d902SRodney W. Grimes } 1650f10c7bbSTim J. Robbins if ((ich = getwc(lp->fp)) == WEOF) { 1669b50d902SRodney W. Grimes if (!--opencnt) 1679b50d902SRodney W. Grimes break; 1689b50d902SRodney W. Grimes lp->fp = NULL; 1699b50d902SRodney W. Grimes if (output && lp->cnt && 1709b50d902SRodney W. Grimes (ch = delim[(lp->cnt - 1) % delimcnt])) 1710f10c7bbSTim J. Robbins putwchar(ch); 1729b50d902SRodney W. Grimes continue; 1739b50d902SRodney W. Grimes } 1749b50d902SRodney W. Grimes /* 1759b50d902SRodney W. Grimes * make sure that we don't print any delimiters 1769b50d902SRodney W. Grimes * unless there's a non-empty file. 1779b50d902SRodney W. Grimes */ 1789b50d902SRodney W. Grimes if (!output) { 1799b50d902SRodney W. Grimes output = 1; 1809b50d902SRodney W. Grimes for (cnt = 0; cnt < lp->cnt; ++cnt) 1811e133604SPhilippe Charnier if ((ch = delim[cnt % delimcnt])) 1820f10c7bbSTim J. Robbins putwchar(ch); 1831e133604SPhilippe Charnier } else if ((ch = delim[(lp->cnt - 1) % delimcnt])) 1840f10c7bbSTim J. Robbins putwchar(ch); 1850f10c7bbSTim J. Robbins if (ich == '\n') 1860f10c7bbSTim J. Robbins continue; 1870f10c7bbSTim J. Robbins do { 1880f10c7bbSTim J. Robbins putwchar(ich); 1890f10c7bbSTim J. Robbins } while ((ich = getwc(lp->fp)) != WEOF && ich != '\n'); 1909b50d902SRodney W. Grimes } 1919b50d902SRodney W. Grimes if (output) 1920f10c7bbSTim J. Robbins putwchar('\n'); 1939b50d902SRodney W. Grimes } 1940968654cSTim J. Robbins 1950968654cSTim J. Robbins return (0); 1969b50d902SRodney W. Grimes } 1979b50d902SRodney W. Grimes 198e7b33384SEd Schouten static int 19978552e79SMark Murray sequential(char **argv) 2009b50d902SRodney W. Grimes { 20178552e79SMark Murray FILE *fp; 202d4286259STim J. Robbins int cnt, failed, needdelim; 2030f10c7bbSTim J. Robbins wint_t ch; 2040f10c7bbSTim J. Robbins char *p; 2059b50d902SRodney W. Grimes 2060968654cSTim J. Robbins failed = 0; 2071e133604SPhilippe Charnier for (; (p = *argv); ++argv) { 2089b50d902SRodney W. Grimes if (p[0] == '-' && !p[1]) 2099b50d902SRodney W. Grimes fp = stdin; 2109b50d902SRodney W. Grimes else if (!(fp = fopen(p, "r"))) { 2111e133604SPhilippe Charnier warn("%s", p); 2120968654cSTim J. Robbins failed = 1; 2139b50d902SRodney W. Grimes continue; 2149b50d902SRodney W. Grimes } 215ce78cbf9STim J. Robbins cnt = needdelim = 0; 2160f10c7bbSTim J. Robbins while ((ch = getwc(fp)) != WEOF) { 217ce78cbf9STim J. Robbins if (needdelim) { 218ce78cbf9STim J. Robbins needdelim = 0; 219ce78cbf9STim J. Robbins if (delim[cnt] != '\0') 2200f10c7bbSTim J. Robbins putwchar(delim[cnt]); 221ce78cbf9STim J. Robbins if (++cnt == delimcnt) 2229b50d902SRodney W. Grimes cnt = 0; 2239b50d902SRodney W. Grimes } 2240f10c7bbSTim J. Robbins if (ch != '\n') 2250f10c7bbSTim J. Robbins putwchar(ch); 2260f10c7bbSTim J. Robbins else 227ce78cbf9STim J. Robbins needdelim = 1; 2289b50d902SRodney W. Grimes } 229ce78cbf9STim J. Robbins if (needdelim) 2300f10c7bbSTim J. Robbins putwchar('\n'); 2319b50d902SRodney W. Grimes if (fp != stdin) 2329b50d902SRodney W. Grimes (void)fclose(fp); 2339b50d902SRodney W. Grimes } 2340968654cSTim J. Robbins 2350968654cSTim J. Robbins return (failed != 0); 2369b50d902SRodney W. Grimes } 2379b50d902SRodney W. Grimes 238e7b33384SEd Schouten static int 2390f10c7bbSTim J. Robbins tr(wchar_t *arg) 2409b50d902SRodney W. Grimes { 24178552e79SMark Murray int cnt; 2420f10c7bbSTim J. Robbins wchar_t ch, *p; 2439b50d902SRodney W. Grimes 2449b50d902SRodney W. Grimes for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt) 2459b50d902SRodney W. Grimes if (ch == '\\') 2469b50d902SRodney W. Grimes switch(ch = *p++) { 2479b50d902SRodney W. Grimes case 'n': 2489b50d902SRodney W. Grimes *arg = '\n'; 2499b50d902SRodney W. Grimes break; 2509b50d902SRodney W. Grimes case 't': 2519b50d902SRodney W. Grimes *arg = '\t'; 2529b50d902SRodney W. Grimes break; 2539b50d902SRodney W. Grimes case '0': 2549b50d902SRodney W. Grimes *arg = '\0'; 2559b50d902SRodney W. Grimes break; 2569b50d902SRodney W. Grimes default: 2579b50d902SRodney W. Grimes *arg = ch; 2589b50d902SRodney W. Grimes break; 2599b50d902SRodney W. Grimes } else 2609b50d902SRodney W. Grimes *arg = ch; 2619b50d902SRodney W. Grimes 2621e133604SPhilippe Charnier if (!cnt) 2631e133604SPhilippe Charnier errx(1, "no delimiters specified"); 2649b50d902SRodney W. Grimes return(cnt); 2659b50d902SRodney W. Grimes } 2669b50d902SRodney W. Grimes 2671e133604SPhilippe Charnier static void 26878552e79SMark Murray usage(void) 2699b50d902SRodney W. Grimes { 2701e133604SPhilippe Charnier (void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n"); 2719b50d902SRodney W. Grimes exit(1); 2729b50d902SRodney W. Grimes } 273