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