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