Home | History | Annotate | Line # | Download | only in ssh
ssh.c revision 1.2
      1  1.2  wiz /*	$NetBSD: ssh.c,v 1.2 2002/05/26 00:09:09 wiz Exp $	*/
      2  1.1  gwr 
      3  1.1  gwr /*
      4  1.1  gwr  * Copyright (c) 1995 Gordon W. Ross
      5  1.1  gwr  * All rights reserved.
      6  1.1  gwr  *
      7  1.1  gwr  * Redistribution and use in source and binary forms, with or without
      8  1.1  gwr  * modification, are permitted provided that the following conditions
      9  1.1  gwr  * are met:
     10  1.1  gwr  * 1. Redistributions of source code must retain the above copyright
     11  1.1  gwr  *    notice, this list of conditions and the following disclaimer.
     12  1.1  gwr  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  gwr  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  gwr  *    documentation and/or other materials provided with the distribution.
     15  1.1  gwr  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  gwr  *    derived from this software without specific prior written permission.
     17  1.1  gwr  * 4. All advertising materials mentioning features or use of this software
     18  1.1  gwr  *    must display the following acknowledgement:
     19  1.1  gwr  *      This product includes software developed by Gordon W. Ross
     20  1.1  gwr  *
     21  1.1  gwr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  gwr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  gwr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  gwr  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  gwr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  gwr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  gwr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  gwr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  gwr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  gwr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  gwr  */
     32  1.1  gwr 
     33  1.1  gwr /*
     34  1.1  gwr  * Small Shell - Nothing fancy.  Just runs programs.
     35  1.1  gwr  * The RAMDISK root uses this to save space.
     36  1.1  gwr  */
     37  1.1  gwr 
     38  1.1  gwr #include <errno.h>
     39  1.1  gwr #include <fcntl.h>
     40  1.1  gwr #include <setjmp.h>
     41  1.1  gwr #include <signal.h>
     42  1.1  gwr #include <stdio.h>
     43  1.1  gwr #include <stdlib.h>
     44  1.1  gwr #include <string.h>
     45  1.1  gwr #include <unistd.h>
     46  1.1  gwr 
     47  1.1  gwr #include <sys/param.h>
     48  1.1  gwr #include <sys/wait.h>
     49  1.1  gwr 
     50  1.1  gwr /* XXX - SunOS hacks... */
     51  1.1  gwr #ifndef	WCOREDUMP
     52  1.1  gwr #define	WCOREDUMP(x) ((x) & 0200)
     53  1.1  gwr #endif
     54  1.1  gwr 
     55  1.1  gwr #ifndef	__P
     56  1.1  gwr #define __P(x) x
     57  1.1  gwr #endif	/* __P */
     58  1.1  gwr 
     59  1.1  gwr extern char *optarg;
     60  1.1  gwr extern int optind, opterr;
     61  1.1  gwr 
     62  1.1  gwr #define MAXLINE 256
     63  1.1  gwr #define MAXARGS 32
     64  1.1  gwr 
     65  1.1  gwr #define	MAXPATH 256
     66  1.1  gwr char cur_path[MAXPATH] = "PATH=/bin:/usr/bin";
     67  1.1  gwr 
     68  1.1  gwr char rc_name[] = ".sshrc";
     69  1.1  gwr char *prompt = "ssh: ";
     70  1.1  gwr 
     71  1.1  gwr int eflag;	/* exit on cmd failure */
     72  1.1  gwr int iflag;	/* interactive mode (catch interrupts) */
     73  1.1  gwr int sflag;	/* read from stdin (ignore file arg) */
     74  1.1  gwr int xflag;	/* execution trace */
     75  1.1  gwr 
     76  1.1  gwr /* Command file: name, line number, arg count, arg vector */
     77  1.1  gwr char *cf_name;
     78  1.1  gwr int cf_line;
     79  1.1  gwr int cf_argc;
     80  1.1  gwr char **cf_argv;
     81  1.1  gwr 
     82  1.1  gwr int def_omode = 0666;
     83  1.1  gwr int run_bg_pid;
     84  1.1  gwr 
     85  1.1  gwr jmp_buf next_cmd;
     86  1.1  gwr 
     87  1.1  gwr void catchsig __P((int sig));
     88  1.1  gwr void child_newfd __P((int setfd, char *file, int otype));
     89  1.1  gwr int find_in_path __P((char *cmd, char *filebuf));
     90  1.1  gwr void print_termsig __P((FILE *fp, int cstat));
     91  1.1  gwr int runfile __P((FILE *fp));
     92  1.1  gwr 
     93  1.1  gwr 
     94  1.1  gwr main(argc, argv)
     95  1.1  gwr 	int argc;
     96  1.1  gwr 	char **argv;
     97  1.1  gwr {
     98  1.1  gwr 	struct sigaction sa;
     99  1.1  gwr 	FILE *cfp;		/* command file ptr */
    100  1.1  gwr 	int c, sig;
    101  1.1  gwr 	int error = 0;
    102  1.1  gwr 
    103  1.1  gwr 	while ((c = getopt(argc, argv, "eisx")) != -1) {
    104  1.1  gwr 		switch (c) {
    105  1.1  gwr 		case 'e':
    106  1.1  gwr 			eflag++;
    107  1.1  gwr 			break;
    108  1.1  gwr 		case 'i':
    109  1.1  gwr 			eflag++;
    110  1.1  gwr 			break;
    111  1.1  gwr 		case 's':
    112  1.1  gwr 			sflag++;
    113  1.1  gwr 			break;
    114  1.1  gwr 		case 'x':
    115  1.1  gwr 			xflag++;
    116  1.1  gwr 			break;
    117  1.1  gwr 		case '?':
    118  1.1  gwr 			error++;
    119  1.1  gwr 			break;
    120  1.1  gwr 		}
    121  1.1  gwr 	}
    122  1.1  gwr 	if (error) {
    123  1.1  gwr 		fprintf(stderr, "usage:  ssh [-eisx] [cmd_file [...]]\n");
    124  1.1  gwr 		exit(1);
    125  1.1  gwr 	}
    126  1.1  gwr 	cf_argc = argc - optind;
    127  1.1  gwr 	cf_argv = &argv[optind];
    128  1.1  gwr 
    129  1.1  gwr 	/* If this is a login shell, run the rc file. */
    130  1.1  gwr 	if (argv[0] && argv[0][0] == '-') {
    131  1.1  gwr 		cf_line = 0;
    132  1.1  gwr 		cf_name = rc_name;
    133  1.1  gwr 		if ((cfp = fopen(cf_name, "r")) != NULL) {
    134  1.1  gwr 			error = runfile(cfp);
    135  1.1  gwr 			fclose(cfp);
    136  1.1  gwr 		}
    137  1.1  gwr 	}
    138  1.1  gwr 
    139  1.1  gwr 	/* If no file names, read commands from stdin. */
    140  1.1  gwr 	if (cf_argc == 0)
    141  1.1  gwr 		sflag++;
    142  1.1  gwr 	/* If stdin is a tty, be interactive. */
    143  1.1  gwr 	if (sflag && isatty(fileno(stdin)))
    144  1.1  gwr 		iflag++;
    145  1.1  gwr 
    146  1.1  gwr 	/* Maybe run a command file... */
    147  1.1  gwr 	if (!sflag && cf_argc) {
    148  1.1  gwr 		cf_line = 0;
    149  1.1  gwr 		cf_name = cf_argv[0];
    150  1.1  gwr 		cfp = fopen(cf_name, "r");
    151  1.1  gwr 		if (cfp == NULL) {
    152  1.1  gwr 			perror(cf_name);
    153  1.1  gwr 			exit(1);
    154  1.1  gwr 		}
    155  1.1  gwr 		error = runfile(cfp);
    156  1.1  gwr 		fclose(cfp);
    157  1.1  gwr 		exit(error);
    158  1.1  gwr 	}
    159  1.1  gwr 
    160  1.1  gwr 	/* Read commands from stdin. */
    161  1.1  gwr 	cf_line = 0;
    162  1.1  gwr 	cf_name = "(stdin)";
    163  1.1  gwr 	if (iflag) {
    164  1.1  gwr 		eflag = 0;	/* don't kill shell on error. */
    165  1.1  gwr 		sig = setjmp(next_cmd);
    166  1.1  gwr 		if (sig == 0) {
    167  1.1  gwr 			/* Initialization... */
    168  1.1  gwr 			sa.sa_handler = catchsig;
    169  1.1  gwr 			sa.sa_flags = 0;
    170  1.1  gwr 			sigemptyset(&sa.sa_mask);
    171  1.1  gwr 			sigaction(SIGINT,  &sa, NULL);
    172  1.1  gwr 			sigaction(SIGQUIT, &sa, NULL);
    173  1.1  gwr 			sigaction(SIGTERM, &sa, NULL);
    174  1.1  gwr 		} else {
    175  1.1  gwr 			/* Got here via longjmp. */
    176  1.1  gwr 			fprintf(stderr, " signal %d\n", sig);
    177  1.1  gwr 			sigemptyset(&sa.sa_mask);
    178  1.1  gwr 			sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
    179  1.1  gwr 		}
    180  1.1  gwr 	}
    181  1.1  gwr 	error = runfile(stdin);
    182  1.1  gwr 	exit (error);
    183  1.1  gwr }
    184  1.1  gwr 
    185  1.1  gwr void
    186  1.1  gwr catchsig(sig)
    187  1.1  gwr 	int sig;
    188  1.1  gwr {
    189  1.1  gwr 	longjmp(next_cmd, sig);
    190  1.1  gwr }
    191  1.1  gwr 
    192  1.1  gwr /*
    193  1.1  gwr  * Run command from the passed stdio file pointer.
    194  1.1  gwr  * Returns exit status.
    195  1.1  gwr  */
    196  1.1  gwr int
    197  1.1  gwr runfile(cfp)
    198  1.1  gwr 	FILE *cfp;
    199  1.1  gwr {
    200  1.1  gwr 	char ibuf[MAXLINE];
    201  1.1  gwr 	char *argv[MAXARGS];
    202  1.1  gwr 	char *p;
    203  1.1  gwr 	int i, argc, exitcode, cpid, cstat;
    204  1.1  gwr 
    205  1.1  gwr 	/* The command loop. */
    206  1.1  gwr 	exitcode = 0;
    207  1.1  gwr 	for (;;) {
    208  1.1  gwr 		if (iflag) {
    209  1.1  gwr 			fprintf(stderr, prompt);
    210  1.1  gwr 			fflush(stderr);
    211  1.1  gwr 		}
    212  1.1  gwr 
    213  1.1  gwr 		if ((fgets(ibuf, sizeof(ibuf), cfp)) == NULL)
    214  1.1  gwr 			break;
    215  1.1  gwr 		cf_line++;
    216  1.1  gwr 
    217  1.1  gwr 		argc = 0;
    218  1.1  gwr 		p = ibuf;
    219  1.1  gwr 
    220  1.1  gwr 		while (argc < MAXARGS-1) {
    221  1.1  gwr 			/* skip blanks or tabs */
    222  1.1  gwr 			while ((*p == ' ') || (*p == '\t')) {
    223  1.1  gwr 			next_token:
    224  1.1  gwr 				*p++ = '\0';
    225  1.1  gwr 			}
    226  1.1  gwr 			/* end of line? */
    227  1.1  gwr 			if ((*p == '\n') || (*p == '#')) {
    228  1.1  gwr 				*p = '\0';
    229  1.1  gwr 				break;	/* to eol */
    230  1.1  gwr 			}
    231  1.1  gwr 			if (*p == '\0')
    232  1.1  gwr 				break;
    233  1.1  gwr 			/* save start of token */
    234  1.1  gwr 			argv[argc++] = p;
    235  1.1  gwr 			/* find end of token */
    236  1.1  gwr 			while (*p) {
    237  1.1  gwr 				if ((*p == '\n') || (*p == '#')) {
    238  1.1  gwr 					*p = '\0';
    239  1.1  gwr 					goto eol;
    240  1.1  gwr 				}
    241  1.1  gwr 				if ((*p == ' ') || (*p == '\t'))
    242  1.1  gwr 					goto next_token;
    243  1.1  gwr 				p++;
    244  1.1  gwr 			}
    245  1.1  gwr 		}
    246  1.1  gwr 	eol:
    247  1.1  gwr 
    248  1.1  gwr 		if (argc > 0) {
    249  1.1  gwr 			if (xflag) {
    250  1.1  gwr 				fprintf(stderr, "x");
    251  1.1  gwr 				for (i = 0; i < argc; i++) {
    252  1.1  gwr 					fprintf(stderr, " %s", argv[i]);
    253  1.1  gwr 				}
    254  1.1  gwr 				fprintf(stderr, "\n");
    255  1.1  gwr 			}
    256  1.1  gwr 			argv[argc] = NULL;
    257  1.1  gwr 			exitcode = cmd_eval(argc, argv);
    258  1.1  gwr 		}
    259  1.1  gwr 
    260  1.1  gwr 		/* Collect children. */
    261  1.1  gwr 		while ((cpid = waitpid(0, &cstat, WNOHANG)) > 0) {
    262  1.1  gwr 			if (iflag) {
    263  1.1  gwr 				fprintf(stderr, "[%d] ", cpid);
    264  1.1  gwr 				if (WTERMSIG(cstat)) {
    265  1.1  gwr 					print_termsig(stderr, cstat);
    266  1.1  gwr 				} else {
    267  1.1  gwr 					fprintf(stderr, "Exited, status %d\n",
    268  1.1  gwr 							WEXITSTATUS(cstat));
    269  1.1  gwr 				}
    270  1.1  gwr 			}
    271  1.1  gwr 		}
    272  1.1  gwr 
    273  1.1  gwr 		if (exitcode && eflag)
    274  1.1  gwr 			break;
    275  1.1  gwr 	}
    276  1.1  gwr 	/* return status of last command */
    277  1.1  gwr 	return (exitcode);
    278  1.1  gwr }
    279  1.1  gwr 
    280  1.1  gwr 
    281  1.1  gwr /****************************************************************
    282  1.1  gwr  *  Table of buildin commands
    283  1.1  gwr  *  for cmd_eval() to search...
    284  1.1  gwr  ****************************************************************/
    285  1.1  gwr 
    286  1.1  gwr struct cmd {
    287  1.1  gwr 	char *name;
    288  1.1  gwr 	int (*func)();
    289  1.1  gwr 	char *help;
    290  1.1  gwr };
    291  1.1  gwr struct cmd cmd_table[];
    292  1.1  gwr 
    293  1.1  gwr /*
    294  1.1  gwr  * Evaluate a command named as argv[0]
    295  1.1  gwr  * with arguments argv[1],argv[2]...
    296  1.1  gwr  * Returns exit status.
    297  1.1  gwr  */
    298  1.1  gwr int
    299  1.1  gwr cmd_eval(argc, argv)
    300  1.1  gwr 	int argc;
    301  1.1  gwr 	char **argv;
    302  1.1  gwr {
    303  1.1  gwr 	struct cmd *cp;
    304  1.1  gwr 
    305  1.1  gwr 	/*
    306  1.1  gwr 	 * Do linear search for a builtin command.
    307  1.1  gwr 	 * Performance does not matter here.
    308  1.1  gwr 	 */
    309  1.1  gwr 	for (cp = cmd_table; cp->name; cp++) {
    310  1.1  gwr 		if (!strcmp(cp->name, argv[0])) {
    311  1.1  gwr 			/* Pass only args to builtin. */
    312  1.1  gwr 			--argc; argv++;
    313  1.1  gwr 			return (cp->func(argc, argv));
    314  1.1  gwr 		}
    315  1.1  gwr 	}
    316  1.1  gwr 
    317  1.1  gwr 	/*
    318  1.1  gwr 	 * If no matching builtin, let "run ..."
    319  1.1  gwr 	 * have a chance to try an external.
    320  1.1  gwr 	 */
    321  1.1  gwr 	return (cmd_run(argc, argv));
    322  1.1  gwr }
    323  1.1  gwr 
    324  1.1  gwr /*****************************************************************
    325  1.1  gwr  *  Here are the actual commands.  For these,
    326  1.1  gwr  *  the command name has been skipped, so
    327  1.1  gwr  *  argv[0] is the first arg (if any args).
    328  1.1  gwr  *  All return an exit status.
    329  1.1  gwr  ****************************************************************/
    330  1.1  gwr 
    331  1.1  gwr char help_cd[] = "cd [dir]";
    332  1.1  gwr 
    333  1.1  gwr int
    334  1.1  gwr cmd_cd(argc, argv)
    335  1.1  gwr 	int argc;
    336  1.1  gwr 	char **argv;
    337  1.1  gwr {
    338  1.1  gwr 	char *dir;
    339  1.1  gwr 	int err;
    340  1.1  gwr 
    341  1.1  gwr 	if (argc > 0)
    342  1.1  gwr 		dir = argv[0];
    343  1.1  gwr 	else {
    344  1.1  gwr 		dir = getenv("HOME");
    345  1.1  gwr 		if (dir == NULL)
    346  1.1  gwr 			dir = "/";
    347  1.1  gwr 	}
    348  1.1  gwr 	if (chdir(dir)) {
    349  1.1  gwr 		perror(dir);
    350  1.1  gwr 		return (1);
    351  1.1  gwr 	}
    352  1.1  gwr 	return(0);
    353  1.1  gwr }
    354  1.1  gwr 
    355  1.1  gwr char help_exit[] = "exit [n]";
    356  1.1  gwr 
    357  1.1  gwr int
    358  1.1  gwr cmd_exit(argc, argv)
    359  1.1  gwr 	int argc;
    360  1.1  gwr 	char **argv;
    361  1.1  gwr {
    362  1.1  gwr 	int val = 0;
    363  1.1  gwr 
    364  1.1  gwr 	if (argc > 0)
    365  1.1  gwr 		val = atoi(argv[0]);
    366  1.1  gwr 	exit(val);
    367  1.1  gwr }
    368  1.1  gwr 
    369  1.1  gwr char help_help[] = "help [command]";
    370  1.1  gwr 
    371  1.1  gwr int
    372  1.1  gwr cmd_help(argc, argv)
    373  1.1  gwr 	int argc;
    374  1.1  gwr 	char **argv;
    375  1.1  gwr {
    376  1.1  gwr 	struct cmd *cp;
    377  1.1  gwr 
    378  1.1  gwr 	if (argc > 0) {
    379  1.1  gwr 		for (cp = cmd_table; cp->name; cp++) {
    380  1.1  gwr 			if (!strcmp(cp->name, argv[0])) {
    381  1.1  gwr 				printf("usage:  %s\n", cp->help);
    382  1.1  gwr 				return (0);
    383  1.1  gwr 			}
    384  1.1  gwr 		}
    385  1.1  gwr 		printf("%s: no such command\n", argv[0]);
    386  1.1  gwr 	}
    387  1.1  gwr 
    388  1.1  gwr 	printf("Builtin commands: ");
    389  1.1  gwr 	for (cp = cmd_table; cp->name; cp++) {
    390  1.1  gwr 		printf(" %s", cp->name);
    391  1.1  gwr 	}
    392  1.1  gwr 	printf("\nFor specific usage:  help [command]\n");
    393  1.1  gwr 	return (0);
    394  1.1  gwr }
    395  1.1  gwr 
    396  1.1  gwr char help_path[] = "path [dir1:dir2:...]";
    397  1.1  gwr 
    398  1.1  gwr int
    399  1.1  gwr cmd_path(argc, argv)
    400  1.1  gwr 	int argc;
    401  1.1  gwr 	char **argv;
    402  1.1  gwr {
    403  1.1  gwr 	int i;
    404  1.1  gwr 
    405  1.1  gwr 	if (argc <= 0) {
    406  1.1  gwr 		printf("%s\n", cur_path);
    407  1.1  gwr 		return(0);
    408  1.1  gwr 	}
    409  1.1  gwr 
    410  1.1  gwr 	strncpy(cur_path+5, argv[0], MAXPATH-6);
    411  1.1  gwr 	putenv(cur_path);
    412  1.1  gwr 
    413  1.1  gwr 	return (0);
    414  1.1  gwr }
    415  1.1  gwr 
    416  1.1  gwr /*****************************************************************
    417  1.1  gwr  *  The "run" command is the big one.
    418  1.1  gwr  *  Does fork/exec/wait, redirection...
    419  1.1  gwr  *  Returns exit status of child
    420  1.1  gwr  *  (or zero for a background job)
    421  1.1  gwr  ****************************************************************/
    422  1.1  gwr 
    423  1.1  gwr char help_run[] = "\
    424  1.1  gwr run [-bg] [-i ifile] [-o ofile] [-e efile] program [args...]\n\
    425  1.1  gwr or simply:  program [args...]";
    426  1.1  gwr 
    427  1.1  gwr int
    428  1.1  gwr cmd_run(argc, argv)
    429  1.1  gwr 	int argc;
    430  1.1  gwr 	char **argv;
    431  1.1  gwr {
    432  1.1  gwr 	struct sigaction sa;
    433  1.1  gwr 	int pid, err, cstat, fd;
    434  1.1  gwr 	char file[MAXPATHLEN];
    435  1.1  gwr 	int background;
    436  1.1  gwr 	char *opt, *ifile, *ofile, *efile;
    437  1.1  gwr 	extern char **environ;
    438  1.1  gwr 
    439  1.1  gwr 	/*
    440  1.1  gwr 	 * Parse options:
    441  1.1  gwr 	 * -b  : background
    442  1.1  gwr 	 * -i  : input file
    443  1.1  gwr 	 * -o  : output file
    444  1.1  gwr 	 * -e  : error file
    445  1.1  gwr 	 */
    446  1.1  gwr 	background = 0;
    447  1.1  gwr 	ifile = ofile = efile = NULL;
    448  1.1  gwr 	while ((argc > 0) && (argv[0][0] == '-')) {
    449  1.1  gwr 		opt = argv[0];
    450  1.1  gwr 		--argc; argv++;
    451  1.1  gwr 		switch (opt[1]) {
    452  1.1  gwr 		case 'b':
    453  1.1  gwr 			background++;
    454  1.1  gwr 			break;
    455  1.1  gwr 		case 'i':
    456  1.1  gwr 			ifile = argv[0];
    457  1.1  gwr 			goto shift;
    458  1.1  gwr 		case 'o':
    459  1.1  gwr 			ofile = argv[0];
    460  1.1  gwr 			goto shift;
    461  1.1  gwr 		case 'e':
    462  1.1  gwr 			efile = argv[0];
    463  1.1  gwr 			goto shift;
    464  1.1  gwr 		default:
    465  1.1  gwr 			fprintf(stderr, "run %s: bad option\n", opt);
    466  1.1  gwr 			return (1);
    467  1.1  gwr 		shift:
    468  1.1  gwr 			--argc; argv++;
    469  1.1  gwr 		}
    470  1.1  gwr 	}
    471  1.1  gwr 
    472  1.1  gwr 	if (argc <= 0) {
    473  1.1  gwr 		fprintf(stderr, "%s:%d run: missing command\n",
    474  1.1  gwr 				cf_name, cf_line);
    475  1.1  gwr 		return (1);
    476  1.1  gwr 	}
    477  1.1  gwr 
    478  1.1  gwr 	/* Commands containing '/' get no path search. */
    479  1.1  gwr 	if (strchr(argv[0], '/')) {
    480  1.1  gwr 		strncpy(file, argv[0], sizeof(file)-1);
    481  1.1  gwr 		if (access(file, X_OK)) {
    482  1.1  gwr 			perror(file);
    483  1.1  gwr 			return (1);
    484  1.1  gwr 		}
    485  1.1  gwr 	} else {
    486  1.1  gwr 		if (find_in_path(argv[0], file)) {
    487  1.1  gwr 			fprintf(stderr, "%s: command not found\n", argv[0]);
    488  1.1  gwr 			return (1);
    489  1.1  gwr 		}
    490  1.1  gwr 	}
    491  1.1  gwr 
    492  1.1  gwr 	pid = fork();
    493  1.1  gwr 	if (pid == 0) {
    494  1.1  gwr 		/* child runs this */
    495  1.1  gwr 		/* handle redirection options... */
    496  1.1  gwr 		if (ifile)
    497  1.1  gwr 			child_newfd(0, ifile, O_RDONLY);
    498  1.1  gwr 		if (ofile)
    499  1.1  gwr 			child_newfd(1, ofile, O_WRONLY|O_CREAT);
    500  1.1  gwr 		if (efile)
    501  1.1  gwr 			child_newfd(2, efile, O_WRONLY|O_CREAT);
    502  1.1  gwr 		if (background) {
    503  1.1  gwr 			/* Ignore SIGINT, SIGQUIT */
    504  1.1  gwr 			sa.sa_handler = SIG_IGN;
    505  1.1  gwr 			sa.sa_flags = 0;
    506  1.1  gwr 			sigemptyset(&sa.sa_mask);
    507  1.1  gwr 			sigaction(SIGINT,  &sa, NULL);
    508  1.1  gwr 			sigaction(SIGQUIT, &sa, NULL);
    509  1.1  gwr 		}
    510  1.1  gwr 		err = execve(file, argv, environ);
    511  1.1  gwr 		perror(argv[0]);
    512  1.1  gwr 		return (1);
    513  1.1  gwr 	}
    514  1.1  gwr 	/* parent */
    515  1.1  gwr 	/* Handle background option... */
    516  1.1  gwr 	if (background) {
    517  1.1  gwr 		fprintf(stderr, "[%d]\n", pid);
    518  1.1  gwr 		run_bg_pid = pid;
    519  1.1  gwr 		return (0);
    520  1.1  gwr 	}
    521  1.1  gwr 	if (waitpid(pid, &cstat, 0) < 0) {
    522  1.1  gwr 		perror("waitpid");
    523  1.1  gwr 		return (1);
    524  1.1  gwr 	}
    525  1.1  gwr 	if (WTERMSIG(cstat)) {
    526  1.1  gwr 		print_termsig(stderr, cstat);
    527  1.1  gwr 	}
    528  1.1  gwr 	return (WEXITSTATUS(cstat));
    529  1.1  gwr }
    530  1.1  gwr 
    531  1.1  gwr /*****************************************************************
    532  1.1  gwr  *  table of builtin commands
    533  1.1  gwr  ****************************************************************/
    534  1.1  gwr struct cmd cmd_table[] = {
    535  1.1  gwr 	{ "cd",   cmd_cd,   help_cd },
    536  1.1  gwr 	{ "exit", cmd_exit, help_exit },
    537  1.1  gwr 	{ "help", cmd_help, help_help },
    538  1.1  gwr 	{ "path", cmd_path, help_path },
    539  1.1  gwr 	{ "run",  cmd_run,  help_run },
    540  1.1  gwr 	{ 0 },
    541  1.1  gwr };
    542  1.1  gwr 
    543  1.1  gwr /*****************************************************************
    544  1.1  gwr  *  helper functions for the "run" command
    545  1.1  gwr  ****************************************************************/
    546  1.1  gwr 
    547  1.1  gwr int
    548  1.1  gwr find_in_path(cmd, filebuf)
    549  1.1  gwr 	char *cmd;
    550  1.1  gwr 	char *filebuf;
    551  1.1  gwr {
    552  1.1  gwr 	char *dirp, *endp, *bufp;	/* dir, end */
    553  1.1  gwr 
    554  1.1  gwr 	dirp = cur_path + 5;
    555  1.1  gwr 	while (*dirp) {
    556  1.1  gwr 		endp = dirp;
    557  1.1  gwr 		bufp = filebuf;
    558  1.1  gwr 		while (*endp && (*endp != ':'))
    559  1.1  gwr 			*bufp++ = *endp++;
    560  1.1  gwr 		*bufp++ = '/';
    561  1.1  gwr 		strcpy(bufp, cmd);
    562  1.1  gwr 		if (access(filebuf, X_OK) == 0)
    563  1.1  gwr 			return (0);
    564  1.1  gwr 		if (*endp == ':')
    565  1.1  gwr 			endp++;
    566  1.1  gwr 		dirp = endp;	/* next dir */
    567  1.1  gwr 	}
    568  1.1  gwr 	return (-1);
    569  1.1  gwr }
    570  1.1  gwr 
    571  1.1  gwr /*
    572  1.1  gwr  * Set the file descriptor SETFD to FILE,
    573  1.1  gwr  * which was opened with OTYPE and MODE.
    574  1.1  gwr  */
    575  1.1  gwr void
    576  1.1  gwr child_newfd(setfd, file, otype)
    577  1.1  gwr 	int setfd;	/* what to set (i.e. 0,1,2) */
    578  1.1  gwr 	char *file;
    579  1.1  gwr 	int otype;	/* O_RDONLY, etc. */
    580  1.1  gwr {
    581  1.1  gwr 	int newfd;
    582  1.1  gwr 
    583  1.1  gwr 	close(setfd);
    584  1.1  gwr 	if ((newfd = open(file, otype, def_omode)) < 0) {
    585  1.1  gwr 		perror(file);
    586  1.1  gwr 		exit(1);
    587  1.1  gwr 	}
    588  1.1  gwr 	if (newfd != setfd) {
    589  1.1  gwr 		dup2(newfd, setfd);
    590  1.1  gwr 		close(newfd);
    591  1.1  gwr 	}
    592  1.1  gwr }
    593  1.1  gwr 
    594  1.1  gwr void
    595  1.1  gwr print_termsig(fp, cstat)
    596  1.1  gwr 	FILE *fp;
    597  1.1  gwr 	int cstat;
    598  1.1  gwr {
    599  1.1  gwr 	fprintf(fp, "Terminated, signal %d",
    600  1.1  gwr 			WTERMSIG(cstat));
    601  1.1  gwr 	if (WCOREDUMP(cstat))
    602  1.1  gwr 		fprintf(fp, " (core dumped)");
    603  1.1  gwr 	fprintf(fp, "\n");
    604  1.1  gwr }
    605