1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved. The Berkeley software License Agreement
4: * specifies the terms and conditions for redistribution.
5: */
6:
7: #ifndef lint
8: char copyright[] =
9: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10: All rights reserved.\n";
11: #endif not lint
12:
13: #ifndef lint
14: static char sccsid[] = "@(#)timedc.c 2.3 (Berkeley) 6/5/86";
15: #endif not lint
16:
17: #include "timedc.h"
18: #include <signal.h>
19: #include <ctype.h>
20: #include <setjmp.h>
21: #include <syslog.h>
22:
23: int top;
24: int margc;
25: int fromatty;
26: char *margv[20];
27: char cmdline[200];
28: jmp_buf toplevel;
29: int intr();
30: int priv_resources();
31: struct cmd *getcmd();
32:
33:
34: main(argc, argv)
35: char *argv[];
36: {
37: register struct cmd *c;
38:
39: openlog("timedc", LOG_ODELAY, LOG_AUTH);
40:
41: /*
42: * security dictates!
43: */
44: if (priv_resources() < 0) {
45: fprintf(stderr, "Could not get priviledged resources\n");
46: exit(1);
47: }
48: (void) setuid(getuid());
49:
50: if (--argc > 0) {
51: c = getcmd(*++argv);
52: if (c == (struct cmd *)-1) {
53: printf("?Ambiguous command\n");
54: exit(1);
55: }
56: if (c == 0) {
57: printf("?Invalid command\n");
58: exit(1);
59: }
60: if (c->c_priv && getuid()) {
61: printf("?Privileged command\n");
62: exit(1);
63: }
64: (*c->c_handler)(argc, argv);
65: exit(0);
66: }
67: fromatty = isatty(fileno(stdin));
68: top = setjmp(toplevel) == 0;
69: if (top)
70: (void) signal(SIGINT, intr);
71: for (;;) {
72: cmdscanner(top);
73: top = 1;
74: }
75: }
76:
77: intr()
78: {
79: if (!fromatty)
80: exit(0);
81: longjmp(toplevel, 1);
82: }
83:
84: /*
85: * Command parser.
86: */
87: cmdscanner(top)
88: int top;
89: {
90: register struct cmd *c;
91: extern struct cmd cmdtab[];
92: extern int help();
93:
94: if (!top)
95: putchar('\n');
96: for (;;) {
97: if (fromatty) {
98: printf("timedc> ");
99: (void) fflush(stdout);
100: }
101: if (gets(cmdline) == 0)
102: quit();
103: if (cmdline[0] == 0)
104: break;
105: makeargv();
106: c = getcmd(margv[0]);
107: if (c == (struct cmd *)-1) {
108: printf("?Ambiguous command\n");
109: continue;
110: }
111: if (c == 0) {
112: printf("?Invalid command\n");
113: continue;
114: }
115: if (c->c_priv && getuid()) {
116: printf("?Privileged command\n");
117: continue;
118: }
119: (*c->c_handler)(margc, margv);
120: }
121: longjmp(toplevel, 0);
122: }
123:
124: struct cmd *
125: getcmd(name)
126: register char *name;
127: {
128: register char *p, *q;
129: register struct cmd *c, *found;
130: register int nmatches, longest;
131: extern int NCMDS;
132:
133: longest = 0;
134: nmatches = 0;
135: found = 0;
136: for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
137: p = c->c_name;
138: for (q = name; *q == *p++; q++)
139: if (*q == 0) /* exact match? */
140: return(c);
141: if (!*q) { /* the name was a prefix */
142: if (q - name > longest) {
143: longest = q - name;
144: nmatches = 1;
145: found = c;
146: } else if (q - name == longest)
147: nmatches++;
148: }
149: }
150: if (nmatches > 1)
151: return((struct cmd *)-1);
152: return(found);
153: }
154:
155: /*
156: * Slice a string up into argc/argv.
157: */
158: makeargv()
159: {
160: register char *cp;
161: register char **argp = margv;
162:
163: margc = 0;
164: for (cp = cmdline; *cp;) {
165: while (isspace(*cp))
166: cp++;
167: if (*cp == '\0')
168: break;
169: *argp++ = cp;
170: margc += 1;
171: while (*cp != '\0' && !isspace(*cp))
172: cp++;
173: if (*cp == '\0')
174: break;
175: *cp++ = '\0';
176: }
177: *argp++ = 0;
178: }
179:
180: #define HELPINDENT (sizeof ("directory"))
181:
182: /*
183: * Help command.
184: */
185: help(argc, argv)
186: int argc;
187: char *argv[];
188: {
189: register struct cmd *c;
190:
191: if (argc == 1) {
192: register int i, j, w;
193: int columns, width = 0, lines;
194: extern int NCMDS;
195:
196: printf("Commands may be abbreviated. Commands are:\n\n");
197: for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
198: int len = strlen(c->c_name);
199:
200: if (len > width)
201: width = len;
202: }
203: width = (width + 8) &~ 7;
204: columns = 80 / width;
205: if (columns == 0)
206: columns = 1;
207: lines = (NCMDS + columns - 1) / columns;
208: for (i = 0; i < lines; i++) {
209: for (j = 0; j < columns; j++) {
210: c = cmdtab + j * lines + i;
211: printf("%s", c->c_name);
212: if (c + lines >= &cmdtab[NCMDS]) {
213: printf("\n");
214: break;
215: }
216: w = strlen(c->c_name);
217: while (w < width) {
218: w = (w + 8) &~ 7;
219: putchar('\t');
220: }
221: }
222: }
223: return;
224: }
225: while (--argc > 0) {
226: register char *arg;
227: arg = *++argv;
228: c = getcmd(arg);
229: if (c == (struct cmd *)-1)
230: printf("?Ambiguous help command %s\n", arg);
231: else if (c == (struct cmd *)0)
232: printf("?Invalid help command %s\n", arg);
233: else
234: printf("%-*s\t%s\n", HELPINDENT,
235: c->c_name, c->c_help);
236: }
237: }
Defined functions
help
defined in line
185; used 4 times
intr
defined in line
77; used 2 times
main
defined in line
34;
never used
Defined variables
margc
defined in line
24; used 3 times
margv
defined in line
26; used 3 times
top
defined in line
23; used 7 times
Defined macros