19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329b50d902SRodney W. Grimes /*
339b50d902SRodney W. Grimes * lam - laminate files
349b50d902SRodney W. Grimes * Author: John Kunze, UCB
359b50d902SRodney W. Grimes */
369b50d902SRodney W. Grimes
37ba032055SAllan Jude #include <sys/capsicum.h>
38ba032055SAllan Jude
39ba032055SAllan Jude #include <capsicum_helpers.h>
40aeacf525SMike Heffner #include <ctype.h>
41df899658SPhilippe Charnier #include <err.h>
42ba032055SAllan Jude #include <errno.h>
439b50d902SRodney W. Grimes #include <stdio.h>
449b50d902SRodney W. Grimes #include <stdlib.h>
459b50d902SRodney W. Grimes #include <string.h>
46*4472fd66SCosimo Cecchi #include <sysexits.h>
47ba032055SAllan Jude #include <unistd.h>
489b50d902SRodney W. Grimes
499b50d902SRodney W. Grimes #define MAXOFILES 20
509b50d902SRodney W. Grimes #define BIGBUFSIZ 5 * BUFSIZ
519b50d902SRodney W. Grimes
5286350df6SEd Schouten static struct openfile { /* open file structure */
539b50d902SRodney W. Grimes FILE *fp; /* file pointer */
549b50d902SRodney W. Grimes short eof; /* eof flag */
559b50d902SRodney W. Grimes short pad; /* pad flag for missing columns */
569b50d902SRodney W. Grimes char eol; /* end of line character */
57b5b9b430SMike Barcroft const char *sepstring; /* string to print before each line */
58b5b9b430SMike Barcroft const char *format; /* printf(3) style string spec. */
599b50d902SRodney W. Grimes } input[MAXOFILES];
609b50d902SRodney W. Grimes
6186350df6SEd Schouten static int morefiles; /* set by getargs(), changed by gatherline() */
6286350df6SEd Schouten static int nofinalnl; /* normally append \n to each output line */
6386350df6SEd Schouten static char line[BIGBUFSIZ];
6486350df6SEd Schouten static char *linep;
659b50d902SRodney W. Grimes
66b5b9b430SMike Barcroft static char *gatherline(struct openfile *);
67b5b9b430SMike Barcroft static void getargs(char *[]);
68b5b9b430SMike Barcroft static char *pad(struct openfile *);
697a536c3bSMike Heffner static void usage(void);
709b50d902SRodney W. Grimes
719b50d902SRodney W. Grimes int
main(int argc,char * argv[])72f63eec78SJuli Mallett main(int argc, char *argv[])
739b50d902SRodney W. Grimes {
74aeacf525SMike Heffner struct openfile *ip;
759b50d902SRodney W. Grimes
76f201bc9cSJuli Mallett if (argc == 1)
77df899658SPhilippe Charnier usage();
780064a5b3SBaptiste Daroussin if (caph_limit_stdio() == -1)
790064a5b3SBaptiste Daroussin err(1, "unable to limit stdio");
80f63eec78SJuli Mallett getargs(argv);
81f201bc9cSJuli Mallett if (!morefiles)
82f201bc9cSJuli Mallett usage();
83ba032055SAllan Jude
84ba032055SAllan Jude /*
85ba032055SAllan Jude * Cache NLS data, for strerror, for err(3), before entering capability
86ba032055SAllan Jude * mode.
87ba032055SAllan Jude */
88ba032055SAllan Jude caph_cache_catpages();
897672a014SMariusz Zaborski if (caph_enter() < 0)
90ba032055SAllan Jude err(1, "unable to enter capability mode");
91ba032055SAllan Jude
929b50d902SRodney W. Grimes for (;;) {
939b50d902SRodney W. Grimes linep = line;
949b50d902SRodney W. Grimes for (ip = input; ip->fp != NULL; ip++)
959b50d902SRodney W. Grimes linep = gatherline(ip);
969b50d902SRodney W. Grimes if (!morefiles)
979b50d902SRodney W. Grimes exit(0);
989b50d902SRodney W. Grimes fputs(line, stdout);
999b50d902SRodney W. Grimes fputs(ip->sepstring, stdout);
1009b50d902SRodney W. Grimes if (!nofinalnl)
1019b50d902SRodney W. Grimes putchar('\n');
1029b50d902SRodney W. Grimes }
1039b50d902SRodney W. Grimes }
1049b50d902SRodney W. Grimes
105b5b9b430SMike Barcroft static void
getargs(char * av[])1068ecfa014SMike Heffner getargs(char *av[])
1079b50d902SRodney W. Grimes {
108aeacf525SMike Heffner struct openfile *ip = input;
109aeacf525SMike Heffner char *p, *c;
1109b50d902SRodney W. Grimes static char fmtbuf[BUFSIZ];
1119b50d902SRodney W. Grimes char *fmtp = fmtbuf;
1129b50d902SRodney W. Grimes int P, S, F, T;
113ba032055SAllan Jude cap_rights_t rights_ro;
1149b50d902SRodney W. Grimes
115ba032055SAllan Jude cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT);
1169b50d902SRodney W. Grimes P = S = F = T = 0; /* capitalized options */
1179b50d902SRodney W. Grimes while ((p = *++av) != NULL) {
1189b50d902SRodney W. Grimes if (*p != '-' || !p[1]) {
119f201bc9cSJuli Mallett if (++morefiles >= MAXOFILES)
120f201bc9cSJuli Mallett errx(1, "too many input files");
121f201bc9cSJuli Mallett if (*p == '-')
1229b50d902SRodney W. Grimes ip->fp = stdin;
123f201bc9cSJuli Mallett else if ((ip->fp = fopen(p, "r")) == NULL) {
1240c4d24a7SKris Kennaway err(1, "%s", p);
1259b50d902SRodney W. Grimes }
126377421dfSMariusz Zaborski if (caph_rights_limit(fileno(ip->fp), &rights_ro) < 0)
127ba032055SAllan Jude err(1, "unable to limit rights on: %s", p);
1289b50d902SRodney W. Grimes ip->pad = P;
1299b50d902SRodney W. Grimes if (!ip->sepstring)
1309b50d902SRodney W. Grimes ip->sepstring = (S ? (ip-1)->sepstring : "");
1319b50d902SRodney W. Grimes if (!ip->format)
1329b50d902SRodney W. Grimes ip->format = ((P || F) ? (ip-1)->format : "%s");
1339b50d902SRodney W. Grimes if (!ip->eol)
1349b50d902SRodney W. Grimes ip->eol = (T ? (ip-1)->eol : '\n');
1359b50d902SRodney W. Grimes ip++;
1369b50d902SRodney W. Grimes continue;
1379b50d902SRodney W. Grimes }
138aeacf525SMike Heffner c = ++p;
139be1e385eSTim J. Robbins switch (tolower((unsigned char)*c)) {
1409b50d902SRodney W. Grimes case 's':
1419b50d902SRodney W. Grimes if (*++p || (p = *++av))
1429b50d902SRodney W. Grimes ip->sepstring = p;
1439b50d902SRodney W. Grimes else
144dfaacec6SJuli Mallett usage();
1459b50d902SRodney W. Grimes S = (*c == 'S' ? 1 : 0);
1469b50d902SRodney W. Grimes break;
1479b50d902SRodney W. Grimes case 't':
1489b50d902SRodney W. Grimes if (*++p || (p = *++av))
1499b50d902SRodney W. Grimes ip->eol = *p;
1509b50d902SRodney W. Grimes else
151dfaacec6SJuli Mallett usage();
1529b50d902SRodney W. Grimes T = (*c == 'T' ? 1 : 0);
1539b50d902SRodney W. Grimes nofinalnl = 1;
1549b50d902SRodney W. Grimes break;
1559b50d902SRodney W. Grimes case 'p':
1569b50d902SRodney W. Grimes ip->pad = 1;
1579b50d902SRodney W. Grimes P = (*c == 'P' ? 1 : 0);
158aeacf525SMike Heffner /* FALLTHROUGH */
1599b50d902SRodney W. Grimes case 'f':
1609b50d902SRodney W. Grimes F = (*c == 'F' ? 1 : 0);
1619b50d902SRodney W. Grimes if (*++p || (p = *++av)) {
1629b50d902SRodney W. Grimes fmtp += strlen(fmtp) + 1;
163aeacf525SMike Heffner if (fmtp >= fmtbuf + sizeof(fmtbuf))
164df899658SPhilippe Charnier errx(1, "no more format space");
165aeacf525SMike Heffner /* restrict format string to only valid width formatters */
166aeacf525SMike Heffner if (strspn(p, "-.0123456789") != strlen(p))
167aeacf525SMike Heffner errx(1, "invalid format string `%s'", p);
168aeacf525SMike Heffner if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p)
169aeacf525SMike Heffner >= fmtbuf + sizeof(fmtbuf) - fmtp)
170aeacf525SMike Heffner errx(1, "no more format space");
1719b50d902SRodney W. Grimes ip->format = fmtp;
1729b50d902SRodney W. Grimes }
1739b50d902SRodney W. Grimes else
174dfaacec6SJuli Mallett usage();
1759b50d902SRodney W. Grimes break;
1769b50d902SRodney W. Grimes default:
177dfaacec6SJuli Mallett usage();
1789b50d902SRodney W. Grimes }
1799b50d902SRodney W. Grimes }
1809b50d902SRodney W. Grimes ip->fp = NULL;
1819b50d902SRodney W. Grimes if (!ip->sepstring)
1829b50d902SRodney W. Grimes ip->sepstring = "";
1839b50d902SRodney W. Grimes }
1849b50d902SRodney W. Grimes
185b5b9b430SMike Barcroft static char *
pad(struct openfile * ip)1867a536c3bSMike Heffner pad(struct openfile *ip)
1879b50d902SRodney W. Grimes {
188aeacf525SMike Heffner char *lp = linep;
1899b50d902SRodney W. Grimes
190aeacf525SMike Heffner strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
191aeacf525SMike Heffner lp += strlen(lp);
1929b50d902SRodney W. Grimes if (ip->pad) {
193aeacf525SMike Heffner snprintf(lp, line + sizeof(line) - lp, ip->format, "");
1949b50d902SRodney W. Grimes lp += strlen(lp);
1959b50d902SRodney W. Grimes }
1969b50d902SRodney W. Grimes return (lp);
1979b50d902SRodney W. Grimes }
1989b50d902SRodney W. Grimes
199b5b9b430SMike Barcroft static char *
gatherline(struct openfile * ip)2007a536c3bSMike Heffner gatherline(struct openfile *ip)
2019b50d902SRodney W. Grimes {
2029b50d902SRodney W. Grimes char s[BUFSIZ];
203aeacf525SMike Heffner int c;
204aeacf525SMike Heffner char *p;
205aeacf525SMike Heffner char *lp = linep;
206aeacf525SMike Heffner char *end = s + sizeof(s) - 1;
2079b50d902SRodney W. Grimes
2089b50d902SRodney W. Grimes if (ip->eof)
2099b50d902SRodney W. Grimes return (pad(ip));
2109b50d902SRodney W. Grimes for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
2119b50d902SRodney W. Grimes if ((*p = c) == ip->eol)
2129b50d902SRodney W. Grimes break;
2139b50d902SRodney W. Grimes *p = '\0';
2149b50d902SRodney W. Grimes if (c == EOF) {
2159b50d902SRodney W. Grimes ip->eof = 1;
216*4472fd66SCosimo Cecchi if (ferror(ip->fp)) {
217*4472fd66SCosimo Cecchi err(EX_IOERR, NULL);
218*4472fd66SCosimo Cecchi }
2199b50d902SRodney W. Grimes if (ip->fp == stdin)
2209b50d902SRodney W. Grimes fclose(stdin);
2219b50d902SRodney W. Grimes morefiles--;
2229b50d902SRodney W. Grimes return (pad(ip));
2239b50d902SRodney W. Grimes }
224aeacf525SMike Heffner strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
225aeacf525SMike Heffner lp += strlen(lp);
226aeacf525SMike Heffner snprintf(lp, line + sizeof(line) - lp, ip->format, s);
2279b50d902SRodney W. Grimes lp += strlen(lp);
2289b50d902SRodney W. Grimes return (lp);
2299b50d902SRodney W. Grimes }
2309b50d902SRodney W. Grimes
231df899658SPhilippe Charnier static void
usage(void)232ef636796SEd Schouten usage(void)
2339b50d902SRodney W. Grimes {
234df899658SPhilippe Charnier fprintf(stderr, "%s\n%s\n",
235df899658SPhilippe Charnier "usage: lam [ -f min.max ] [ -s sepstring ] [ -t c ] file ...",
236df899658SPhilippe Charnier " lam [ -p min.max ] [ -s sepstring ] [ -t c ] file ...");
2379b50d902SRodney W. Grimes exit(1);
2389b50d902SRodney W. Grimes }
239