1 /* $NetBSD: paste.c,v 1.14 2008/07/21 14:19:24 lukem Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Adam S. Moskowitz of Menlo Consulting.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 The Regents of the University of California. All rights reserved.");
42 /*static char sccsid[] = "from: @(#)paste.c 8.1 (Berkeley) 6/6/93";*/
43 __RCSID("$NetBSD: paste.c,v 1.14 2008/07/21 14:19:24 lukem Exp $");
46 #include <sys/types.h>
55 void parallel(int, char **);
56 void sequential(char **);
60 char dflt_delim
[] = "\t";
61 char *delim
= dflt_delim
;
65 main(int argc
, char **argv
)
70 while ((ch
= getopt(argc
, argv
, "d:s")) != -1) {
73 delim
= strdup(optarg
);
95 parallel(int argc
, char **argv
)
102 fpp
= calloc(argc
, sizeof *fpp
);
106 for (cnt
= 0; cnt
< argc
; cnt
++) {
107 if (strcmp(argv
[cnt
], "-") == 0)
109 else if (!(fpp
[cnt
] = fopen(argv
[cnt
], "r")))
110 err(1, "%s", argv
[cnt
]);
114 /* Start with the NUL at the end of 'delim' ... */
115 dp
= delim
+ delimcnt
;
117 for (cnt
= 0; cnt
< argc
; cnt
++) {
121 line
= fgetln(fp
, &line_len
);
129 /* Output enough separators to catch up */
134 if (dp
>= delim
+ delimcnt
)
136 } while (++output
<= cnt
);
137 /* Remove any trailing newline - check for last line */
138 if (line
[line_len
- 1] == '\n')
140 printf("%.*s", (int)line_len
, line
);
146 /* Add separators to end of line */
147 while (++output
<= cnt
) {
151 if (dp
>= delim
+ delimcnt
)
161 sequential(char **argv
)
166 char buf
[_POSIX2_LINE_MAX
+ 1];
168 for (; (p
= *argv
) != NULL
; ++argv
) {
169 if (p
[0] == '-' && !p
[1])
171 else if (!(fp
= fopen(p
, "r"))) {
175 if (fgets(buf
, sizeof(buf
), fp
)) {
176 for (cnt
= 0, dp
= delim
;;) {
177 if (!(p
= strchr(buf
, '\n')))
178 err(1, "%s: input line too long.",
181 (void)printf("%s", buf
);
182 if (!fgets(buf
, sizeof(buf
), fp
))
184 if ((ch
= *dp
++) != 0)
186 if (++cnt
== delimcnt
) {
204 for (p
= arg
, cnt
= 0; (ch
= *p
++); ++arg
, ++cnt
)
223 errx(1, "no delimiters specified.");
231 (void)fprintf(stderr
, "paste: [-s] [-d delimiters] file ...\n");