]> git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/lam/lam.c
download.sh: Properly update
[apple_cmds.git] / text_cmds / lam / lam.c
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)lam.c 8.1 (Berkeley) 6/6/93";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD: src/usr.bin/lam/lam.c,v 1.14 2005/08/05 01:04:36 jmallett Exp $");
47
48 /*
49 * lam - laminate files
50 * Author: John Kunze, UCB
51 */
52
53 #include <ctype.h>
54 #include <err.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sysexits.h>
59
60 #define MAXOFILES 20
61 #define BIGBUFSIZ 5 * BUFSIZ
62
63 struct openfile { /* open file structure */
64 FILE *fp; /* file pointer */
65 short eof; /* eof flag */
66 short pad; /* pad flag for missing columns */
67 char eol; /* end of line character */
68 const char *sepstring; /* string to print before each line */
69 const char *format; /* printf(3) style string spec. */
70 } input[MAXOFILES];
71
72 int morefiles; /* set by getargs(), changed by gatherline() */
73 int nofinalnl; /* normally append \n to each output line */
74 char line[BIGBUFSIZ];
75 char *linep;
76
77 static char *gatherline(struct openfile *);
78 static void getargs(char *[]);
79 static char *pad(struct openfile *);
80 static void usage(void);
81
82 int
83 main(int argc, char *argv[])
84 {
85 struct openfile *ip;
86
87 if (argc == 1)
88 usage();
89 getargs(argv);
90 if (!morefiles)
91 usage();
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
114 P = S = F = T = 0; /* capitalized options */
115 while ((p = *++av) != NULL) {
116 if (*p != '-' || !p[1]) {
117 if (++morefiles >= MAXOFILES)
118 errx(1, "too many input files");
119 if (*p == '-')
120 ip->fp = stdin;
121 else if ((ip->fp = fopen(p, "r")) == NULL) {
122 err(1, "%s", p);
123 }
124 ip->pad = P;
125 if (!ip->sepstring)
126 ip->sepstring = (S ? (ip-1)->sepstring : "");
127 if (!ip->format)
128 ip->format = ((P || F) ? (ip-1)->format : "%s");
129 if (!ip->eol)
130 ip->eol = (T ? (ip-1)->eol : '\n');
131 ip++;
132 continue;
133 }
134 c = ++p;
135 switch (tolower((unsigned char)*c)) {
136 case 's':
137 if (*++p || (p = *++av))
138 ip->sepstring = p;
139 else
140 usage();
141 S = (*c == 'S' ? 1 : 0);
142 break;
143 case 't':
144 if (*++p || (p = *++av))
145 ip->eol = *p;
146 else
147 usage();
148 T = (*c == 'T' ? 1 : 0);
149 nofinalnl = 1;
150 break;
151 case 'p':
152 ip->pad = 1;
153 P = (*c == 'P' ? 1 : 0);
154 /* FALLTHROUGH */
155 case 'f':
156 F = (*c == 'F' ? 1 : 0);
157 if (*++p || (p = *++av)) {
158 fmtp += strlen(fmtp) + 1;
159 if (fmtp >= fmtbuf + sizeof(fmtbuf))
160 errx(1, "no more format space");
161 /* restrict format string to only valid width formatters */
162 if (strspn(p, "-.0123456789") != strlen(p))
163 errx(1, "invalid format string `%s'", p);
164 if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p)
165 >= fmtbuf + sizeof(fmtbuf) - fmtp)
166 errx(1, "no more format space");
167 ip->format = fmtp;
168 }
169 else
170 usage();
171 break;
172 default:
173 usage();
174 }
175 }
176 ip->fp = NULL;
177 if (!ip->sepstring)
178 ip->sepstring = "";
179 }
180
181 static char *
182 pad(struct openfile *ip)
183 {
184 char *lp = linep;
185
186 strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
187 lp += strlen(lp);
188 if (ip->pad) {
189 snprintf(lp, line + sizeof(line) - lp, ip->format, "");
190 lp += strlen(lp);
191 }
192 return (lp);
193 }
194
195 static char *
196 gatherline(struct openfile *ip)
197 {
198 char s[BUFSIZ];
199 int c;
200 char *p;
201 char *lp = linep;
202 char *end = s + sizeof(s) - 1;
203
204 if (ip->eof)
205 return (pad(ip));
206 for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
207 if ((*p = c) == ip->eol)
208 break;
209 *p = '\0';
210 if (c == EOF) {
211 ip->eof = 1;
212 if (ferror(ip->fp)) {
213 err(EX_IOERR, NULL);
214 }
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
227 static void
228 usage()
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 }