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