1 /* $NetBSD: lam.c,v 1.8 2011/09/04 20:28:09 joerg 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.8 2011/09/04 20:28:09 joerg 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 static int morefiles
; /* set by getargs(), changed by gatherline() */
69 static int nofinalnl
; /* normally append \n to each output line */
70 static char line
[BIGBUFSIZ
];
73 __dead
static void error(const char *, const char *);
74 static char *gatherline(struct openfile
*);
75 static void getargs(char *[]);
76 static char *pad(struct openfile
*);
79 main(int argc
, char *argv
[])
85 error("lam - laminate files", "");
88 for (ip
= input
; ip
->fp
!= NULL
; ip
++)
89 linep
= gatherline(ip
);
93 fputs(ip
->sepstring
, stdout
);
102 struct openfile
*ip
= input
;
104 static char fmtbuf
[BUFSIZ
];
108 P
= S
= F
= T
= 0; /* capitalized options */
109 while ((p
= *++av
) != NULL
) {
110 if (*p
!= '-' || !p
[1]) {
111 if (++morefiles
>= MAXOFILES
)
112 errx(1, "too many input files");
115 else if ((ip
->fp
= fopen(p
, "r")) == NULL
)
116 errx(1, "open %s", p
);
119 ip
->sepstring
= (S
? (ip
-1)->sepstring
: "");
121 ip
->format
= ((P
|| F
) ? (ip
-1)->format
: "%s");
123 ip
->eol
= (T
? (ip
-1)->eol
: '\n');
128 switch (tolower((unsigned char) *c
)) {
130 if (*++p
|| (p
= *++av
))
133 error("Need string after -%s", c
);
134 S
= (*c
== 'S' ? 1 : 0);
137 if (*++p
|| (p
= *++av
))
140 error("Need character after -%s", c
);
141 T
= (*c
== 'T' ? 1 : 0);
146 P
= (*c
== 'P' ? 1 : 0);
149 F
= (*c
== 'F' ? 1 : 0);
150 if (*++p
|| (p
= *++av
)) {
151 fmtp
+= strlen(fmtp
) + 1;
152 if (fmtp
>= fmtbuf
+ sizeof(fmtbuf
))
153 errx(1, "no more format space");
154 /* restrict format string to only valid width formatters */
155 if (strspn(p
, "-.0123456789") != strlen(p
))
156 errx(1, "invalid format string `%s'", p
);
157 if (snprintf(fmtp
, fmtbuf
+ sizeof(fmtbuf
) - fmtp
, "%%%ss", p
)
158 >= fmtbuf
+ sizeof(fmtbuf
) - fmtp
)
159 errx(1, "no more format space");
160 sprintf(fmtp
, "%%%ss", p
);
164 error("Need string after -%s", c
);
167 error("What do you mean by -%s?", c
);
177 pad(struct openfile
*ip
)
181 strlcpy(lp
, ip
->sepstring
, line
+ sizeof(line
) - lp
);
184 snprintf(lp
, line
+ sizeof(line
) - lp
, ip
->format
, "");
191 gatherline(struct openfile
*ip
)
197 char *end
= s
+ sizeof(s
) - 1;
201 for (p
= s
; (c
= fgetc(ip
->fp
)) != EOF
&& p
< end
; p
++)
202 if ((*p
= c
) == ip
->eol
)
212 strlcpy(lp
, ip
->sepstring
, line
+ sizeof(line
) - lp
);
214 snprintf(lp
, line
+ sizeof(line
) - lp
, ip
->format
, s
);
220 error(const char *msg
, const char *s
)
224 "\nUsage: lam [ -[fp] min.max ] [ -s sepstring ] [ -t c ] file ...\n");
225 if (strncmp("lam - ", msg
, 6) == 0)
226 fprintf(stderr
, "Options:\n\t%s\t%s\t%s\t%s\t%s",
227 "-f min.max field widths for file fragments\n",
228 "-p min.max like -f, but pad missing fragments\n",
229 "-s sepstring fragment separator\n",
230 "-t c input line terminator is c, no \\n after output lines\n",
231 "Capitalized options affect more than one file.\n");