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