usched: Allow process to change self cpu affinity
[dragonfly.git] / usr.bin / lam / lam.c
blobf1e68c03c397fc8a601d9b5fe88f6eda96bdce99
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
30 * @(#)lam.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/lam/lam.c,v 1.3.2.3 2001/08/11 03:03:19 mike Exp $
32 * $DragonFly: src/usr.bin/lam/lam.c,v 1.3 2003/10/04 20:36:47 hmp Exp $
36 * lam - laminate files
37 * Author: John Kunze, UCB
40 #include <ctype.h>
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #define MAXOFILES 20
47 #define BIGBUFSIZ 5 * BUFSIZ
49 struct openfile { /* open file structure */
50 FILE *fp; /* file pointer */
51 short eof; /* eof flag */
52 short pad; /* pad flag for missing columns */
53 char eol; /* end of line character */
54 const char *sepstring; /* string to print before each line */
55 const char *format; /* printf(3) style string spec. */
56 } input[MAXOFILES];
58 int morefiles; /* set by getargs(), changed by gatherline() */
59 int nofinalnl; /* normally append \n to each output line */
60 char line[BIGBUFSIZ];
61 char *linep;
63 static char *gatherline(struct openfile *);
64 static void getargs(char *[]);
65 static char *pad(struct openfile *);
66 static void usage(void);
68 int
69 main(int argc __unused, char *argv[])
71 struct openfile *ip;
73 getargs(argv);
74 if (!morefiles)
75 usage();
76 for (;;) {
77 linep = line;
78 for (ip = input; ip->fp != NULL; ip++)
79 linep = gatherline(ip);
80 if (!morefiles)
81 exit(0);
82 fputs(line, stdout);
83 fputs(ip->sepstring, stdout);
84 if (!nofinalnl)
85 putchar('\n');
89 static void
90 getargs(char *av[])
92 struct openfile *ip = input;
93 char *p, *c;
94 static char fmtbuf[BUFSIZ];
95 char *fmtp = fmtbuf;
96 int P, S, F, T;
98 P = S = F = T = 0; /* capitalized options */
99 while ((p = *++av) != NULL) {
100 if (*p != '-' || !p[1]) {
101 if (++morefiles >= MAXOFILES)
102 errx(1, "too many input files");
103 if (*p == '-')
104 ip->fp = stdin;
105 else if ((ip->fp = fopen(p, "r")) == NULL) {
106 err(1, "%s", p);
108 ip->pad = P;
109 if (!ip->sepstring)
110 ip->sepstring = (S ? (ip-1)->sepstring : "");
111 if (!ip->format)
112 ip->format = ((P || F) ? (ip-1)->format : "%s");
113 if (!ip->eol)
114 ip->eol = (T ? (ip-1)->eol : '\n');
115 ip++;
116 continue;
118 c = ++p;
119 switch (tolower(*c)) {
120 case 's':
121 if (*++p || (p = *++av))
122 ip->sepstring = p;
123 else
124 errx(1, "need string after -%s", c);
125 S = (*c == 'S' ? 1 : 0);
126 break;
127 case 't':
128 if (*++p || (p = *++av))
129 ip->eol = *p;
130 else
131 errx(1, "need character after -%s", c);
132 T = (*c == 'T' ? 1 : 0);
133 nofinalnl = 1;
134 break;
135 case 'p':
136 ip->pad = 1;
137 P = (*c == 'P' ? 1 : 0);
138 /* FALLTHROUGH */
139 case 'f':
140 F = (*c == 'F' ? 1 : 0);
141 if (*++p || (p = *++av)) {
142 fmtp += strlen(fmtp) + 1;
143 if (fmtp >= fmtbuf + sizeof(fmtbuf))
144 errx(1, "no more format space");
145 /* restrict format string to only valid width formatters */
146 if (strspn(p, "-.0123456789") != strlen(p))
147 errx(1, "invalid format string `%s'", p);
148 if (snprintf(fmtp, fmtbuf + sizeof(fmtbuf) - fmtp, "%%%ss", p)
149 >= fmtbuf + sizeof(fmtbuf) - fmtp)
150 errx(1, "no more format space");
151 ip->format = fmtp;
153 else
154 errx(1, "need string after -%s", c);
155 break;
156 default:
157 errx(1, "what do you mean by -%s?", c);
158 break;
161 ip->fp = NULL;
162 if (!ip->sepstring)
163 ip->sepstring = "";
166 static char *
167 pad(struct openfile *ip)
169 char *lp = linep;
171 strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
172 lp += strlen(lp);
173 if (ip->pad) {
174 snprintf(lp, line + sizeof(line) - lp, ip->format, "");
175 lp += strlen(lp);
177 return (lp);
180 static char *
181 gatherline(struct openfile *ip)
183 char s[BUFSIZ];
184 int c;
185 char *p;
186 char *lp = linep;
187 char *end = s + sizeof(s) - 1;
189 if (ip->eof)
190 return (pad(ip));
191 for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++)
192 if ((*p = c) == ip->eol)
193 break;
194 *p = '\0';
195 if (c == EOF) {
196 ip->eof = 1;
197 if (ip->fp == stdin)
198 fclose(stdin);
199 morefiles--;
200 return (pad(ip));
202 strlcpy(lp, ip->sepstring, line + sizeof(line) - lp);
203 lp += strlen(lp);
204 snprintf(lp, line + sizeof(line) - lp, ip->format, s);
205 lp += strlen(lp);
206 return (lp);
209 static void
210 usage(void)
212 fprintf(stderr, "%s\n%s\n",
213 "usage: lam [ -f min.max ] [ -s sepstring ] [ -t c ] file ...",
214 " lam [ -p min.max ] [ -s sepstring ] [ -t c ] file ...");
215 exit(1);