Annotation of src/usr.bin/lam/lam.c, Revision 1.13
1.13 ! ray 1: /* $OpenBSD: lam.c,v 1.12 2004/12/27 23:37:37 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.8 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
1.11 millert 34: static const char copyright[] =
1.1 deraadt 35: "@(#) Copyright (c) 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
1.11 millert 41: static const char sccsid[] = "@(#)lam.c 8.1 (Berkeley) 6/6/93";
1.1 deraadt 42: #endif
1.13 ! ray 43: static const char rcsid[] = "$OpenBSD: lam.c,v 1.12 2004/12/27 23:37:37 deraadt Exp $";
1.1 deraadt 44: #endif /* not lint */
45:
46: /*
47: * lam - laminate files
48: * Author: John Kunze, UCB
49: */
50:
1.13 ! ray 51: #include <sys/param.h>
! 52:
1.11 millert 53: #include <ctype.h>
54: #include <err.h>
1.1 deraadt 55: #include <stdio.h>
56: #include <stdlib.h>
57: #include <string.h>
58:
59: #define BIGBUFSIZ 5 * BUFSIZ
60:
61: struct openfile { /* open file structure */
62: FILE *fp; /* file pointer */
63: short eof; /* eof flag */
64: short pad; /* pad flag for missing columns */
65: char eol; /* end of line character */
66: char *sepstring; /* string to print before each line */
67: char *format; /* printf(3) style string spec. */
1.13 ! ray 68: } input[NOFILE_MAX + 1]; /* last one is for the last -s arg. */
! 69: #define INPUTSIZE sizeof(input) / sizeof(*input)
1.1 deraadt 70:
1.13 ! ray 71: int numfiles; /* number of open files */
1.1 deraadt 72: int nofinalnl; /* normally append \n to each output line */
73: char line[BIGBUFSIZ];
74: char *linep;
75:
1.11 millert 76: void usage(void);
1.4 millert 77: char *gatherline(struct openfile *);
1.11 millert 78: void getargs(int, char *[]);
1.4 millert 79: char *pad(struct openfile *);
1.1 deraadt 80:
81: int
1.9 deraadt 82: main(int argc, char *argv[])
1.1 deraadt 83: {
1.13 ! ray 84: int i;
1.1 deraadt 85:
1.13 ! ray 86: /* Process arguments, set numfiles to file argument count. */
1.11 millert 87: getargs(argc, argv);
1.13 ! ray 88: if (numfiles == 0)
1.11 millert 89: usage();
1.13 ! ray 90: /* Concatenate lines from each file, then print. */
1.1 deraadt 91: for (;;) {
92: linep = line;
1.13 ! ray 93: /*
! 94: * For each file that has a line to print, numfile is
! 95: * incremented. Thus if numfiles is 0, we are done.
! 96: */
! 97: numfiles = 0;
! 98: for (i = 0; i < INPUTSIZE - 1 && input[i].fp != NULL; i++)
! 99: linep = gatherline(&input[i]);
! 100: if (numfiles == 0)
1.1 deraadt 101: exit(0);
102: fputs(line, stdout);
1.13 ! ray 103: /* Print terminating -s argument. */
! 104: fputs(input[i].sepstring, stdout);
1.1 deraadt 105: if (!nofinalnl)
106: putchar('\n');
107: }
108: }
109:
110: void
1.11 millert 111: getargs(int argc, char *argv[])
1.1 deraadt 112: {
1.11 millert 113: struct openfile *ip = input;
1.3 mpech 114: char *p;
1.11 millert 115: int ch, P, S, F, T;
116: size_t siz;
1.1 deraadt 117:
118: P = S = F = T = 0; /* capitalized options */
1.11 millert 119: while (optind < argc) {
120: switch (ch = getopt(argc, argv, "F:f:P:p:S:s:T:t:")) {
121: case 'F': case 'f':
122: F = (ch == 'F');
123: /* Validate format string argument. */
124: for (p = optarg; *p != '\0'; p++)
125: if (!isdigit(*p) && *p != '.' && *p != '-')
126: errx(1, "%s: invalid width specified",
127: optarg);
128: /* '%' + width + 's' + '\0' */
129: siz = p - optarg + 3;
130: if ((p = realloc(ip->format, siz)) == NULL)
131: err(1, NULL);
132: snprintf(p, siz, "%%%ss", optarg);
133: ip->format = p;
134: break;
135: case 'P': case 'p':
136: P = (ch == 'P');
137: ip->pad = 1;
138: break;
139: case 'S': case 's':
140: S = (ch == 'S');
141: ip->sepstring = optarg;
142: break;
143: case 'T': case 't':
144: T = (ch == 'T');
145: if (strlen(optarg) != 1)
146: usage();
147: ip->eol = optarg[0];
148: nofinalnl = 1;
149: break;
150: case -1:
151: if (optind >= argc)
152: break; /* to support "--" */
1.13 ! ray 153: /* This is a file, not a flag. */
! 154: ++numfiles;
! 155: if (numfiles >= INPUTSIZE)
! 156: errx(1, "too many files");
1.11 millert 157: if (strcmp(argv[optind], "-") == 0)
1.1 deraadt 158: ip->fp = stdin;
1.11 millert 159: else if ((ip->fp = fopen(argv[optind], "r")) == NULL)
160: err(1, "%s", argv[optind]);
1.1 deraadt 161: ip->pad = P;
1.11 millert 162: if (ip->sepstring == NULL)
163: ip->sepstring = S ? (ip-1)->sepstring : "";
164: if (ip->format == NULL)
165: ip->format = (P || F) ? (ip-1)->format : "%s";
166: if (ip->eol == '\0')
167: ip->eol = T ? (ip-1)->eol : '\n';
1.1 deraadt 168: ip++;
1.11 millert 169: optind++;
1.1 deraadt 170: break;
171: default:
1.11 millert 172: usage();
173: /* NOTREACHED */
1.1 deraadt 174: }
175: }
176: ip->fp = NULL;
1.11 millert 177: if (ip->sepstring == NULL)
1.1 deraadt 178: ip->sepstring = "";
179: }
180:
181: char *
1.9 deraadt 182: pad(struct openfile *ip)
1.1 deraadt 183: {
1.7 millert 184: size_t n;
1.3 mpech 185: char *lp = linep;
1.1 deraadt 186:
1.7 millert 187: n = strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
188: lp += (n < line + sizeof(line) - lp) ? n : strlen(lp);
1.1 deraadt 189: if (ip->pad) {
1.7 millert 190: n = snprintf(lp, line + sizeof(line) - lp, ip->format, "");
1.12 deraadt 191: if (n > 0)
192: lp += (n < line + sizeof(line) - lp) ? n : strlen(lp);
1.1 deraadt 193: }
194: return (lp);
195: }
196:
1.13 ! ray 197: /*
! 198: * Grab line from file, appending to linep. Increments numfiles if file
! 199: * is still open.
! 200: */
1.1 deraadt 201: char *
1.9 deraadt 202: gatherline(struct openfile *ip)
1.1 deraadt 203: {
1.7 millert 204: size_t n;
1.1 deraadt 205: char s[BUFSIZ];
1.3 mpech 206: char *p;
207: char *lp = linep;
1.11 millert 208: char *end = s + BUFSIZ - 1;
1.7 millert 209: int c;
1.1 deraadt 210:
211: if (ip->eof)
212: return (pad(ip));
213: for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
214: if ((*p = c) == ip->eol)
215: break;
216: *p = '\0';
217: if (c == EOF) {
218: ip->eof = 1;
219: if (ip->fp == stdin)
220: fclose(stdin);
221: return (pad(ip));
222: }
1.13 ! ray 223: /* Something will be printed. */
! 224: numfiles++;
1.7 millert 225: n = strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
226: lp += (n < line + sizeof(line) - lp) ? n : strlen(lp);
1.11 millert 227: n = snprintf(lp, line + sizeof(line) - lp, ip->format, s);
1.12 deraadt 228: if (n > 0)
229: lp += (n < line + sizeof(line) - lp) ? n : strlen(lp);
1.1 deraadt 230: return (lp);
231: }
232:
233: void
1.11 millert 234: usage(void)
1.1 deraadt 235: {
1.10 mickey 236: extern char *__progname;
1.11 millert 237:
1.1 deraadt 238: fprintf(stderr,
1.11 millert 239: "usage: %s [-f min.max] [-p min.max] [-s sepstring] [-t c] file ...\n",
1.10 mickey 240: __progname);
1.1 deraadt 241: exit(1);
242: }