1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Adam S. Moskowitz of Menlo Consulting. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static const char copyright[] = 39 "@(#) Copyright (c) 1989, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)paste.c 8.1 (Berkeley) 6/6/93"; 46 #endif 47 static const char rcsid[] = 48 "$Id: paste.c,v 1.4 1997/08/05 03:40:04 asami Exp $"; 49 #endif /* not lint */ 50 51 #include <sys/types.h> 52 #include <err.h> 53 #include <errno.h> 54 #include <limits.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 60 char *delim; 61 int delimcnt; 62 63 void parallel __P((char **)); 64 void sequential __P((char **)); 65 int tr __P((char *)); 66 static void usage __P((void)); 67 68 int 69 main(argc, argv) 70 int argc; 71 char **argv; 72 { 73 int ch, seq; 74 75 seq = 0; 76 while ((ch = getopt(argc, argv, "d:s")) != -1) 77 switch(ch) { 78 case 'd': 79 delimcnt = tr(delim = optarg); 80 break; 81 case 's': 82 seq = 1; 83 break; 84 case '?': 85 default: 86 usage(); 87 } 88 argc -= optind; 89 argv += optind; 90 91 if (!delim) { 92 delimcnt = 1; 93 delim = "\t"; 94 } 95 96 if (seq) 97 sequential(argv); 98 else 99 parallel(argv); 100 exit(0); 101 } 102 103 typedef struct _list { 104 struct _list *next; 105 FILE *fp; 106 int cnt; 107 char *name; 108 } LIST; 109 110 void 111 parallel(argv) 112 char **argv; 113 { 114 register LIST *lp; 115 register int cnt; 116 register char ch, *p; 117 LIST *head, *tmp; 118 int opencnt, output; 119 char buf[_POSIX2_LINE_MAX + 1]; 120 121 for (cnt = 0, head = NULL; (p = *argv); ++argv, ++cnt) { 122 if (!(lp = (LIST *)malloc((u_int)sizeof(LIST)))) 123 errx(1, "%s", strerror(ENOMEM)); 124 if (p[0] == '-' && !p[1]) 125 lp->fp = stdin; 126 else if (!(lp->fp = fopen(p, "r"))) 127 err(1, "%s", p); 128 lp->next = NULL; 129 lp->cnt = cnt; 130 lp->name = p; 131 if (!head) 132 head = tmp = lp; 133 else { 134 tmp->next = lp; 135 tmp = lp; 136 } 137 } 138 139 for (opencnt = cnt; opencnt;) { 140 for (output = 0, lp = head; lp; lp = lp->next) { 141 if (!lp->fp) { 142 if (output && lp->cnt && 143 (ch = delim[(lp->cnt - 1) % delimcnt])) 144 putchar(ch); 145 continue; 146 } 147 if (!fgets(buf, sizeof(buf), lp->fp)) { 148 if (!--opencnt) 149 break; 150 lp->fp = NULL; 151 if (output && lp->cnt && 152 (ch = delim[(lp->cnt - 1) % delimcnt])) 153 putchar(ch); 154 continue; 155 } 156 if (!(p = index(buf, '\n'))) 157 errx(1, "%s: input line too long", lp->name); 158 *p = '\0'; 159 /* 160 * make sure that we don't print any delimiters 161 * unless there's a non-empty file. 162 */ 163 if (!output) { 164 output = 1; 165 for (cnt = 0; cnt < lp->cnt; ++cnt) 166 if ((ch = delim[cnt % delimcnt])) 167 putchar(ch); 168 } else if ((ch = delim[(lp->cnt - 1) % delimcnt])) 169 putchar(ch); 170 (void)printf("%s", buf); 171 } 172 if (output) 173 putchar('\n'); 174 } 175 } 176 177 void 178 sequential(argv) 179 char **argv; 180 { 181 register FILE *fp; 182 register int cnt; 183 register char ch, *p, *dp; 184 char buf[_POSIX2_LINE_MAX + 1]; 185 186 for (; (p = *argv); ++argv) { 187 if (p[0] == '-' && !p[1]) 188 fp = stdin; 189 else if (!(fp = fopen(p, "r"))) { 190 warn("%s", p); 191 continue; 192 } 193 if (fgets(buf, sizeof(buf), fp)) { 194 for (cnt = 0, dp = delim;;) { 195 if (!(p = index(buf, '\n'))) 196 errx(1, "%s: input line too long", *argv); 197 *p = '\0'; 198 (void)printf("%s", buf); 199 if (!fgets(buf, sizeof(buf), fp)) 200 break; 201 if ((ch = *dp++)) 202 putchar(ch); 203 if (++cnt == delimcnt) { 204 dp = delim; 205 cnt = 0; 206 } 207 } 208 putchar('\n'); 209 } 210 if (fp != stdin) 211 (void)fclose(fp); 212 } 213 } 214 215 int 216 tr(arg) 217 char *arg; 218 { 219 register int cnt; 220 register char ch, *p; 221 222 for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt) 223 if (ch == '\\') 224 switch(ch = *p++) { 225 case 'n': 226 *arg = '\n'; 227 break; 228 case 't': 229 *arg = '\t'; 230 break; 231 case '0': 232 *arg = '\0'; 233 break; 234 default: 235 *arg = ch; 236 break; 237 } else 238 *arg = ch; 239 240 if (!cnt) 241 errx(1, "no delimiters specified"); 242 return(cnt); 243 } 244 245 static void 246 usage() 247 { 248 (void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n"); 249 exit(1); 250 } 251