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