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