1 /* $NetBSD: paste.c,v 1.7 2003/08/07 11:15:28 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"); 39 #endif /* not lint */ 40 41 #ifndef lint 42 /*static char sccsid[] = "from: @(#)paste.c 8.1 (Berkeley) 6/6/93";*/ 43 __RCSID("$NetBSD: paste.c,v 1.7 2003/08/07 11:15:28 agc Exp $"); 44 #endif /* not lint */ 45 46 #include <sys/types.h> 47 #include <err.h> 48 #include <errno.h> 49 #include <limits.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <unistd.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 int main __P((int, char **)); 57 void parallel __P((char **)); 58 void sequential __P((char **)); 59 int tr __P((char *)); 60 void usage __P((void)); 61 62 char *delim; 63 int delimcnt; 64 65 int 66 main(argc, argv) 67 int argc; 68 char **argv; 69 { 70 int ch, seq; 71 72 seq = 0; 73 while ((ch = getopt(argc, argv, "d:s")) != -1) 74 switch(ch) { 75 case 'd': 76 delimcnt = tr(delim = optarg); 77 break; 78 case 's': 79 seq = 1; 80 break; 81 case '?': 82 default: 83 usage(); 84 } 85 argc -= optind; 86 argv += optind; 87 88 if (!delim) { 89 delimcnt = 1; 90 delim = "\t"; 91 } 92 93 if (seq) 94 sequential(argv); 95 else 96 parallel(argv); 97 exit(0); 98 } 99 100 typedef struct _list { 101 struct _list *next; 102 FILE *fp; 103 int cnt; 104 char *name; 105 } LIST; 106 107 void 108 parallel(argv) 109 char **argv; 110 { 111 LIST *lp; 112 int cnt; 113 char ch, *p; 114 LIST *head, *tmp; 115 int opencnt, output; 116 char buf[_POSIX2_LINE_MAX + 1]; 117 118 tmp = NULL; 119 for (cnt = 0, head = NULL; (p = *argv) != NULL; ++argv, ++cnt) { 120 if (!(lp = (LIST *)malloc((u_int)sizeof(LIST)))) 121 err(1, "malloc"); 122 if (p[0] == '-' && !p[1]) 123 lp->fp = stdin; 124 else if (!(lp->fp = fopen(p, "r"))) 125 err(1, "%s", p); 126 lp->next = NULL; 127 lp->cnt = cnt; 128 lp->name = p; 129 if (!head) 130 head = tmp = lp; 131 else { 132 tmp->next = lp; 133 tmp = lp; 134 } 135 } 136 137 for (opencnt = cnt; opencnt;) { 138 for (output = 0, lp = head; lp; lp = lp->next) { 139 if (!lp->fp) { 140 if (output && lp->cnt && 141 (ch = delim[(lp->cnt - 1) % delimcnt])) 142 putchar(ch); 143 continue; 144 } 145 if (!fgets(buf, sizeof(buf), lp->fp)) { 146 if (!--opencnt) 147 break; 148 lp->fp = NULL; 149 if (output && lp->cnt && 150 (ch = delim[(lp->cnt - 1) % delimcnt])) 151 putchar(ch); 152 continue; 153 } 154 if (!(p = strchr(buf, '\n'))) 155 err(1, "%s: input line too long.", lp->name); 156 *p = '\0'; 157 /* 158 * make sure that we don't print any delimiters 159 * unless there's a non-empty file. 160 */ 161 if (!output) { 162 output = 1; 163 for (cnt = 0; cnt < lp->cnt; ++cnt) 164 if ((ch = delim[cnt % delimcnt]) != 0) 165 putchar(ch); 166 } else if ((ch = delim[(lp->cnt - 1) % delimcnt]) != 0) 167 putchar(ch); 168 (void)printf("%s", buf); 169 } 170 if (output) 171 putchar('\n'); 172 } 173 } 174 175 void 176 sequential(argv) 177 char **argv; 178 { 179 FILE *fp; 180 int cnt; 181 char ch, *p, *dp; 182 char buf[_POSIX2_LINE_MAX + 1]; 183 184 for (; (p = *argv) != NULL; ++argv) { 185 if (p[0] == '-' && !p[1]) 186 fp = stdin; 187 else if (!(fp = fopen(p, "r"))) { 188 warn("%s", p); 189 continue; 190 } 191 if (fgets(buf, sizeof(buf), fp)) { 192 for (cnt = 0, dp = delim;;) { 193 if (!(p = strchr(buf, '\n'))) 194 err(1, "%s: input line too long.", 195 *argv); 196 *p = '\0'; 197 (void)printf("%s", buf); 198 if (!fgets(buf, sizeof(buf), fp)) 199 break; 200 if ((ch = *dp++) != 0) 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 int cnt; 219 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 void 245 usage() 246 { 247 (void)fprintf(stderr, "paste: [-s] [-d delimiters] file ...\n"); 248 exit(1); 249 } 250