xref: /csrg-svn/bin/sh/exec.c (revision 55297)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char sccsid[] = "@(#)exec.c	5.2 (Berkeley) 3/13/91";
39 #endif /* not lint */
40 
41 /*
42  * When commands are first encountered, they are entered in a hash table.
43  * This ensures that a full path search will not have to be done for them
44  * on each invocation.
45  *
46  * We should investigate converting to a linear search, even though that
47  * would make the command name "hash" a misnomer.
48  */
49 
50 #include "shell.h"
51 #include "main.h"
52 #include "nodes.h"
53 #include "parser.h"
54 #include "redir.h"
55 #include "eval.h"
56 #include "exec.h"
57 #include "builtins.h"
58 #include "var.h"
59 #include "options.h"
60 #include "input.h"
61 #include "output.h"
62 #include "syntax.h"
63 #include "memalloc.h"
64 #include "error.h"
65 #include "init.h"
66 #include "mystring.h"
67 #include <sys/types.h>
68 #include <sys/stat.h>
69 #include <fcntl.h>
70 #include <errno.h>
71 
72 
73 #define CMDTABLESIZE 31		/* should be prime */
74 #define ARB 1			/* actual size determined at run time */
75 
76 
77 
78 struct tblentry {
79 	struct tblentry *next;	/* next entry in hash chain */
80 	union param param;	/* definition of builtin function */
81 	short cmdtype;		/* index identifying command */
82 	char rehash;		/* if set, cd done since entry created */
83 	char cmdname[ARB];	/* name of command */
84 };
85 
86 
87 STATIC struct tblentry *cmdtable[CMDTABLESIZE];
88 STATIC int builtinloc = -1;		/* index in path of %builtin, or -1 */
89 
90 
91 #ifdef __STDC__
92 STATIC void tryexec(char *, char **, char **);
93 STATIC void execinterp(char **, char **);
94 STATIC void printentry(struct tblentry *, int);
95 STATIC void clearcmdentry(int);
96 STATIC struct tblentry *cmdlookup(char *, int);
97 STATIC void delete_cmd_entry(void);
98 #else
99 STATIC void tryexec();
100 STATIC void execinterp();
101 STATIC void printentry();
102 STATIC void clearcmdentry();
103 STATIC struct tblentry *cmdlookup();
104 STATIC void delete_cmd_entry();
105 #endif
106 
107 
108 
109 /*
110  * Exec a program.  Never returns.  If you change this routine, you may
111  * have to change the find_command routine as well.
112  */
113 
114 void
115 shellexec(argv, envp, path, index)
116 	char **argv, **envp;
117 	char *path;
118 	{
119 	char *cmdname;
120 	int e;
121 
122 	if (strchr(argv[0], '/') != NULL) {
123 		tryexec(argv[0], argv, envp);
124 		e = errno;
125 	} else {
126 		e = ENOENT;
127 		while ((cmdname = padvance(&path, argv[0])) != NULL) {
128 			if (--index < 0 && pathopt == NULL) {
129 				tryexec(cmdname, argv, envp);
130 				if (errno != ENOENT && errno != ENOTDIR)
131 					e = errno;
132 			}
133 			stunalloc(cmdname);
134 		}
135 	}
136 	error2(argv[0], errmsg(e, E_EXEC));
137 }
138 
139 
140 STATIC void
141 tryexec(cmd, argv, envp)
142 	char *cmd;
143 	char **argv;
144 	char **envp;
145 	{
146 	int e;
147 	char *p;
148 
149 #ifdef SYSV
150 	do {
151 		execve(cmd, argv, envp);
152 	} while (errno == EINTR);
153 #else
154 	execve(cmd, argv, envp);
155 #endif
156 	e = errno;
157 	if (e == ENOEXEC) {
158 		initshellproc();
159 		setinputfile(cmd, 0);
160 		commandname = arg0 = savestr(argv[0]);
161 #ifndef BSD
162 		pgetc(); pungetc();		/* fill up input buffer */
163 		p = parsenextc;
164 		if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
165 			argv[0] = cmd;
166 			execinterp(argv, envp);
167 		}
168 #endif
169 		setparam(argv + 1);
170 		exraise(EXSHELLPROC);
171 		/*NOTREACHED*/
172 	}
173 	errno = e;
174 }
175 
176 
177 #ifndef BSD
178 /*
179  * Execute an interpreter introduced by "#!", for systems where this
180  * feature has not been built into the kernel.  If the interpreter is
181  * the shell, return (effectively ignoring the "#!").  If the execution
182  * of the interpreter fails, exit.
183  *
184  * This code peeks inside the input buffer in order to avoid actually
185  * reading any input.  It would benefit from a rewrite.
186  */
187 
188 #define NEWARGS 5
189 
190 STATIC void
191 execinterp(argv, envp)
192 	char **argv, **envp;
193 	{
194 	int n;
195 	char *inp;
196 	char *outp;
197 	char c;
198 	char *p;
199 	char **ap;
200 	char *newargs[NEWARGS];
201 	int i;
202 	char **ap2;
203 	char **new;
204 
205 	n = parsenleft - 2;
206 	inp = parsenextc + 2;
207 	ap = newargs;
208 	for (;;) {
209 		while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
210 			inp++;
211 		if (n < 0)
212 			goto bad;
213 		if ((c = *inp++) == '\n')
214 			break;
215 		if (ap == &newargs[NEWARGS])
216 bad:		  error("Bad #! line");
217 		STARTSTACKSTR(outp);
218 		do {
219 			STPUTC(c, outp);
220 		} while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n');
221 		STPUTC('\0', outp);
222 		n++, inp--;
223 		*ap++ = grabstackstr(outp);
224 	}
225 	if (ap == newargs + 1) {	/* if no args, maybe no exec is needed */
226 		p = newargs[0];
227 		for (;;) {
228 			if (equal(p, "sh") || equal(p, "ash")) {
229 				return;
230 			}
231 			while (*p != '/') {
232 				if (*p == '\0')
233 					goto break2;
234 				p++;
235 			}
236 			p++;
237 		}
238 break2:;
239 	}
240 	i = (char *)ap - (char *)newargs;		/* size in bytes */
241 	if (i == 0)
242 		error("Bad #! line");
243 	for (ap2 = argv ; *ap2++ != NULL ; );
244 	new = ckmalloc(i + ((char *)ap2 - (char *)argv));
245 	ap = newargs, ap2 = new;
246 	while ((i -= sizeof (char **)) >= 0)
247 		*ap2++ = *ap++;
248 	ap = argv;
249 	while (*ap2++ = *ap++);
250 	shellexec(new, envp, pathval(), 0);
251 }
252 #endif
253 
254 
255 
256 /*
257  * Do a path search.  The variable path (passed by reference) should be
258  * set to the start of the path before the first call; padvance will update
259  * this value as it proceeds.  Successive calls to padvance will return
260  * the possible path expansions in sequence.  If an option (indicated by
261  * a percent sign) appears in the path entry then the global variable
262  * pathopt will be set to point to it; otherwise pathopt will be set to
263  * NULL.
264  */
265 
266 char *pathopt;
267 
268 char *
269 padvance(path, name)
270 	char **path;
271 	char *name;
272 	{
273 	register char *p, *q;
274 	char *start;
275 	int len;
276 
277 	if (*path == NULL)
278 		return NULL;
279 	start = *path;
280 	for (p = start ; *p && *p != ':' && *p != '%' ; p++);
281 	len = p - start + strlen(name) + 2;	/* "2" is for '/' and '\0' */
282 	while (stackblocksize() < len)
283 		growstackblock();
284 	q = stackblock();
285 	if (p != start) {
286 		bcopy(start, q, p - start);
287 		q += p - start;
288 		*q++ = '/';
289 	}
290 	strcpy(q, name);
291 	pathopt = NULL;
292 	if (*p == '%') {
293 		pathopt = ++p;
294 		while (*p && *p != ':')  p++;
295 	}
296 	if (*p == ':')
297 		*path = p + 1;
298 	else
299 		*path = NULL;
300 	return stalloc(len);
301 }
302 
303 
304 
305 /*** Command hashing code ***/
306 
307 
308 hashcmd(argc, argv)  char **argv; {
309 	struct tblentry **pp;
310 	struct tblentry *cmdp;
311 	int c;
312 	int verbose;
313 	struct cmdentry entry;
314 	char *name;
315 
316 	verbose = 0;
317 	while ((c = nextopt("rv")) != '\0') {
318 		if (c == 'r') {
319 			clearcmdentry(0);
320 		} else if (c == 'v') {
321 			verbose++;
322 		}
323 	}
324 	if (*argptr == NULL) {
325 		for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
326 			for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
327 				printentry(cmdp, verbose);
328 			}
329 		}
330 		return 0;
331 	}
332 	while ((name = *argptr) != NULL) {
333 		if ((cmdp = cmdlookup(name, 0)) != NULL
334 		 && (cmdp->cmdtype == CMDNORMAL
335 		     || cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
336 			delete_cmd_entry();
337 		find_command(name, &entry, 1);
338 		if (verbose) {
339 			if (entry.cmdtype != CMDUNKNOWN) {	/* if no error msg */
340 				cmdp = cmdlookup(name, 0);
341 				printentry(cmdp, verbose);
342 			}
343 			flushall();
344 		}
345 		argptr++;
346 	}
347 	return 0;
348 }
349 
350 
351 STATIC void
352 printentry(cmdp, verbose)
353 	struct tblentry *cmdp;
354 	int verbose;
355 	{
356 	int index;
357 	char *path;
358 	char *name;
359 
360 	if (cmdp->cmdtype == CMDNORMAL) {
361 		index = cmdp->param.index;
362 		path = pathval();
363 		do {
364 			name = padvance(&path, cmdp->cmdname);
365 			stunalloc(name);
366 		} while (--index >= 0);
367 		out1str(name);
368 	} else if (cmdp->cmdtype == CMDBUILTIN) {
369 		out1fmt("builtin %s", cmdp->cmdname);
370 	} else if (cmdp->cmdtype == CMDFUNCTION) {
371 		out1fmt("function %s", cmdp->cmdname);
372 		if (verbose) {
373 			INTOFF;
374 			name = commandtext(cmdp->param.func);
375 			out1c(' ');
376 			out1str(name);
377 			ckfree(name);
378 			INTON;
379 		}
380 #ifdef DEBUG
381 	} else {
382 		error("internal error: cmdtype %d", cmdp->cmdtype);
383 #endif
384 	}
385 	if (cmdp->rehash)
386 		out1c('*');
387 	out1c('\n');
388 }
389 
390 
391 
392 /*
393  * Resolve a command name.  If you change this routine, you may have to
394  * change the shellexec routine as well.
395  */
396 
397 void
398 find_command(name, entry, printerr)
399 	char *name;
400 	struct cmdentry *entry;
401 	{
402 	struct tblentry *cmdp;
403 	int index;
404 	int prev;
405 	char *path;
406 	char *fullname;
407 	struct stat statb;
408 	int e;
409 	int i;
410 
411 	/* If name contains a slash, don't use the hash table */
412 	if (strchr(name, '/') != NULL) {
413 		entry->cmdtype = CMDNORMAL;
414 		entry->u.index = 0;
415 		return;
416 	}
417 
418 	/* If name is in the table, and not invalidated by cd, we're done */
419 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0)
420 		goto success;
421 
422 	/* If %builtin not in path, check for builtin next */
423 	if (builtinloc < 0 && (i = find_builtin(name)) >= 0) {
424 		INTOFF;
425 		cmdp = cmdlookup(name, 1);
426 		cmdp->cmdtype = CMDBUILTIN;
427 		cmdp->param.index = i;
428 		INTON;
429 		goto success;
430 	}
431 
432 	/* We have to search path. */
433 	prev = -1;		/* where to start */
434 	if (cmdp) {		/* doing a rehash */
435 		if (cmdp->cmdtype == CMDBUILTIN)
436 			prev = builtinloc;
437 		else
438 			prev = cmdp->param.index;
439 	}
440 
441 	path = pathval();
442 	e = ENOENT;
443 	index = -1;
444 loop:
445 	while ((fullname = padvance(&path, name)) != NULL) {
446 		stunalloc(fullname);
447 		index++;
448 		if (pathopt) {
449 			if (prefix("builtin", pathopt)) {
450 				if ((i = find_builtin(name)) < 0)
451 					goto loop;
452 				INTOFF;
453 				cmdp = cmdlookup(name, 1);
454 				cmdp->cmdtype = CMDBUILTIN;
455 				cmdp->param.index = i;
456 				INTON;
457 				goto success;
458 			} else if (prefix("func", pathopt)) {
459 				/* handled below */
460 			} else {
461 				goto loop;	/* ignore unimplemented options */
462 			}
463 		}
464 		/* if rehash, don't redo absolute path names */
465 		if (fullname[0] == '/' && index <= prev) {
466 			if (index < prev)
467 				goto loop;
468 			TRACE(("searchexec \"%s\": no change\n", name));
469 			goto success;
470 		}
471 		while (stat(fullname, &statb) < 0) {
472 #ifdef SYSV
473 			if (errno == EINTR)
474 				continue;
475 #endif
476 			if (errno != ENOENT && errno != ENOTDIR)
477 				e = errno;
478 			goto loop;
479 		}
480 		e = EACCES;	/* if we fail, this will be the error */
481 		if ((statb.st_mode & S_IFMT) != S_IFREG)
482 			goto loop;
483 		if (pathopt) {		/* this is a %func directory */
484 			stalloc(strlen(fullname) + 1);
485 			readcmdfile(fullname);
486 			if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
487 				error("%s not defined in %s", name, fullname);
488 			stunalloc(fullname);
489 			goto success;
490 		}
491 #ifdef notdef
492 		if (statb.st_uid == geteuid()) {
493 			if ((statb.st_mode & 0100) == 0)
494 				goto loop;
495 		} else if (statb.st_gid == getegid()) {
496 			if ((statb.st_mode & 010) == 0)
497 				goto loop;
498 		} else {
499 			if ((statb.st_mode & 01) == 0)
500 				goto loop;
501 		}
502 #endif
503 		TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
504 		INTOFF;
505 		cmdp = cmdlookup(name, 1);
506 		cmdp->cmdtype = CMDNORMAL;
507 		cmdp->param.index = index;
508 		INTON;
509 		goto success;
510 	}
511 
512 	/* We failed.  If there was an entry for this command, delete it */
513 	if (cmdp)
514 		delete_cmd_entry();
515 	if (printerr)
516 		outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC));
517 	entry->cmdtype = CMDUNKNOWN;
518 	return;
519 
520 success:
521 	cmdp->rehash = 0;
522 	entry->cmdtype = cmdp->cmdtype;
523 	entry->u = cmdp->param;
524 }
525 
526 
527 
528 /*
529  * Search the table of builtin commands.
530  */
531 
532 int
533 find_builtin(name)
534 	char *name;
535 	{
536 	const register struct builtincmd *bp;
537 
538 	for (bp = builtincmd ; bp->name ; bp++) {
539 		if (*bp->name == *name && equal(bp->name, name))
540 			return bp->code;
541 	}
542 	return -1;
543 }
544 
545 
546 
547 /*
548  * Called when a cd is done.  Marks all commands so the next time they
549  * are executed they will be rehashed.
550  */
551 
552 void
553 hashcd() {
554 	struct tblentry **pp;
555 	struct tblentry *cmdp;
556 
557 	for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
558 		for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
559 			if (cmdp->cmdtype == CMDNORMAL
560 			 || cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)
561 				cmdp->rehash = 1;
562 		}
563 	}
564 }
565 
566 
567 
568 /*
569  * Called before PATH is changed.  The argument is the new value of PATH;
570  * pathval() still returns the old value at this point.  Called with
571  * interrupts off.
572  */
573 
574 void
575 changepath(newval)
576 	char *newval;
577 	{
578 	char *old, *new;
579 	int index;
580 	int firstchange;
581 	int bltin;
582 	int hasdot;
583 
584 	old = pathval();
585 	new = newval;
586 	firstchange = 9999;	/* assume no change */
587 	index = hasdot = 0;
588 	bltin = -1;
589 	if (*new == ':')
590 		hasdot++;
591 	for (;;) {
592 		if (*old != *new) {
593 			firstchange = index;
594 			if (*old == '\0' && *new == ':'
595 			 || *old == ':' && *new == '\0')
596 				firstchange++;
597 			old = new;	/* ignore subsequent differences */
598 		}
599 		if (*new == '\0')
600 			break;
601 		if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
602 			bltin = index;
603 		if (*new == ':') {
604 			char c = *(new+1);
605 
606 			index++;
607 			if (c == ':' || c == '\0' || (c == '.' &&
608 			   ((c = *(new+2)) == ':' || c == '\0')))
609 				hasdot++;
610 		}
611 		new++, old++;
612 	}
613 	if (hasdot && geteuid() == 0)
614 		out2str("sh: warning: running as root with dot in PATH\n");
615 	if (builtinloc < 0 && bltin >= 0)
616 		builtinloc = bltin;		/* zap builtins */
617 	if (builtinloc >= 0 && bltin < 0)
618 		firstchange = 0;
619 	clearcmdentry(firstchange);
620 	builtinloc = bltin;
621 }
622 
623 
624 /*
625  * Clear out command entries.  The argument specifies the first entry in
626  * PATH which has changed.
627  */
628 
629 STATIC void
630 clearcmdentry(firstchange) {
631 	struct tblentry **tblp;
632 	struct tblentry **pp;
633 	struct tblentry *cmdp;
634 
635 	INTOFF;
636 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
637 		pp = tblp;
638 		while ((cmdp = *pp) != NULL) {
639 			if (cmdp->cmdtype == CMDNORMAL && cmdp->param.index >= firstchange
640 			 || cmdp->cmdtype == CMDBUILTIN && builtinloc >= firstchange) {
641 				*pp = cmdp->next;
642 				ckfree(cmdp);
643 			} else {
644 				pp = &cmdp->next;
645 			}
646 		}
647 	}
648 	INTON;
649 }
650 
651 
652 /*
653  * Delete all functions.
654  */
655 
656 #ifdef mkinit
657 MKINIT void deletefuncs();
658 
659 SHELLPROC {
660 	deletefuncs();
661 }
662 #endif
663 
664 void
665 deletefuncs() {
666 	struct tblentry **tblp;
667 	struct tblentry **pp;
668 	struct tblentry *cmdp;
669 
670 	INTOFF;
671 	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
672 		pp = tblp;
673 		while ((cmdp = *pp) != NULL) {
674 			if (cmdp->cmdtype == CMDFUNCTION) {
675 				*pp = cmdp->next;
676 				freefunc(cmdp->param.func);
677 				ckfree(cmdp);
678 			} else {
679 				pp = &cmdp->next;
680 			}
681 		}
682 	}
683 	INTON;
684 }
685 
686 
687 
688 /*
689  * Locate a command in the command hash table.  If "add" is nonzero,
690  * add the command to the table if it is not already present.  The
691  * variable "lastcmdentry" is set to point to the address of the link
692  * pointing to the entry, so that delete_cmd_entry can delete the
693  * entry.
694  */
695 
696 struct tblentry **lastcmdentry;
697 
698 
699 STATIC struct tblentry *
700 cmdlookup(name, add)
701 	char *name;
702 	{
703 	int hashval;
704 	register char *p;
705 	struct tblentry *cmdp;
706 	struct tblentry **pp;
707 
708 	p = name;
709 	hashval = *p << 4;
710 	while (*p)
711 		hashval += *p++;
712 	hashval &= 0x7FFF;
713 	pp = &cmdtable[hashval % CMDTABLESIZE];
714 	for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
715 		if (equal(cmdp->cmdname, name))
716 			break;
717 		pp = &cmdp->next;
718 	}
719 	if (add && cmdp == NULL) {
720 		INTOFF;
721 		cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
722 					+ strlen(name) + 1);
723 		cmdp->next = NULL;
724 		cmdp->cmdtype = CMDUNKNOWN;
725 		cmdp->rehash = 0;
726 		strcpy(cmdp->cmdname, name);
727 		INTON;
728 	}
729 	lastcmdentry = pp;
730 	return cmdp;
731 }
732 
733 /*
734  * Delete the command entry returned on the last lookup.
735  */
736 
737 STATIC void
738 delete_cmd_entry() {
739 	struct tblentry *cmdp;
740 
741 	INTOFF;
742 	cmdp = *lastcmdentry;
743 	*lastcmdentry = cmdp->next;
744 	ckfree(cmdp);
745 	INTON;
746 }
747 
748 
749 
750 #ifdef notdef
751 void
752 getcmdentry(name, entry)
753 	char *name;
754 	struct cmdentry *entry;
755 	{
756 	struct tblentry *cmdp = cmdlookup(name, 0);
757 
758 	if (cmdp) {
759 		entry->u = cmdp->param;
760 		entry->cmdtype = cmdp->cmdtype;
761 	} else {
762 		entry->cmdtype = CMDUNKNOWN;
763 		entry->u.index = 0;
764 	}
765 }
766 #endif
767 
768 
769 /*
770  * Add a new command entry, replacing any existing command entry for
771  * the same name.
772  */
773 
774 void
775 addcmdentry(name, entry)
776 	char *name;
777 	struct cmdentry *entry;
778 	{
779 	struct tblentry *cmdp;
780 
781 	INTOFF;
782 	cmdp = cmdlookup(name, 1);
783 	if (cmdp->cmdtype == CMDFUNCTION) {
784 		freefunc(cmdp->param.func);
785 	}
786 	cmdp->cmdtype = entry->cmdtype;
787 	cmdp->param = entry->u;
788 	INTON;
789 }
790 
791 
792 /*
793  * Define a shell function.
794  */
795 
796 void
797 defun(name, func)
798 	char *name;
799 	union node *func;
800 	{
801 	struct cmdentry entry;
802 
803 	INTOFF;
804 	entry.cmdtype = CMDFUNCTION;
805 	entry.u.func = copyfunc(func);
806 	addcmdentry(name, &entry);
807 	INTON;
808 }
809 
810 
811 /*
812  * Delete a function if it exists.
813  */
814 
815 int
816 unsetfunc(name)
817 	char *name;
818 	{
819 	struct tblentry *cmdp;
820 
821 	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
822 		freefunc(cmdp->param.func);
823 		delete_cmd_entry();
824 		return (0);
825 	}
826 	return (1);
827 }
828