Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[dragonfly.git] / usr.sbin / timed / timedc / timedc.c
1 /*-
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1985, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)timedc.c 8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.sbin/timed/timedc/timedc.c,v 1.5 2003/10/11 07:35:35 tjr Exp $
36  */
37
38 #include "timedc.h"
39 #include <ctype.h>
40 #include <err.h>
41 #include <setjmp.h>
42 #include <signal.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <syslog.h>
46 #include <unistd.h>
47
48 int trace = 0;
49 FILE *fd = NULL;
50 int     margc;
51 int     fromatty;
52 #define MAX_MARGV       20
53 char    *margv[MAX_MARGV];
54 char    cmdline[200];
55 jmp_buf toplevel;
56 static struct cmd *getcmd(char *);
57
58 int
59 main(int argc, char **argv)
60 {
61         struct cmd *c;
62
63         openlog("timedc", LOG_ODELAY, LOG_AUTH);
64
65         /*
66          * security dictates!
67          */
68         if (priv_resources() < 0)
69                 errx(1, "could not get privileged resources");
70         setuid(getuid());
71
72         if (--argc > 0) {
73                 c = getcmd(*++argv);
74                 if (c == (struct cmd *)-1) {
75                         printf("?Ambiguous command\n");
76                         exit(1);
77                 }
78                 if (c == NULL) {
79                         printf("?Invalid command\n");
80                         exit(1);
81                 }
82                 if (c->c_priv && getuid()) {
83                         printf("?Privileged command\n");
84                         exit(1);
85                 }
86                 (*c->c_handler)(argc, argv);
87                 exit(0);
88         }
89
90         fromatty = isatty(fileno(stdin));
91         if (setjmp(toplevel))
92                 putchar('\n');
93         signal(SIGINT, intr);
94         for (;;) {
95                 if (fromatty) {
96                         printf("timedc> ");
97                         fflush(stdout);
98                 }
99                 if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
100                         quit();
101                 if (cmdline[0] == 0)
102                         break;
103                 makeargv();
104                 if (margv[0] == NULL)
105                         continue;
106                 c = getcmd(margv[0]);
107                 if (c == (struct cmd *)-1) {
108                         printf("?Ambiguous command\n");
109                         continue;
110                 }
111                 if (c == NULL) {
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         return 0;
122 }
123
124 void
125 intr(int signo)
126 {
127         if (!fromatty)
128                 exit(0);
129         longjmp(toplevel, 1);
130 }
131
132 static struct cmd *
133 getcmd(char *name)
134 {
135         char *p, *q;
136         struct cmd *c, *found;
137         int nmatches, longest;
138         extern int NCMDS;
139
140         longest = 0;
141         nmatches = 0;
142         found = NULL;
143         for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
144                 p = c->c_name;
145                 for (q = name; *q == *p++; q++)
146                         if (*q == 0)            /* exact match? */
147                                 return(c);
148                 if (!*q) {                      /* the name was a prefix */
149                         if (q - name > longest) {
150                                 longest = q - name;
151                                 nmatches = 1;
152                                 found = c;
153                         } else if (q - name == longest)
154                                 nmatches++;
155                 }
156         }
157         if (nmatches > 1)
158                 return((struct cmd *)-1);
159         return(found);
160 }
161
162 /*
163  * Slice a string up into argc/argv.
164  */
165 void
166 makeargv(void)
167 {
168         char *cp;
169         char **argp = margv;
170
171         margc = 0;
172         for (cp = cmdline; margc < MAX_MARGV - 1 && *cp;) {
173                 while (isspace(*cp))
174                         cp++;
175                 if (*cp == '\0')
176                         break;
177                 *argp++ = cp;
178                 margc += 1;
179                 while (*cp != '\0' && !isspace(*cp))
180                         cp++;
181                 if (*cp == '\0')
182                         break;
183                 *cp++ = '\0';
184         }
185         *argp++ = NULL;
186 }
187
188 #define HELPINDENT (sizeof("directory"))
189
190 /*
191  * Help command.
192  */
193 void
194 help(int argc, char **argv)
195 {
196         struct cmd *c;
197
198         if (argc == 1) {
199                 int i, j, w;
200                 int columns, width = 0, lines;
201                 extern int NCMDS;
202
203                 printf("Commands may be abbreviated.  Commands are:\n\n");
204                 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
205                         int len = strlen(c->c_name);
206
207                         if (len > width)
208                                 width = len;
209                 }
210                 width = (width + 8) &~ 7;
211                 columns = 80 / width;
212                 if (columns == 0)
213                         columns = 1;
214                 lines = (NCMDS + columns - 1) / columns;
215                 for (i = 0; i < lines; i++) {
216                         for (j = 0; j < columns; j++) {
217                                 c = cmdtab + j * lines + i;
218                                 printf("%s", c->c_name);
219                                 if (c + lines >= &cmdtab[NCMDS]) {
220                                         printf("\n");
221                                         break;
222                                 }
223                                 w = strlen(c->c_name);
224                                 while (w < width) {
225                                         w = (w + 8) &~ 7;
226                                         putchar('\t');
227                                 }
228                         }
229                 }
230                 return;
231         }
232         while (--argc > 0) {
233                 char *arg;
234                 arg = *++argv;
235                 c = getcmd(arg);
236                 if (c == (struct cmd *)-1)
237                         printf("?Ambiguous help command %s\n", arg);
238                 else if (c == NULL)
239                         printf("?Invalid help command %s\n", arg);
240                 else
241                         printf("%-*s\t%s\n", (int)HELPINDENT,
242                                 c->c_name, c->c_help);
243         }
244 }