Home | History | Annotate | Line # | Download | only in timedc
timedc.c revision 1.3
      1 /*-
      2  * Copyright (c) 1985, 1993 The Regents of the University of California.
      3  * 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 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1985, 1993 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 static char sccsid[] = "@(#)timedc.c	5.1 (Berkeley) 5/11/93";
     42 #endif /* not lint */
     43 
     44 #ifdef sgi
     45 #ident "$Revision: 1.3 $"
     46 #endif
     47 
     48 #include "timedc.h"
     49 #include <strings.h>
     50 #include <signal.h>
     51 #include <ctype.h>
     52 #include <setjmp.h>
     53 #include <unistd.h>
     54 #include <stdlib.h>
     55 #include <syslog.h>
     56 
     57 int trace = 0;
     58 FILE *fd = 0;
     59 int	margc;
     60 int	fromatty;
     61 char	*margv[20];
     62 char	cmdline[200];
     63 jmp_buf	toplevel;
     64 static struct cmd *getcmd(char *);
     65 
     66 int
     67 main(int argc, char *argv[])
     68 {
     69 	register struct cmd *c;
     70 
     71 	openlog("timedc", LOG_ODELAY, LOG_AUTH);
     72 
     73 	/*
     74 	 * security dictates!
     75 	 */
     76 	if (priv_resources() < 0) {
     77 		fprintf(stderr, "Could not get privileged resources\n");
     78 		exit(1);
     79 	}
     80 	(void) setuid(getuid());
     81 
     82 	if (--argc > 0) {
     83 		c = getcmd(*++argv);
     84 		if (c == (struct cmd *)-1) {
     85 			printf("?Ambiguous command\n");
     86 			exit(1);
     87 		}
     88 		if (c == 0) {
     89 			printf("?Invalid command\n");
     90 			exit(1);
     91 		}
     92 		if (c->c_priv && getuid()) {
     93 			printf("?Privileged command\n");
     94 			exit(1);
     95 		}
     96 		(*c->c_handler)(argc, argv);
     97 		exit(0);
     98 	}
     99 
    100 	fromatty = isatty(fileno(stdin));
    101 	if (setjmp(toplevel))
    102 		putchar('\n');
    103 	(void) signal(SIGINT, intr);
    104 	for (;;) {
    105 		if (fromatty) {
    106 			printf("timedc> ");
    107 			(void) fflush(stdout);
    108 		}
    109 		if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
    110 			quit();
    111 		if (cmdline[0] == 0)
    112 			break;
    113 		makeargv();
    114 		if (margv[0] == 0)
    115 			continue;
    116 		c = getcmd(margv[0]);
    117 		if (c == (struct cmd *)-1) {
    118 			printf("?Ambiguous command\n");
    119 			continue;
    120 		}
    121 		if (c == 0) {
    122 			printf("?Invalid command\n");
    123 			continue;
    124 		}
    125 		if (c->c_priv && getuid()) {
    126 			printf("?Privileged command\n");
    127 			continue;
    128 		}
    129 		(*c->c_handler)(margc, margv);
    130 	}
    131 	return 0;
    132 }
    133 
    134 void
    135 intr(signo)
    136 	int signo;
    137 {
    138 	if (!fromatty)
    139 		exit(0);
    140 	longjmp(toplevel, 1);
    141 }
    142 
    143 
    144 static struct cmd *
    145 getcmd(char *name)
    146 {
    147 	register char *p, *q;
    148 	register struct cmd *c, *found;
    149 	register int nmatches, longest;
    150 	extern struct cmd cmdtab[];
    151 	extern int NCMDS;
    152 
    153 	longest = 0;
    154 	nmatches = 0;
    155 	found = 0;
    156 	for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
    157 		p = c->c_name;
    158 		for (q = name; *q == *p++; q++)
    159 			if (*q == 0)		/* exact match? */
    160 				return(c);
    161 		if (!*q) {			/* the name was a prefix */
    162 			if (q - name > longest) {
    163 				longest = q - name;
    164 				nmatches = 1;
    165 				found = c;
    166 			} else if (q - name == longest)
    167 				nmatches++;
    168 		}
    169 	}
    170 	if (nmatches > 1)
    171 		return((struct cmd *)-1);
    172 	return(found);
    173 }
    174 
    175 /*
    176  * Slice a string up into argc/argv.
    177  */
    178 void
    179 makeargv()
    180 {
    181 	register char *cp;
    182 	register char **argp = margv;
    183 
    184 	margc = 0;
    185 	for (cp = cmdline; *cp;) {
    186 		while (isspace(*cp))
    187 			cp++;
    188 		if (*cp == '\0')
    189 			break;
    190 		*argp++ = cp;
    191 		margc += 1;
    192 		while (*cp != '\0' && !isspace(*cp))
    193 			cp++;
    194 		if (*cp == '\0')
    195 			break;
    196 		*cp++ = '\0';
    197 	}
    198 	*argp++ = 0;
    199 }
    200 
    201 #define HELPINDENT (sizeof ("directory"))
    202 
    203 /*
    204  * Help command.
    205  */
    206 void
    207 help(argc, argv)
    208 	int argc;
    209 	char *argv[];
    210 {
    211 	register struct cmd *c;
    212 	extern struct cmd cmdtab[];
    213 
    214 	if (argc == 1) {
    215 		register int i, j, w;
    216 		int columns, width = 0, lines;
    217 		extern int NCMDS;
    218 
    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);
    222 
    223 			if (len > width)
    224 				width = len;
    225 		}
    226 		width = (width + 8) &~ 7;
    227 		columns = 80 / width;
    228 		if (columns == 0)
    229 			columns = 1;
    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]) {
    236 					printf("\n");
    237 					break;
    238 				}
    239 				w = strlen(c->c_name);
    240 				while (w < width) {
    241 					w = (w + 8) &~ 7;
    242 					putchar('\t');
    243 				}
    244 			}
    245 		}
    246 		return;
    247 	}
    248 	while (--argc > 0) {
    249 		register char *arg;
    250 		arg = *++argv;
    251 		c = getcmd(arg);
    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);
    256 		else
    257 			printf("%-*s\t%s\n", (int)HELPINDENT,
    258 				c->c_name, c->c_help);
    259 	}
    260 }
    261