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 *);
65 main(int argc, char *argv[])
67 register struct cmd *c;
69 openlog("timedc", LOG_ODELAY, LOG_AUTH);
74 if (priv_resources() < 0)
75 errx(1, "could not get privileged resources");
76 if (setuid(getuid()) != 0)
81 if (c == (struct cmd *)-1) {
82 printf("?Ambiguous command\n");
86 printf("?Invalid command\n");
89 if (c->c_priv && getuid()) {
90 printf("?Privileged command\n");
93 (*c->c_handler)(argc, argv);
97 fromatty = isatty(fileno(stdin));
100 (void) signal(SIGINT, intr);
104 (void) fflush(stdout);
106 if (fgets(cmdline, sizeof(cmdline), stdin) == NULL)
113 c = getcmd(margv[0]);
114 if (c == (struct cmd *)-1) {
115 printf("?Ambiguous command\n");
119 printf("?Invalid command\n");
122 if (c->c_priv && getuid()) {
123 printf("?Privileged command\n");
126 (*c->c_handler)(margc, margv);
132 intr(int signo __unused)
136 longjmp(toplevel, 1);
143 register char *p, *q;
144 register struct cmd *c, *found;
145 register int nmatches, longest;
151 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
153 for (q = name; *q == *p++; q++)
154 if (*q == 0) /* exact match? */
156 if (!*q) { /* the name was a prefix */
157 if (q - name > longest) {
161 } else if (q - name == longest)
166 return((struct cmd *)-1);
171 * Slice a string up into argc/argv.
177 register char **argp = margv;
180 for (cp = cmdline; margc < MAX_MARGV - 1 && *cp; ) {
187 while (*cp != '\0' && !isspace(*cp))
196 #define HELPINDENT (sizeof ("directory"))
202 help(int argc, char *argv[])
204 register struct cmd *c;
207 register int i, j, w;
208 int columns, width = 0, lines;
211 printf("Commands may be abbreviated. Commands are:\n\n");
212 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
213 int len = strlen(c->c_name);
218 width = (width + 8) &~ 7;
219 columns = 80 / width;
222 lines = (NCMDS + columns - 1) / columns;
223 for (i = 0; i < lines; i++) {
224 for (j = 0; j < columns; j++) {
225 c = cmdtab + j * lines + i;
226 printf("%s", c->c_name);
227 if (c + lines >= &cmdtab[NCMDS]) {
231 w = strlen(c->c_name);
244 if (c == (struct cmd *)-1)
245 printf("?Ambiguous help command %s\n", arg);
246 else if (c == (struct cmd *)0)
247 printf("?Invalid help command %s\n", arg);
249 printf("%-*s\t%s\n", (int)HELPINDENT,
250 c->c_name, c->c_help);