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 * 3. All advertising materials mentioning features or use of this software 179b50d902SRodney W. Grimes * must display the following acknowledgement: 189b50d902SRodney W. Grimes * This product includes software developed by the University of 199b50d902SRodney W. Grimes * California, Berkeley and its contributors. 209b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 219b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 229b50d902SRodney W. Grimes * without specific prior written permission. 239b50d902SRodney W. Grimes * 249b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 259b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 269b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 279b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 289b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 299b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 309b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 319b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 329b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 339b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 349b50d902SRodney W. Grimes * SUCH DAMAGE. 359b50d902SRodney W. Grimes */ 369b50d902SRodney W. Grimes 379b50d902SRodney W. Grimes #ifndef lint 389b50d902SRodney W. Grimes static char copyright[] = 399b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 409b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 419b50d902SRodney W. Grimes #endif /* not lint */ 429b50d902SRodney W. Grimes 439b50d902SRodney W. Grimes #ifndef lint 449b50d902SRodney W. Grimes static char sccsid[] = "@(#)paste.c 8.1 (Berkeley) 6/6/93"; 459b50d902SRodney W. Grimes #endif /* not lint */ 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include <sys/types.h> 489b50d902SRodney W. Grimes #include <errno.h> 499b50d902SRodney W. Grimes #include <limits.h> 509b50d902SRodney W. Grimes #include <stdio.h> 519b50d902SRodney W. Grimes #include <string.h> 529b50d902SRodney W. Grimes 539b50d902SRodney W. Grimes char *delim; 549b50d902SRodney W. Grimes int delimcnt; 559b50d902SRodney W. Grimes 569b50d902SRodney W. Grimes main(argc, argv) 579b50d902SRodney W. Grimes int argc; 589b50d902SRodney W. Grimes char **argv; 599b50d902SRodney W. Grimes { 609b50d902SRodney W. Grimes extern char *optarg; 619b50d902SRodney W. Grimes extern int optind; 629b50d902SRodney W. Grimes int ch, seq; 639b50d902SRodney W. Grimes 649b50d902SRodney W. Grimes seq = 0; 651c8af878SWarner Losh while ((ch = getopt(argc, argv, "d:s")) != -1) 669b50d902SRodney W. Grimes switch(ch) { 679b50d902SRodney W. Grimes case 'd': 689b50d902SRodney W. Grimes delimcnt = tr(delim = optarg); 699b50d902SRodney W. Grimes break; 709b50d902SRodney W. Grimes case 's': 719b50d902SRodney W. Grimes seq = 1; 729b50d902SRodney W. Grimes break; 739b50d902SRodney W. Grimes case '?': 749b50d902SRodney W. Grimes default: 759b50d902SRodney W. Grimes usage(); 769b50d902SRodney W. Grimes } 779b50d902SRodney W. Grimes argc -= optind; 789b50d902SRodney W. Grimes argv += optind; 799b50d902SRodney W. Grimes 809b50d902SRodney W. Grimes if (!delim) { 819b50d902SRodney W. Grimes delimcnt = 1; 829b50d902SRodney W. Grimes delim = "\t"; 839b50d902SRodney W. Grimes } 849b50d902SRodney W. Grimes 859b50d902SRodney W. Grimes if (seq) 869b50d902SRodney W. Grimes sequential(argv); 879b50d902SRodney W. Grimes else 889b50d902SRodney W. Grimes parallel(argv); 899b50d902SRodney W. Grimes exit(0); 909b50d902SRodney W. Grimes } 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes typedef struct _list { 939b50d902SRodney W. Grimes struct _list *next; 949b50d902SRodney W. Grimes FILE *fp; 959b50d902SRodney W. Grimes int cnt; 969b50d902SRodney W. Grimes char *name; 979b50d902SRodney W. Grimes } LIST; 989b50d902SRodney W. Grimes 999b50d902SRodney W. Grimes parallel(argv) 1009b50d902SRodney W. Grimes char **argv; 1019b50d902SRodney W. Grimes { 1029b50d902SRodney W. Grimes register LIST *lp; 1039b50d902SRodney W. Grimes register int cnt; 1049b50d902SRodney W. Grimes register char ch, *p; 1059b50d902SRodney W. Grimes LIST *head, *tmp; 1069b50d902SRodney W. Grimes int opencnt, output; 1079b50d902SRodney W. Grimes char buf[_POSIX2_LINE_MAX + 1], *malloc(); 1089b50d902SRodney W. Grimes 1099b50d902SRodney W. Grimes for (cnt = 0, head = NULL; p = *argv; ++argv, ++cnt) { 1109b50d902SRodney W. Grimes if (!(lp = (LIST *)malloc((u_int)sizeof(LIST)))) { 1119b50d902SRodney W. Grimes (void)fprintf(stderr, "paste: %s.\n", strerror(ENOMEM)); 1129b50d902SRodney W. Grimes exit(1); 1139b50d902SRodney W. Grimes } 1149b50d902SRodney W. Grimes if (p[0] == '-' && !p[1]) 1159b50d902SRodney W. Grimes lp->fp = stdin; 1169b50d902SRodney W. Grimes else if (!(lp->fp = fopen(p, "r"))) { 1179b50d902SRodney W. Grimes (void)fprintf(stderr, "paste: %s: %s.\n", p, 1189b50d902SRodney W. Grimes strerror(errno)); 1199b50d902SRodney W. Grimes exit(1); 1209b50d902SRodney W. Grimes } 1219b50d902SRodney W. Grimes lp->next = NULL; 1229b50d902SRodney W. Grimes lp->cnt = cnt; 1239b50d902SRodney W. Grimes lp->name = p; 1249b50d902SRodney W. Grimes if (!head) 1259b50d902SRodney W. Grimes head = tmp = lp; 1269b50d902SRodney W. Grimes else { 1279b50d902SRodney W. Grimes tmp->next = lp; 1289b50d902SRodney W. Grimes tmp = lp; 1299b50d902SRodney W. Grimes } 1309b50d902SRodney W. Grimes } 1319b50d902SRodney W. Grimes 1329b50d902SRodney W. Grimes for (opencnt = cnt; opencnt;) { 1339b50d902SRodney W. Grimes for (output = 0, lp = head; lp; lp = lp->next) { 1349b50d902SRodney W. Grimes if (!lp->fp) { 1359b50d902SRodney W. Grimes if (output && lp->cnt && 1369b50d902SRodney W. Grimes (ch = delim[(lp->cnt - 1) % delimcnt])) 1379b50d902SRodney W. Grimes putchar(ch); 1389b50d902SRodney W. Grimes continue; 1399b50d902SRodney W. Grimes } 1409b50d902SRodney W. Grimes if (!fgets(buf, sizeof(buf), lp->fp)) { 1419b50d902SRodney W. Grimes if (!--opencnt) 1429b50d902SRodney W. Grimes break; 1439b50d902SRodney W. Grimes lp->fp = NULL; 1449b50d902SRodney W. Grimes if (output && lp->cnt && 1459b50d902SRodney W. Grimes (ch = delim[(lp->cnt - 1) % delimcnt])) 1469b50d902SRodney W. Grimes putchar(ch); 1479b50d902SRodney W. Grimes continue; 1489b50d902SRodney W. Grimes } 1499b50d902SRodney W. Grimes if (!(p = index(buf, '\n'))) { 1509b50d902SRodney W. Grimes (void)fprintf(stderr, 1519b50d902SRodney W. Grimes "paste: %s: input line too long.\n", 1529b50d902SRodney W. Grimes lp->name); 1539b50d902SRodney W. Grimes exit(1); 1549b50d902SRodney W. Grimes } 1559b50d902SRodney W. Grimes *p = '\0'; 1569b50d902SRodney W. Grimes /* 1579b50d902SRodney W. Grimes * make sure that we don't print any delimiters 1589b50d902SRodney W. Grimes * unless there's a non-empty file. 1599b50d902SRodney W. Grimes */ 1609b50d902SRodney W. Grimes if (!output) { 1619b50d902SRodney W. Grimes output = 1; 1629b50d902SRodney W. Grimes for (cnt = 0; cnt < lp->cnt; ++cnt) 1639b50d902SRodney W. Grimes if (ch = delim[cnt % delimcnt]) 1649b50d902SRodney W. Grimes putchar(ch); 1659b50d902SRodney W. Grimes } else if (ch = delim[(lp->cnt - 1) % delimcnt]) 1669b50d902SRodney W. Grimes putchar(ch); 1679b50d902SRodney W. Grimes (void)printf("%s", buf); 1689b50d902SRodney W. Grimes } 1699b50d902SRodney W. Grimes if (output) 1709b50d902SRodney W. Grimes putchar('\n'); 1719b50d902SRodney W. Grimes } 1729b50d902SRodney W. Grimes } 1739b50d902SRodney W. Grimes 1749b50d902SRodney W. Grimes sequential(argv) 1759b50d902SRodney W. Grimes char **argv; 1769b50d902SRodney W. Grimes { 1779b50d902SRodney W. Grimes register FILE *fp; 1789b50d902SRodney W. Grimes register int cnt; 1799b50d902SRodney W. Grimes register char ch, *p, *dp; 1809b50d902SRodney W. Grimes char buf[_POSIX2_LINE_MAX + 1]; 1819b50d902SRodney W. Grimes 1829b50d902SRodney W. Grimes for (; p = *argv; ++argv) { 1839b50d902SRodney W. Grimes if (p[0] == '-' && !p[1]) 1849b50d902SRodney W. Grimes fp = stdin; 1859b50d902SRodney W. Grimes else if (!(fp = fopen(p, "r"))) { 1869b50d902SRodney W. Grimes (void)fprintf(stderr, "paste: %s: %s.\n", p, 1879b50d902SRodney W. Grimes strerror(errno)); 1889b50d902SRodney W. Grimes continue; 1899b50d902SRodney W. Grimes } 1909b50d902SRodney W. Grimes if (fgets(buf, sizeof(buf), fp)) { 1919b50d902SRodney W. Grimes for (cnt = 0, dp = delim;;) { 1929b50d902SRodney W. Grimes if (!(p = index(buf, '\n'))) { 1939b50d902SRodney W. Grimes (void)fprintf(stderr, 1949b50d902SRodney W. Grimes "paste: %s: input line too long.\n", 1959b50d902SRodney W. Grimes *argv); 1969b50d902SRodney W. Grimes exit(1); 1979b50d902SRodney W. Grimes } 1989b50d902SRodney W. Grimes *p = '\0'; 1999b50d902SRodney W. Grimes (void)printf("%s", buf); 2009b50d902SRodney W. Grimes if (!fgets(buf, sizeof(buf), fp)) 2019b50d902SRodney W. Grimes break; 2029b50d902SRodney W. Grimes if (ch = *dp++) 2039b50d902SRodney W. Grimes putchar(ch); 2049b50d902SRodney W. Grimes if (++cnt == delimcnt) { 2059b50d902SRodney W. Grimes dp = delim; 2069b50d902SRodney W. Grimes cnt = 0; 2079b50d902SRodney W. Grimes } 2089b50d902SRodney W. Grimes } 2099b50d902SRodney W. Grimes putchar('\n'); 2109b50d902SRodney W. Grimes } 2119b50d902SRodney W. Grimes if (fp != stdin) 2129b50d902SRodney W. Grimes (void)fclose(fp); 2139b50d902SRodney W. Grimes } 2149b50d902SRodney W. Grimes } 2159b50d902SRodney W. Grimes 2169b50d902SRodney W. Grimes tr(arg) 2179b50d902SRodney W. Grimes char *arg; 2189b50d902SRodney W. Grimes { 2199b50d902SRodney W. Grimes register int cnt; 2209b50d902SRodney W. Grimes register char ch, *p; 2219b50d902SRodney W. Grimes 2229b50d902SRodney W. Grimes for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt) 2239b50d902SRodney W. Grimes if (ch == '\\') 2249b50d902SRodney W. Grimes switch(ch = *p++) { 2259b50d902SRodney W. Grimes case 'n': 2269b50d902SRodney W. Grimes *arg = '\n'; 2279b50d902SRodney W. Grimes break; 2289b50d902SRodney W. Grimes case 't': 2299b50d902SRodney W. Grimes *arg = '\t'; 2309b50d902SRodney W. Grimes break; 2319b50d902SRodney W. Grimes case '0': 2329b50d902SRodney W. Grimes *arg = '\0'; 2339b50d902SRodney W. Grimes break; 2349b50d902SRodney W. Grimes default: 2359b50d902SRodney W. Grimes *arg = ch; 2369b50d902SRodney W. Grimes break; 2379b50d902SRodney W. Grimes } else 2389b50d902SRodney W. Grimes *arg = ch; 2399b50d902SRodney W. Grimes 2409b50d902SRodney W. Grimes if (!cnt) { 2419b50d902SRodney W. Grimes (void)fprintf(stderr, "paste: no delimiters specified.\n"); 2429b50d902SRodney W. Grimes exit(1); 2439b50d902SRodney W. Grimes } 2449b50d902SRodney W. Grimes return(cnt); 2459b50d902SRodney W. Grimes } 2469b50d902SRodney W. Grimes 2479b50d902SRodney W. Grimes usage() 2489b50d902SRodney W. Grimes { 2499b50d902SRodney W. Grimes (void)fprintf(stderr, "paste: [-s] [-d delimiters] file ...\n"); 2509b50d902SRodney W. Grimes exit(1); 2519b50d902SRodney W. Grimes } 252