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