Deleted Added
full compact
exec.c (197820) exec.c (200956)
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. 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

--- 22 unchanged lines hidden (view full) ---

31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. 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

--- 22 unchanged lines hidden (view full) ---

31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/exec.c 197820 2009-10-06 22:00:14Z jilles $");
39__FBSDID("$FreeBSD: head/bin/sh/exec.c 200956 2009-12-24 18:41:14Z jilles $");
40
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <unistd.h>
44#include <fcntl.h>
45#include <errno.h>
46#include <stdlib.h>
47

--- 45 unchanged lines hidden (view full) ---

93
94STATIC struct tblentry *cmdtable[CMDTABLESIZE];
95STATIC int builtinloc = -1; /* index in path of %builtin, or -1 */
96int exerrno = 0; /* Last exec error */
97
98
99STATIC void tryexec(char *, char **, char **);
100STATIC void printentry(struct tblentry *, int);
40
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <unistd.h>
44#include <fcntl.h>
45#include <errno.h>
46#include <stdlib.h>
47

--- 45 unchanged lines hidden (view full) ---

93
94STATIC struct tblentry *cmdtable[CMDTABLESIZE];
95STATIC int builtinloc = -1; /* index in path of %builtin, or -1 */
96int exerrno = 0; /* Last exec error */
97
98
99STATIC void tryexec(char *, char **, char **);
100STATIC void printentry(struct tblentry *, int);
101STATIC struct tblentry *cmdlookup(char *, int);
101STATIC struct tblentry *cmdlookup(const char *, int);
102STATIC void delete_cmd_entry(void);
103
104
105
106/*
107 * Exec a program. Never returns. If you change this routine, you may
108 * have to change the find_command routine as well.
109 */
110
111void
102STATIC void delete_cmd_entry(void);
103
104
105
106/*
107 * Exec a program. Never returns. If you change this routine, you may
108 * have to change the find_command routine as well.
109 */
110
111void
112shellexec(char **argv, char **envp, char *path, int index)
112shellexec(char **argv, char **envp, const char *path, int index)
113{
114 char *cmdname;
115 int e;
116
117 if (strchr(argv[0], '/') != NULL) {
118 tryexec(argv[0], argv, envp);
119 e = errno;
120 } else {

--- 49 unchanged lines hidden (view full) ---

170 * set to the start of the path before the first call; padvance will update
171 * this value as it proceeds. Successive calls to padvance will return
172 * the possible path expansions in sequence. If an option (indicated by
173 * a percent sign) appears in the path entry then the global variable
174 * pathopt will be set to point to it; otherwise pathopt will be set to
175 * NULL.
176 */
177
113{
114 char *cmdname;
115 int e;
116
117 if (strchr(argv[0], '/') != NULL) {
118 tryexec(argv[0], argv, envp);
119 e = errno;
120 } else {

--- 49 unchanged lines hidden (view full) ---

170 * set to the start of the path before the first call; padvance will update
171 * this value as it proceeds. Successive calls to padvance will return
172 * the possible path expansions in sequence. If an option (indicated by
173 * a percent sign) appears in the path entry then the global variable
174 * pathopt will be set to point to it; otherwise pathopt will be set to
175 * NULL.
176 */
177
178char *pathopt;
178const char *pathopt;
179
180char *
179
180char *
181padvance(char **path, char *name)
181padvance(const char **path, const char *name)
182{
182{
183 char *p, *q;
184 char *start;
183 const char *p, *start;
184 char *q;
185 int len;
186
187 if (*path == NULL)
188 return NULL;
189 start = *path;
190 for (p = start; *p && *p != ':' && *p != '%'; p++)
191 ; /* nothing */
192 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */

--- 71 unchanged lines hidden (view full) ---

264 return 0;
265}
266
267
268STATIC void
269printentry(struct tblentry *cmdp, int verbose)
270{
271 int index;
185 int len;
186
187 if (*path == NULL)
188 return NULL;
189 start = *path;
190 for (p = start; *p && *p != ':' && *p != '%'; p++)
191 ; /* nothing */
192 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */

--- 71 unchanged lines hidden (view full) ---

264 return 0;
265}
266
267
268STATIC void
269printentry(struct tblentry *cmdp, int verbose)
270{
271 int index;
272 char *path;
272 const char *path;
273 char *name;
274
275 if (cmdp->cmdtype == CMDNORMAL) {
276 index = cmdp->param.index;
277 path = pathval();
278 do {
279 name = padvance(&path, cmdp->cmdname);
280 stunalloc(name);

--- 24 unchanged lines hidden (view full) ---

305
306
307/*
308 * Resolve a command name. If you change this routine, you may have to
309 * change the shellexec routine as well.
310 */
311
312void
273 char *name;
274
275 if (cmdp->cmdtype == CMDNORMAL) {
276 index = cmdp->param.index;
277 path = pathval();
278 do {
279 name = padvance(&path, cmdp->cmdname);
280 stunalloc(name);

--- 24 unchanged lines hidden (view full) ---

305
306
307/*
308 * Resolve a command name. If you change this routine, you may have to
309 * change the shellexec routine as well.
310 */
311
312void
313find_command(char *name, struct cmdentry *entry, int printerr, char *path)
313find_command(const char *name, struct cmdentry *entry, int printerr,
314 const char *path)
314{
315 struct tblentry *cmdp;
316 int index;
317 int prev;
318 char *fullname;
319 struct stat statb;
320 int e;
321 int i;

--- 119 unchanged lines hidden (view full) ---

441
442
443
444/*
445 * Search the table of builtin commands.
446 */
447
448int
315{
316 struct tblentry *cmdp;
317 int index;
318 int prev;
319 char *fullname;
320 struct stat statb;
321 int e;
322 int i;

--- 119 unchanged lines hidden (view full) ---

442
443
444
445/*
446 * Search the table of builtin commands.
447 */
448
449int
449find_builtin(char *name, int *special)
450find_builtin(const char *name, int *special)
450{
451 const struct builtincmd *bp;
452
453 for (bp = builtincmd ; bp->name ; bp++) {
454 if (*bp->name == *name && equal(bp->name, name)) {
455 *special = bp->special;
456 return bp->code;
457 }

--- 145 unchanged lines hidden (view full) ---

603 * pointing to the entry, so that delete_cmd_entry can delete the
604 * entry.
605 */
606
607STATIC struct tblentry **lastcmdentry;
608
609
610STATIC struct tblentry *
451{
452 const struct builtincmd *bp;
453
454 for (bp = builtincmd ; bp->name ; bp++) {
455 if (*bp->name == *name && equal(bp->name, name)) {
456 *special = bp->special;
457 return bp->code;
458 }

--- 145 unchanged lines hidden (view full) ---

604 * pointing to the entry, so that delete_cmd_entry can delete the
605 * entry.
606 */
607
608STATIC struct tblentry **lastcmdentry;
609
610
611STATIC struct tblentry *
611cmdlookup(char *name, int add)
612cmdlookup(const char *name, int add)
612{
613 int hashval;
613{
614 int hashval;
614 char *p;
615 const char *p;
615 struct tblentry *cmdp;
616 struct tblentry **pp;
617
618 p = name;
619 hashval = *p << 4;
620 while (*p)
621 hashval += *p++;
622 hashval &= 0x7FFF;

--- 36 unchanged lines hidden (view full) ---

659
660
661/*
662 * Add a new command entry, replacing any existing command entry for
663 * the same name.
664 */
665
666void
616 struct tblentry *cmdp;
617 struct tblentry **pp;
618
619 p = name;
620 hashval = *p << 4;
621 while (*p)
622 hashval += *p++;
623 hashval &= 0x7FFF;

--- 36 unchanged lines hidden (view full) ---

660
661
662/*
663 * Add a new command entry, replacing any existing command entry for
664 * the same name.
665 */
666
667void
667addcmdentry(char *name, struct cmdentry *entry)
668addcmdentry(const char *name, struct cmdentry *entry)
668{
669 struct tblentry *cmdp;
670
671 INTOFF;
672 cmdp = cmdlookup(name, 1);
673 if (cmdp->cmdtype == CMDFUNCTION) {
674 unreffunc(cmdp->param.func);
675 }
676 cmdp->cmdtype = entry->cmdtype;
677 cmdp->param = entry->u;
678 INTON;
679}
680
681
682/*
683 * Define a shell function.
684 */
685
686void
669{
670 struct tblentry *cmdp;
671
672 INTOFF;
673 cmdp = cmdlookup(name, 1);
674 if (cmdp->cmdtype == CMDFUNCTION) {
675 unreffunc(cmdp->param.func);
676 }
677 cmdp->cmdtype = entry->cmdtype;
678 cmdp->param = entry->u;
679 INTON;
680}
681
682
683/*
684 * Define a shell function.
685 */
686
687void
687defun(char *name, union node *func)
688defun(const char *name, union node *func)
688{
689 struct cmdentry entry;
690
691 INTOFF;
692 entry.cmdtype = CMDFUNCTION;
693 entry.u.func = copyfunc(func);
694 addcmdentry(name, &entry);
695 INTON;
696}
697
698
699/*
700 * Delete a function if it exists.
701 */
702
703int
689{
690 struct cmdentry entry;
691
692 INTOFF;
693 entry.cmdtype = CMDFUNCTION;
694 entry.u.func = copyfunc(func);
695 addcmdentry(name, &entry);
696 INTON;
697}
698
699
700/*
701 * Delete a function if it exists.
702 */
703
704int
704unsetfunc(char *name)
705unsetfunc(const char *name)
705{
706 struct tblentry *cmdp;
707
708 if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
709 unreffunc(cmdp->param.func);
710 delete_cmd_entry();
711 return (0);
712 }

--- 49 unchanged lines hidden (view full) ---

762 else {
763 /* Finally use brute force */
764 find_command(argv[i], &entry, 0, pathval());
765 }
766
767 switch (entry.cmdtype) {
768 case CMDNORMAL: {
769 if (strchr(argv[i], '/') == NULL) {
706{
707 struct tblentry *cmdp;
708
709 if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
710 unreffunc(cmdp->param.func);
711 delete_cmd_entry();
712 return (0);
713 }

--- 49 unchanged lines hidden (view full) ---

763 else {
764 /* Finally use brute force */
765 find_command(argv[i], &entry, 0, pathval());
766 }
767
768 switch (entry.cmdtype) {
769 case CMDNORMAL: {
770 if (strchr(argv[i], '/') == NULL) {
770 char *path = pathval(), *name;
771 const char *path = pathval();
772 char *name;
771 int j = entry.u.index;
772 do {
773 name = padvance(&path, argv[i]);
774 stunalloc(name);
775 } while (--j >= 0);
776 if (cmd == TYPECMD_SMALLV)
777 out1fmt("%s\n", name);
778 else

--- 56 unchanged lines hidden ---
773 int j = entry.u.index;
774 do {
775 name = padvance(&path, argv[i]);
776 stunalloc(name);
777 } while (--j >= 0);
778 if (cmd == TYPECMD_SMALLV)
779 out1fmt("%s\n", name);
780 else

--- 56 unchanged lines hidden ---