1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32/*
33 *	lam - laminate files
34 *	Author:  John Kunze, UCB
35 */
36
37#include <sys/capsicum.h>
38
39#include <capsicum_helpers.h>
40#include <ctype.h>
41#include <err.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48#define	MAXOFILES	20
49#define	BIGBUFSIZ	5 * BUFSIZ
50
51static struct openfile {	/* open file structure */
52	FILE	*fp;		/* file pointer */
53	short	eof;		/* eof flag */
54	short	pad;		/* pad flag for missing columns */
55	char	eol;		/* end of line character */
56	const char *sepstring;	/* string to print before each line */
57	const char *format;	/* printf(3) style string spec. */
58}	input[MAXOFILES];
59
60static int	morefiles;	/* set by getargs(), changed by gatherline() */
61static int	nofinalnl;	/* normally append \n to each output line */
62static char	line[BIGBUFSIZ];
63static char	*linep;
64
65static char    *gatherline(struct openfile *);
66static void	getargs(char *[]);
67static char    *pad(struct openfile *);
68static void	usage(void);
69
70int
71main(int argc, char *argv[])
72{
73	struct	openfile *ip;
74
75	if (argc == 1)
76		usage();
77	if (caph_limit_stdio() == -1)
78		err(1, "unable to limit stdio");
79	getargs(argv);
80	if (!morefiles)
81		usage();
82
83	/*
84	 * Cache NLS data, for strerror, for err(3), before entering capability
85	 * mode.
86	 */
87	caph_cache_catpages();
88	if (caph_enter() < 0)
89		err(1, "unable to enter capability mode");
90
91	for (;;) {
92		linep = line;
93		for (ip = input; ip->fp != NULL; ip++)
94			linep = gatherline(ip);
95		if (!morefiles)
96			exit(0);
97		fputs(line, stdout);
98		fputs(ip->sepstring, stdout);
99		if (!nofinalnl)
100			putchar('\n');
101	}
102}
103
104static void
105getargs(char *av[])
106{
107	struct	openfile *ip = input;
108	char *p, *c;
109	static char fmtbuf[BUFSIZ];
110	char *fmtp = fmtbuf;
111	int P, S, F, T;
112	cap_rights_t rights_ro;
113
114	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT);
115	P = S = F = T = 0;		/* capitalized options */
116	while ((p = *++av) != NULL) {
117		if (*p != '-' || !p[1]) {
118			if (++morefiles >= MAXOFILES)
119				errx(1, "too many input files");
120			if (*p == '-')
121				ip->fp = stdin;
122			else if ((ip->fp = fopen(p, "r")) == NULL) {
123				err(1, "%s", p);
124			}
125			if (caph_rights_limit(fileno(ip->fp), &rights_ro) < 0)
126				err(1, "unable to limit rights on: %s", p);
127			ip->pad = P;
128			if (!ip->sepstring)
129				ip->sepstring = (S ? (ip-1)->sepstring : "");
130			if (!ip->format)
131				ip->format = ((P || F) ? (ip-1)->format : "%s");
132			if (!ip->eol)
133				ip->eol = (T ? (ip-1)->eol : '\n');
134			ip++;
135			continue;
136		}
137		c = ++p;
138		switch (tolower((unsigned char)*c)) {
139		case 's':
140			if (*++p || (p = *++av))
141				ip->sepstring = p;
142			else
143				usage();
144			S = (*c == 'S' ? 1 : 0);
145			break;
146		case 't':
147			if (*++p || (p = *++av))
148				ip->eol = *p;
149			else
150				usage();
151			T = (*c == 'T' ? 1 : 0);
152			nofinalnl = 1;
153			break;
154		case 'p':
155			ip->pad = 1;
156			P = (*c == 'P' ? 1 : 0);
157			/* FALLTHROUGH */
158		case 'f':
159			F = (*c == 'F' ? 1 : 0);
160			if (*++p || (p = *++av)) {
161				fmtp += strlen(fmtp) + 1;
162				if (fmtp >= fmtbuf + sizeof(fmtbuf))
163					errx(1, "no more format space");
164				/* restrict format string to only valid width formatters */
165				if (strspn(p, "-.0123456789") != strlen(p))
166					errx(1, "invalid format string `%s'", p);
167				if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p)
168				    >= fmtbuf + sizeof(fmtbuf) - fmtp)
169					errx(1, "no more format space");
170				ip->format = fmtp;
171			}
172			else
173				usage();
174			break;
175		default:
176			usage();
177		}
178	}
179	ip->fp = NULL;
180	if (!ip->sepstring)
181		ip->sepstring = "";
182}
183
184static char *
185pad(struct openfile *ip)
186{
187	char *lp = linep;
188
189	strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
190	lp += strlen(lp);
191	if (ip->pad) {
192		snprintf(lp, line + sizeof(line) - lp, ip->format, "");
193		lp += strlen(lp);
194	}
195	return (lp);
196}
197
198static char *
199gatherline(struct openfile *ip)
200{
201	char s[BUFSIZ];
202	int c;
203	char *p;
204	char *lp = linep;
205	char *end = s + sizeof(s) - 1;
206
207	if (ip->eof)
208		return (pad(ip));
209	for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
210		if ((*p = c) == ip->eol)
211			break;
212	*p = '\0';
213	if (c == EOF) {
214		ip->eof = 1;
215		if (ip->fp == stdin)
216			fclose(stdin);
217		morefiles--;
218		return (pad(ip));
219	}
220	strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
221	lp += strlen(lp);
222	snprintf(lp, line + sizeof(line) - lp, ip->format, s);
223	lp += strlen(lp);
224	return (lp);
225}
226
227static void
228usage(void)
229{
230	fprintf(stderr, "%s\n%s\n",
231"usage: lam [ -f min.max ] [ -s sepstring ] [ -t c ] file ...",
232"       lam [ -p min.max ] [ -s sepstring ] [ -t c ] file ...");
233	exit(1);
234}
235