]>
Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ac2f15b3 A |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
b7080c8e A |
14 | * |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ac2f15b3 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
b7080c8e A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /*- | |
26 | * Copyright (c) 1985, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * 4. Neither the name of the University nor the names of its contributors | |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | */ | |
57 | ||
58 | #ifndef lint | |
59 | static char copyright[] = | |
60 | "@(#) Copyright (c) 1985, 1993\n\ | |
61 | The Regents of the University of California. All rights reserved.\n"; | |
62 | #endif /* not lint */ | |
63 | ||
64 | #ifndef lint | |
65 | static char sccsid[] = "@(#)timedc.c 8.1 (Berkeley) 6/6/93"; | |
66 | #endif /* not lint */ | |
67 | ||
68 | #ifdef sgi | |
69 | #ident "$Revision: 1.1.1.1 $" | |
70 | #endif | |
71 | ||
72 | #include "timedc.h" | |
73 | #include <strings.h> | |
74 | #include <signal.h> | |
75 | #include <ctype.h> | |
76 | #include <setjmp.h> | |
77 | #include <unistd.h> | |
78 | #include <stdlib.h> | |
79 | #include <syslog.h> | |
80 | ||
81 | int trace = 0; | |
82 | FILE *fd = 0; | |
83 | int margc; | |
84 | int fromatty; | |
85 | char *margv[20]; | |
86 | char cmdline[200]; | |
87 | jmp_buf toplevel; | |
88 | static struct cmd *getcmd __P((char *)); | |
89 | ||
90 | int | |
91 | main(argc, argv) | |
92 | int argc; | |
93 | char *argv[]; | |
94 | { | |
95 | register struct cmd *c; | |
96 | ||
97 | openlog("timedc", LOG_ODELAY, LOG_AUTH); | |
98 | ||
99 | /* | |
100 | * security dictates! | |
101 | */ | |
102 | if (priv_resources() < 0) { | |
103 | fprintf(stderr, "Could not get privileged resources\n"); | |
104 | exit(1); | |
105 | } | |
106 | (void) setuid(getuid()); | |
107 | ||
108 | if (--argc > 0) { | |
109 | c = getcmd(*++argv); | |
110 | if (c == (struct cmd *)-1) { | |
111 | printf("?Ambiguous command\n"); | |
112 | exit(1); | |
113 | } | |
114 | if (c == 0) { | |
115 | printf("?Invalid command\n"); | |
116 | exit(1); | |
117 | } | |
118 | if (c->c_priv && getuid()) { | |
119 | printf("?Privileged command\n"); | |
120 | exit(1); | |
121 | } | |
122 | (*c->c_handler)(argc, argv); | |
123 | exit(0); | |
124 | } | |
125 | ||
126 | fromatty = isatty(fileno(stdin)); | |
127 | if (setjmp(toplevel)) | |
128 | putchar('\n'); | |
129 | (void) signal(SIGINT, intr); | |
130 | for (;;) { | |
131 | if (fromatty) { | |
132 | printf("timedc> "); | |
133 | (void) fflush(stdout); | |
134 | } | |
135 | if (fgets(cmdline, sizeof(cmdline), stdin) == 0) | |
136 | quit(); | |
137 | if (cmdline[0] == 0) | |
138 | break; | |
139 | makeargv(); | |
140 | if (margv[0] == 0) | |
141 | continue; | |
142 | c = getcmd(margv[0]); | |
143 | if (c == (struct cmd *)-1) { | |
144 | printf("?Ambiguous command\n"); | |
145 | continue; | |
146 | } | |
147 | if (c == 0) { | |
148 | printf("?Invalid command\n"); | |
149 | continue; | |
150 | } | |
151 | if (c->c_priv && getuid()) { | |
152 | printf("?Privileged command\n"); | |
153 | continue; | |
154 | } | |
155 | (*c->c_handler)(margc, margv); | |
156 | } | |
157 | return 0; | |
158 | } | |
159 | ||
160 | void | |
161 | intr(signo) | |
162 | int signo; | |
163 | { | |
164 | if (!fromatty) | |
165 | exit(0); | |
166 | longjmp(toplevel, 1); | |
167 | } | |
168 | ||
169 | ||
170 | static struct cmd * | |
171 | getcmd(name) | |
172 | char *name; | |
173 | { | |
174 | register char *p, *q; | |
175 | register struct cmd *c, *found; | |
176 | register int nmatches, longest; | |
177 | extern int NCMDS; | |
178 | ||
179 | longest = 0; | |
180 | nmatches = 0; | |
181 | found = 0; | |
182 | for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { | |
183 | p = c->c_name; | |
184 | for (q = name; *q == *p++; q++) | |
185 | if (*q == 0) /* exact match? */ | |
186 | return(c); | |
187 | if (!*q) { /* the name was a prefix */ | |
188 | if (q - name > longest) { | |
189 | longest = q - name; | |
190 | nmatches = 1; | |
191 | found = c; | |
192 | } else if (q - name == longest) | |
193 | nmatches++; | |
194 | } | |
195 | } | |
196 | if (nmatches > 1) | |
197 | return((struct cmd *)-1); | |
198 | return(found); | |
199 | } | |
200 | ||
201 | /* | |
202 | * Slice a string up into argc/argv. | |
203 | */ | |
204 | void | |
205 | makeargv() | |
206 | { | |
207 | register char *cp; | |
208 | register char **argp = margv; | |
209 | ||
210 | margc = 0; | |
211 | for (cp = cmdline; *cp;) { | |
212 | while (isspace(*cp)) | |
213 | cp++; | |
214 | if (*cp == '\0') | |
215 | break; | |
216 | *argp++ = cp; | |
217 | margc += 1; | |
218 | while (*cp != '\0' && !isspace(*cp)) | |
219 | cp++; | |
220 | if (*cp == '\0') | |
221 | break; | |
222 | *cp++ = '\0'; | |
223 | } | |
224 | *argp++ = 0; | |
225 | } | |
226 | ||
227 | #define HELPINDENT (sizeof ("directory")) | |
228 | ||
229 | /* | |
230 | * Help command. | |
231 | */ | |
232 | void | |
233 | help(argc, argv) | |
234 | int argc; | |
235 | char *argv[]; | |
236 | { | |
237 | register struct cmd *c; | |
238 | ||
239 | if (argc == 1) { | |
240 | register int i, j, w; | |
241 | int columns, width = 0, lines; | |
242 | extern int NCMDS; | |
243 | ||
244 | printf("Commands may be abbreviated. Commands are:\n\n"); | |
245 | for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { | |
246 | int len = strlen(c->c_name); | |
247 | ||
248 | if (len > width) | |
249 | width = len; | |
250 | } | |
251 | width = (width + 8) &~ 7; | |
252 | columns = 80 / width; | |
253 | if (columns == 0) | |
254 | columns = 1; | |
255 | lines = (NCMDS + columns - 1) / columns; | |
256 | for (i = 0; i < lines; i++) { | |
257 | for (j = 0; j < columns; j++) { | |
258 | c = cmdtab + j * lines + i; | |
259 | printf("%s", c->c_name); | |
260 | if (c + lines >= &cmdtab[NCMDS]) { | |
261 | printf("\n"); | |
262 | break; | |
263 | } | |
264 | w = strlen(c->c_name); | |
265 | while (w < width) { | |
266 | w = (w + 8) &~ 7; | |
267 | putchar('\t'); | |
268 | } | |
269 | } | |
270 | } | |
271 | return; | |
272 | } | |
273 | while (--argc > 0) { | |
274 | register char *arg; | |
275 | arg = *++argv; | |
276 | c = getcmd(arg); | |
277 | if (c == (struct cmd *)-1) | |
278 | printf("?Ambiguous help command %s\n", arg); | |
279 | else if (c == (struct cmd *)0) | |
280 | printf("?Invalid help command %s\n", arg); | |
281 | else | |
282 | printf("%-*s\t%s\n", (int)HELPINDENT, | |
283 | c->c_name, c->c_help); | |
284 | } | |
285 | } |