1 /* $OpenBSD: paste.c,v 1.26 2018/08/04 19:19:37 schwarze Exp $ */
4 * Copyright (c) 1989 The Regents of the University of California.
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/queue.h>
36 #include <sys/types.h>
49 __dead
void usage(void);
50 void parallel(char **);
51 void sequential(char **);
54 main(int argc
, char *argv
[])
60 if (pledge("stdio rpath", NULL
) == -1)
64 while ((ch
= getopt(argc
, argv
, "d:s")) != -1) {
67 delimcnt
= tr(delim
= optarg
);
96 SIMPLEQ_ENTRY(list
) entries
;
103 parallel(char **argv
)
105 SIMPLEQ_HEAD(, list
) head
= SIMPLEQ_HEAD_INITIALIZER(head
);
114 for (cnt
= 0; (p
= *argv
) != NULL
; ++argv
, ++cnt
) {
115 if ((lp
= malloc(sizeof(*lp
))) == NULL
)
118 if (p
[0] == '-' && p
[1] == '\0')
120 else if ((lp
->fp
= fopen(p
, "r")) == NULL
)
124 SIMPLEQ_INSERT_TAIL(&head
, lp
, entries
);
130 for (opencnt
= cnt
; opencnt
;) {
132 SIMPLEQ_FOREACH(lp
, &head
, entries
) {
133 if (lp
->fp
== NULL
) {
134 if (output
&& lp
->cnt
&&
135 (ch
= delim
[(lp
->cnt
- 1) % delimcnt
]))
139 if ((len
= getline(&line
, &linesize
, lp
->fp
)) == -1) {
141 err(1, "%s", lp
->fp
== stdin
?
142 "getline" : lp
->name
);
148 if (output
&& lp
->cnt
&&
149 (ch
= delim
[(lp
->cnt
- 1) % delimcnt
]))
153 if (line
[len
- 1] == '\n')
154 line
[len
- 1] = '\0';
156 * make sure that we don't print any delimiters
157 * unless there's a non-empty file.
161 for (cnt
= 0; cnt
< lp
->cnt
; ++cnt
)
162 if ((ch
= delim
[cnt
% delimcnt
]))
164 } else if ((ch
= delim
[(lp
->cnt
- 1) % delimcnt
]))
175 sequential(char **argv
)
185 for (; (p
= *argv
) != NULL
; ++argv
) {
186 if (p
[0] == '-' && p
[1] == '\0')
188 else if ((fp
= fopen(p
, "r")) == NULL
) {
193 while ((len
= getline(&line
, &linesize
, fp
)) != -1) {
194 if (line
[len
- 1] == '\n')
195 line
[len
- 1] = '\0';
198 if (++cnt
== delimcnt
)
203 err(1, "%s", fp
== stdin
? "getline" : p
);
218 for (p
= arg
, cnt
= 0; (ch
= *p
++) != '\0'; ++arg
, ++cnt
) {
239 errx(1, "no delimiters specified");
246 extern char *__progname
;
247 fprintf(stderr
, "usage: %s [-s] [-d list] file ...\n", __progname
);