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.3 1997/07/31 06:59:26 charnier 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 <string.h> 57 #include <unistd.h> 58 59 char *delim; 60 int delimcnt; 61 62 void parallel __P((char **)); 63 void sequential __P((char **)); 64 int tr __P((char *)); 65 static void usage __P((void)); 66 67 int 68 main(argc, argv) 69 int argc; 70 char **argv; 71 { 72 int ch, seq; 73 74 seq = 0; 75 while ((ch = getopt(argc, argv, "d:s")) != -1) 76 switch(ch) { 77 case 'd': 78 delimcnt = tr(delim = optarg); 79 break; 80 case 's': 81 seq = 1; 82 break; 83 case '?': 84 default: 85 usage(); 86 } 87 argc -= optind; 88 argv += optind; 89 90 if (!delim) { 91 delimcnt = 1; 92 delim = "\t"; 93 } 94 95 if (seq) 96 sequential(argv); 97 else 98 parallel(argv); 99 exit(0); 100 } 101 102 typedef struct _list { 103 struct _list *next; 104 FILE *fp; 105 int cnt; 106 char *name; 107 } LIST; 108 109 void 110 parallel(argv) 111 char **argv; 112 { 113 register LIST *lp; 114 register int cnt; 115 register char ch, *p; 116 LIST *head, *tmp; 117 int opencnt, output; 118 char buf[_POSIX2_LINE_MAX + 1]; 119 120 for (cnt = 0, head = NULL; (p = *argv); ++argv, ++cnt) { 121 if (!(lp = (LIST *)malloc((u_int)sizeof(LIST)))) 122 errx(1, "%s", strerror(ENOMEM)); 123 if (p[0] == '-' && !p[1]) 124 lp->fp = stdin; 125 else if (!(lp->fp = fopen(p, "r"))) 126 err(1, "%s", p); 127 lp->next = NULL; 128 lp->cnt = cnt; 129 lp->name = p; 130 if (!head) 131 head = tmp = lp; 132 else { 133 tmp->next = lp; 134 tmp = lp; 135 } 136 } 137 138 for (opencnt = cnt; opencnt;) { 139 for (output = 0, lp = head; lp; lp = lp->next) { 140 if (!lp->fp) { 141 if (output && lp->cnt && 142 (ch = delim[(lp->cnt - 1) % delimcnt])) 143 putchar(ch); 144 continue; 145 } 146 if (!fgets(buf, sizeof(buf), lp->fp)) { 147 if (!--opencnt) 148 break; 149 lp->fp = NULL; 150 if (output && lp->cnt && 151 (ch = delim[(lp->cnt - 1) % delimcnt])) 152 putchar(ch); 153 continue; 154 } 155 if (!(p = index(buf, '\n'))) 156 errx(1, "%s: input line too long", lp->name); 157 *p = '\0'; 158 /* 159 * make sure that we don't print any delimiters 160 * unless there's a non-empty file. 161 */ 162 if (!output) { 163 output = 1; 164 for (cnt = 0; cnt < lp->cnt; ++cnt) 165 if ((ch = delim[cnt % delimcnt])) 166 putchar(ch); 167 } else if ((ch = delim[(lp->cnt - 1) % delimcnt])) 168 putchar(ch); 169 (void)printf("%s", buf); 170 } 171 if (output) 172 putchar('\n'); 173 } 174 } 175 176 void 177 sequential(argv) 178 char **argv; 179 { 180 register FILE *fp; 181 register int cnt; 182 register char ch, *p, *dp; 183 char buf[_POSIX2_LINE_MAX + 1]; 184 185 for (; (p = *argv); ++argv) { 186 if (p[0] == '-' && !p[1]) 187 fp = stdin; 188 else if (!(fp = fopen(p, "r"))) { 189 warn("%s", p); 190 continue; 191 } 192 if (fgets(buf, sizeof(buf), fp)) { 193 for (cnt = 0, dp = delim;;) { 194 if (!(p = index(buf, '\n'))) 195 errx(1, "%s: input line too long", *argv); 196 *p = '\0'; 197 (void)printf("%s", buf); 198 if (!fgets(buf, sizeof(buf), fp)) 199 break; 200 if ((ch = *dp++)) 201 putchar(ch); 202 if (++cnt == delimcnt) { 203 dp = delim; 204 cnt = 0; 205 } 206 } 207 putchar('\n'); 208 } 209 if (fp != stdin) 210 (void)fclose(fp); 211 } 212 } 213 214 int 215 tr(arg) 216 char *arg; 217 { 218 register int cnt; 219 register char ch, *p; 220 221 for (p = arg, cnt = 0; (ch = *p++); ++arg, ++cnt) 222 if (ch == '\\') 223 switch(ch = *p++) { 224 case 'n': 225 *arg = '\n'; 226 break; 227 case 't': 228 *arg = '\t'; 229 break; 230 case '0': 231 *arg = '\0'; 232 break; 233 default: 234 *arg = ch; 235 break; 236 } else 237 *arg = ch; 238 239 if (!cnt) 240 errx(1, "no delimiters specified"); 241 return(cnt); 242 } 243 244 static void 245 usage() 246 { 247 (void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n"); 248 exit(1); 249 } 250