2 * Copyright (c) 1985, 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
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 * 4. 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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1985, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)timedc.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
59 char *margv[MAX_MARGV];
62 static struct cmd *getcmd(char *);
69 register struct cmd *c;
71 openlog("timedc", LOG_ODELAY, LOG_AUTH);
76 if (priv_resources() < 0)
77 errx(1, "could not get privileged resources");
78 if (setuid(getuid()) != 0)
83 if (c == (struct cmd *)-1) {
84 printf("?Ambiguous command\n");
88 printf("?Invalid command\n");
91 if (c->c_priv && getuid()) {
92 printf("?Privileged command\n");
95 (*c->c_handler)(argc, argv);
99 fromatty = isatty(fileno(stdin));
100 if (setjmp(toplevel))
102 (void) signal(SIGINT, intr);
106 (void) fflush(stdout);
108 if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
115 c = getcmd(margv[0]);
116 if (c == (struct cmd *)-1) {
117 printf("?Ambiguous command\n");
121 printf("?Invalid command\n");
124 if (c->c_priv && getuid()) {
125 printf("?Privileged command\n");
128 (*c->c_handler)(margc, margv);
139 longjmp(toplevel, 1);
147 register char *p, *q;
148 register struct cmd *c, *found;
149 register int nmatches, longest;
155 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
157 for (q = name; *q == *p++; q++)
158 if (*q == 0) /* exact match? */
160 if (!*q) { /* the name was a prefix */
161 if (q - name > longest) {
165 } else if (q - name == longest)
170 return((struct cmd *)-1);
175 * Slice a string up into argc/argv.
181 register char **argp = margv;
184 for (cp = cmdline; margc < MAX_MARGV - 1 && *cp; ) {
191 while (*cp != '\0' && !isspace(*cp))
200 #define HELPINDENT (sizeof ("directory"))
210 register struct cmd *c;
213 register int i, j, w;
214 int columns, width = 0, lines;
217 printf("Commands may be abbreviated. Commands are:\n\n");
218 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
219 int len = strlen(c->c_name);
224 width = (width + 8) &~ 7;
225 columns = 80 / width;
228 lines = (NCMDS + columns - 1) / columns;
229 for (i = 0; i < lines; i++) {
230 for (j = 0; j < columns; j++) {
231 c = cmdtab + j * lines + i;
232 printf("%s", c->c_name);
233 if (c + lines >= &cmdtab[NCMDS]) {
237 w = strlen(c->c_name);
250 if (c == (struct cmd *)-1)
251 printf("?Ambiguous help command %s\n", arg);
252 else if (c == (struct cmd *)0)
253 printf("?Invalid help command %s\n", arg);
255 printf("%-*s\t%s\n", (int)HELPINDENT,
256 c->c_name, c->c_help);