1 /* $NetBSD: timedc.c,v 1.20 2008/02/16 07:31:13 matt Exp $ */
4 * Copyright (c) 1985, 1993 The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1985, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)timedc.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: timedc.c,v 1.20 2008/02/16 07:31:13 matt Exp $");
63 char *margv
[MAX_MARGV
];
66 static const struct cmd
*getcmd(char *);
67 static int drop_privileges(void);
70 main(int argc
, char *argv
[])
75 openlog("timedc", 0, LOG_AUTH
);
80 if (priv_resources() < 0)
81 errx(EXIT_FAILURE
, "Could not get privileged resources");
82 if (drop_privileges() < 0)
83 errx(EXIT_FAILURE
, "Could not drop privileges");
87 if (c
== (struct cmd
*)-1) {
88 printf("?Ambiguous command\n");
92 printf("?Invalid 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)
113 printf("?Too many arguments\n");
118 c
= getcmd(margv
[0]);
119 if (c
== (struct cmd
*)-1) {
120 printf("?Ambiguous command\n");
124 printf("?Invalid command\n");
127 (*c
->c_handler
)(margc
, margv
);
138 longjmp(toplevel
, 1);
142 static const struct cmd
*
147 const struct cmd
*c
, *found
;
148 int nmatches
, longest
;
149 extern const struct cmd cmdtab
[];
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.
184 for (cp
= cmdline
; argp
< &margv
[MAX_MARGV
- 1] && *cp
;) {
185 while (isspace((unsigned char)*cp
))
191 while (*cp
!= '\0' && !isspace((unsigned char)*cp
))
197 if (margc
== MAX_MARGV
- 1)
203 #define HELPINDENT (sizeof ("directory"))
209 help(int argc
, char *argv
[])
212 extern const struct cmd cmdtab
[];
216 int columns
, width
= 0, lines
;
219 printf("Commands may be abbreviated. Commands are:\n\n");
220 for (c
= cmdtab
; c
< &cmdtab
[NCMDS
]; c
++) {
221 int len
= strlen(c
->c_name
);
226 width
= (width
+ 8) &~ 7;
227 columns
= 80 / width
;
230 lines
= (NCMDS
+ columns
- 1) / columns
;
231 for (i
= 0; i
< lines
; i
++) {
232 for (j
= 0; j
< columns
; j
++) {
233 c
= cmdtab
+ j
* lines
+ i
;
234 printf("%s", c
->c_name
);
235 if (c
+ lines
>= &cmdtab
[NCMDS
]) {
239 w
= strlen(c
->c_name
);
252 if (c
== (struct cmd
*)-1)
253 printf("?Ambiguous help command %s\n", arg
);
254 else if (c
== (struct cmd
*)0)
255 printf("?Invalid help command %s\n", arg
);
257 printf("%-*s\t%s\n", (int)HELPINDENT
,
258 c
->c_name
, c
->c_help
);
263 drop_privileges(void)
265 static const char user
[] = "_timedc";
266 const struct passwd
*pw
;
270 if ((pw
= getpwnam(user
)) == NULL
) {
271 warnx("getpwnam(\"%s\") failed", user
);
276 if (setgroups(1, &gid
)) {