2 * SPDX-License-Identifier: BSD-3-Clause
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)lam.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * lam - laminate files
48 * Author: John Kunze, UCB
51 #include <sys/capsicum.h>
53 #include <capsicum_helpers.h>
63 #define BIGBUFSIZ 5 * BUFSIZ
65 static struct openfile { /* open file structure */
66 FILE *fp; /* file pointer */
67 short eof; /* eof flag */
68 short pad; /* pad flag for missing columns */
69 char eol; /* end of line character */
70 const char *sepstring; /* string to print before each line */
71 const char *format; /* printf(3) style string spec. */
74 static int morefiles; /* set by getargs(), changed by gatherline() */
75 static int nofinalnl; /* normally append \n to each output line */
76 static char line[BIGBUFSIZ];
79 static char *gatherline(struct openfile *);
80 static void getargs(char *[]);
81 static char *pad(struct openfile *);
82 static void usage(void);
85 main(int argc, char *argv[])
91 if (caph_limit_stdio() == -1)
92 err(1, "unable to limit stdio");
98 * Cache NLS data, for strerror, for err(3), before entering capability
101 caph_cache_catpages();
102 if (caph_enter() < 0)
103 err(1, "unable to enter capability mode");
107 for (ip = input; ip->fp != NULL; ip++)
108 linep = gatherline(ip);
112 fputs(ip->sepstring, stdout);
121 struct openfile *ip = input;
123 static char fmtbuf[BUFSIZ];
126 cap_rights_t rights_ro;
128 cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT);
129 P = S = F = T = 0; /* capitalized options */
130 while ((p = *++av) != NULL) {
131 if (*p != '-' || !p[1]) {
132 if (++morefiles >= MAXOFILES)
133 errx(1, "too many input files");
136 else if ((ip->fp = fopen(p, "r")) == NULL) {
139 if (caph_rights_limit(fileno(ip->fp), &rights_ro) < 0)
140 err(1, "unable to limit rights on: %s", p);
143 ip->sepstring = (S ? (ip-1)->sepstring : "");
145 ip->format = ((P || F) ? (ip-1)->format : "%s");
147 ip->eol = (T ? (ip-1)->eol : '\n');
152 switch (tolower((unsigned char)*c)) {
154 if (*++p || (p = *++av))
158 S = (*c == 'S' ? 1 : 0);
161 if (*++p || (p = *++av))
165 T = (*c == 'T' ? 1 : 0);
170 P = (*c == 'P' ? 1 : 0);
173 F = (*c == 'F' ? 1 : 0);
174 if (*++p || (p = *++av)) {
175 fmtp += strlen(fmtp) + 1;
176 if (fmtp >= fmtbuf + sizeof(fmtbuf))
177 errx(1, "no more format space");
178 /* restrict format string to only valid width formatters */
179 if (strspn(p, "-.0123456789") != strlen(p))
180 errx(1, "invalid format string `%s'", p);
181 if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p)
182 >= fmtbuf + sizeof(fmtbuf) - fmtp)
183 errx(1, "no more format space");
199 pad(struct openfile *ip)
203 strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
206 snprintf(lp, line + sizeof(line) - lp, ip->format, "");
213 gatherline(struct openfile *ip)
219 char *end = s + sizeof(s) - 1;
223 for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
224 if ((*p = c) == ip->eol)
234 strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
236 snprintf(lp, line + sizeof(line) - lp, ip->format, s);
244 fprintf(stderr, "%s\n%s\n",
245 "usage: lam [ -f min.max ] [ -s sepstring ] [ -t c ] file ...",
246 " lam [ -p min.max ] [ -s sepstring ] [ -t c ] file ...");