1 /* $NetBSD: lam.c,v 1.6 2008/07/21 14:19:23 lukem Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)lam.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: lam.c,v 1.6 2008/07/21 14:19:23 lukem Exp $");
46 * lam - laminate files
47 * Author: John Kunze, UCB
57 #define BIGBUFSIZ 5 * BUFSIZ
59 struct openfile
{ /* open file structure */
60 FILE *fp
; /* file pointer */
61 short eof
; /* eof flag */
62 short pad
; /* pad flag for missing columns */
63 char eol
; /* end of line character */
64 const char *sepstring
; /* string to print before each line */
65 const char *format
; /* printf(3) style string spec. */
68 int morefiles
; /* set by getargs(), changed by gatherline() */
69 int nofinalnl
; /* normally append \n to each output line */
73 void error
__P((const char *, const char *));
74 char *gatherline
__P((struct openfile
*));
75 void getargs
__P((char *[]));
76 int main
__P((int, char **));
77 char *pad
__P((struct openfile
*));
88 error("lam - laminate files", "");
91 for (ip
= input
; ip
->fp
!= NULL
; ip
++)
92 linep
= gatherline(ip
);
96 fputs(ip
->sepstring
, stdout
);
106 struct openfile
*ip
= input
;
108 static char fmtbuf
[BUFSIZ
];
112 P
= S
= F
= T
= 0; /* capitalized options */
113 while ((p
= *++av
) != NULL
) {
114 if (*p
!= '-' || !p
[1]) {
115 if (++morefiles
>= MAXOFILES
)
116 errx(1, "too many input files");
119 else if ((ip
->fp
= fopen(p
, "r")) == NULL
)
120 errx(1, "open %s", p
);
123 ip
->sepstring
= (S
? (ip
-1)->sepstring
: "");
125 ip
->format
= ((P
|| F
) ? (ip
-1)->format
: "%s");
127 ip
->eol
= (T
? (ip
-1)->eol
: '\n');
132 switch (tolower((unsigned char) *c
)) {
134 if (*++p
|| (p
= *++av
))
137 error("Need string after -%s", c
);
138 S
= (*c
== 'S' ? 1 : 0);
141 if (*++p
|| (p
= *++av
))
144 error("Need character after -%s", c
);
145 T
= (*c
== 'T' ? 1 : 0);
150 P
= (*c
== 'P' ? 1 : 0);
153 F
= (*c
== 'F' ? 1 : 0);
154 if (*++p
|| (p
= *++av
)) {
155 fmtp
+= strlen(fmtp
) + 1;
156 if (fmtp
>= fmtbuf
+ sizeof(fmtbuf
))
157 errx(1, "no more format space");
158 /* restrict format string to only valid width formatters */
159 if (strspn(p
, "-.0123456789") != strlen(p
))
160 errx(1, "invalid format string `%s'", p
);
161 if (snprintf(fmtp
, fmtbuf
+ sizeof(fmtbuf
) - fmtp
, "%%%ss", p
)
162 >= fmtbuf
+ sizeof(fmtbuf
) - fmtp
)
163 errx(1, "no more format space");
164 sprintf(fmtp
, "%%%ss", p
);
168 error("Need string after -%s", c
);
171 error("What do you mean by -%s?", c
);
186 strlcpy(lp
, ip
->sepstring
, line
+ sizeof(line
) - lp
);
189 snprintf(lp
, line
+ sizeof(line
) - lp
, ip
->format
, "");
203 char *end
= s
+ sizeof(s
) - 1;
207 for (p
= s
; (c
= fgetc(ip
->fp
)) != EOF
&& p
< end
; p
++)
208 if ((*p
= c
) == ip
->eol
)
218 strlcpy(lp
, ip
->sepstring
, line
+ sizeof(line
) - lp
);
220 snprintf(lp
, line
+ sizeof(line
) - lp
, ip
->format
, s
);
226 error(const char *msg
, const char *s
)
230 "\nUsage: lam [ -[fp] min.max ] [ -s sepstring ] [ -t c ] file ...\n");
231 if (strncmp("lam - ", msg
, 6) == 0)
232 fprintf(stderr
, "Options:\n\t%s\t%s\t%s\t%s\t%s",
233 "-f min.max field widths for file fragments\n",
234 "-p min.max like -f, but pad missing fragments\n",
235 "-s sepstring fragment separator\n",
236 "-t c input line terminator is c, no \\n after output lines\n",
237 "Capitalized options affect more than one file.\n");