Merge branch 'vendor/OPENSSL'
[dragonfly.git] / bin / sh / exec.c
CommitLineData
984263bc
MD
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
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.
dc71b7ab 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
1de703da
MD
31 *
32 * @(#)exec.c 8.4 (Berkeley) 6/8/95
b1abb130 33 * $FreeBSD: head/bin/sh/exec.c 253650 2013-07-25 15:08:41Z jilles $
984263bc
MD
34 */
35
984263bc
MD
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <errno.h>
99512ac4 41#include <paths.h>
984263bc
MD
42#include <stdlib.h>
43
44/*
45 * When commands are first encountered, they are entered in a hash table.
46 * This ensures that a full path search will not have to be done for them
47 * on each invocation.
48 *
49 * We should investigate converting to a linear search, even though that
50 * would make the command name "hash" a misnomer.
51 */
52
53#include "shell.h"
54#include "main.h"
55#include "nodes.h"
56#include "parser.h"
57#include "redir.h"
58#include "eval.h"
59#include "exec.h"
60#include "builtins.h"
61#include "var.h"
62#include "options.h"
63#include "input.h"
64#include "output.h"
65#include "syntax.h"
66#include "memalloc.h"
67#include "error.h"
984263bc
MD
68#include "mystring.h"
69#include "show.h"
70#include "jobs.h"
71#include "alias.h"
72
73
74#define CMDTABLESIZE 31 /* should be prime */
984263bc
MD
75
76
77
78struct tblentry {
79 struct tblentry *next; /* next entry in hash chain */
80 union param param; /* definition of builtin function */
0d5aaed6 81 int special; /* flag for special builtin commands */
f00eae14
PA
82 signed char cmdtype; /* index identifying command */
83 char cmdname[]; /* name of command */
984263bc
MD
84};
85
86
99512ac4 87static struct tblentry *cmdtable[CMDTABLESIZE];
f00eae14 88static int cmdtable_cd = 0; /* cmdtable contains cd-dependent entries */
984263bc
MD
89int exerrno = 0; /* Last exec error */
90
91
99512ac4
PA
92static void tryexec(char *, char **, char **);
93static void printentry(struct tblentry *, int);
94static struct tblentry *cmdlookup(const char *, int);
95static void delete_cmd_entry(void);
b6bd99f6 96static void addcmdentry(const char *, struct cmdentry *);
984263bc
MD
97
98/*
99 * Exec a program. Never returns. If you change this routine, you may
100 * have to change the find_command routine as well.
99512ac4
PA
101 *
102 * The argv array may be changed and element argv[-1] should be writable.
984263bc
MD
103 */
104
105void
934863d7 106shellexec(char **argv, char **envp, const char *path, int idx)
984263bc
MD
107{
108 char *cmdname;
109 int e;
110
111 if (strchr(argv[0], '/') != NULL) {
112 tryexec(argv[0], argv, envp);
113 e = errno;
114 } else {
115 e = ENOENT;
116 while ((cmdname = padvance(&path, argv[0])) != NULL) {
934863d7 117 if (--idx < 0 && pathopt == NULL) {
984263bc
MD
118 tryexec(cmdname, argv, envp);
119 if (errno != ENOENT && errno != ENOTDIR)
120 e = errno;
99512ac4
PA
121 if (e == ENOEXEC)
122 break;
984263bc
MD
123 }
124 stunalloc(cmdname);
125 }
126 }
127
128 /* Map to POSIX errors */
99512ac4 129 if (e == ENOENT || e == ENOTDIR) {
984263bc 130 exerrno = 127;
010f7689 131 exerror(EXEXEC, "%s: not found", argv[0]);
99512ac4
PA
132 } else {
133 exerrno = 126;
134 exerror(EXEXEC, "%s: %s", argv[0], strerror(e));
135 }
984263bc
MD
136}
137
138
99512ac4 139static void
984263bc
MD
140tryexec(char *cmd, char **argv, char **envp)
141{
99512ac4
PA
142 int e, in;
143 ssize_t n;
144 char buf[256];
984263bc
MD
145
146 execve(cmd, argv, envp);
147 e = errno;
148 if (e == ENOEXEC) {
99512ac4
PA
149 INTOFF;
150 in = open(cmd, O_RDONLY | O_NONBLOCK);
151 if (in != -1) {
152 n = pread(in, buf, sizeof buf, 0);
153 close(in);
154 if (n > 0 && memchr(buf, '\0', n) != NULL) {
155 errno = ENOEXEC;
156 return;
157 }
158 }
159 *argv = cmd;
160 *--argv = __DECONST(char *, _PATH_BSHELL);
161 execve(_PATH_BSHELL, argv, envp);
984263bc
MD
162 }
163 errno = e;
164}
165
166/*
167 * Do a path search. The variable path (passed by reference) should be
168 * set to the start of the path before the first call; padvance will update
169 * this value as it proceeds. Successive calls to padvance will return
170 * the possible path expansions in sequence. If an option (indicated by
171 * a percent sign) appears in the path entry then the global variable
172 * pathopt will be set to point to it; otherwise pathopt will be set to
173 * NULL.
174 */
175
492efe05 176const char *pathopt;
984263bc
MD
177
178char *
492efe05 179padvance(const char **path, const char *name)
984263bc 180{
99512ac4 181 const char *p, *start;
492efe05 182 char *q;
b1abb130 183 size_t len;
984263bc
MD
184
185 if (*path == NULL)
186 return NULL;
187 start = *path;
99512ac4
PA
188 for (p = start; *p && *p != ':' && *p != '%'; p++)
189 ; /* nothing */
984263bc 190 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */
99512ac4
PA
191 STARTSTACKSTR(q);
192 CHECKSTRSPACE(len, q);
984263bc
MD
193 if (p != start) {
194 memcpy(q, start, p - start);
195 q += p - start;
196 *q++ = '/';
197 }
198 strcpy(q, name);
199 pathopt = NULL;
200 if (*p == '%') {
201 pathopt = ++p;
202 while (*p && *p != ':') p++;
203 }
204 if (*p == ':')
205 *path = p + 1;
206 else
207 *path = NULL;
208 return stalloc(len);
209}
210
211
212
213/*** Command hashing code ***/
214
215
216int
217hashcmd(int argc __unused, char **argv __unused)
218{
219 struct tblentry **pp;
220 struct tblentry *cmdp;
221 int c;
222 int verbose;
223 struct cmdentry entry;
224 char *name;
8bb2400d 225 int errors;
984263bc 226
8bb2400d 227 errors = 0;
984263bc
MD
228 verbose = 0;
229 while ((c = nextopt("rv")) != '\0') {
230 if (c == 'r') {
99512ac4 231 clearcmdentry();
984263bc
MD
232 } else if (c == 'v') {
233 verbose++;
234 }
235 }
236 if (*argptr == NULL) {
237 for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
238 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
239 if (cmdp->cmdtype == CMDNORMAL)
240 printentry(cmdp, verbose);
241 }
242 }
243 return 0;
244 }
245 while ((name = *argptr) != NULL) {
246 if ((cmdp = cmdlookup(name, 0)) != NULL
99512ac4 247 && cmdp->cmdtype == CMDNORMAL)
984263bc 248 delete_cmd_entry();
99512ac4 249 find_command(name, &entry, DO_ERR, pathval());
8bb2400d
PA
250 if (entry.cmdtype == CMDUNKNOWN)
251 errors = 1;
252 else if (verbose) {
253 cmdp = cmdlookup(name, 0);
254 if (cmdp != NULL)
255 printentry(cmdp, verbose);
256 else {
257 outfmt(out2, "%s: not found\n", name);
258 errors = 1;
984263bc
MD
259 }
260 flushall();
261 }
262 argptr++;
263 }
8bb2400d 264 return errors;
984263bc
MD
265}
266
267
99512ac4 268static void
984263bc
MD
269printentry(struct tblentry *cmdp, int verbose)
270{
934863d7 271 int idx;
492efe05 272 const char *path;
984263bc
MD
273 char *name;
274
275 if (cmdp->cmdtype == CMDNORMAL) {
934863d7 276 idx = cmdp->param.index;
984263bc
MD
277 path = pathval();
278 do {
279 name = padvance(&path, cmdp->cmdname);
280 stunalloc(name);
934863d7 281 } while (--idx >= 0);
984263bc
MD
282 out1str(name);
283 } else if (cmdp->cmdtype == CMDBUILTIN) {
284 out1fmt("builtin %s", cmdp->cmdname);
285 } else if (cmdp->cmdtype == CMDFUNCTION) {
286 out1fmt("function %s", cmdp->cmdname);
287 if (verbose) {
288 INTOFF;
99512ac4 289 name = commandtext(getfuncnode(cmdp->param.func));
984263bc
MD
290 out1c(' ');
291 out1str(name);
292 ckfree(name);
293 INTON;
294 }
295#ifdef DEBUG
296 } else {
297 error("internal error: cmdtype %d", cmdp->cmdtype);
298#endif
299 }
984263bc
MD
300 out1c('\n');
301}
302
303
304
305/*
306 * Resolve a command name. If you change this routine, you may have to
307 * change the shellexec routine as well.
308 */
309
310void
99512ac4
PA
311find_command(const char *name, struct cmdentry *entry, int act,
312 const char *path)
984263bc 313{
99512ac4 314 struct tblentry *cmdp, loc_cmd;
934863d7 315 int idx;
984263bc
MD
316 char *fullname;
317 struct stat statb;
318 int e;
319 int i;
0d5aaed6 320 int spec;
f00eae14 321 int cd;
984263bc
MD
322
323 /* If name contains a slash, don't use the hash table */
324 if (strchr(name, '/') != NULL) {
325 entry->cmdtype = CMDNORMAL;
326 entry->u.index = 0;
327 return;
328 }
329
f00eae14
PA
330 cd = 0;
331
984263bc 332 /* If name is in the table, and not invalidated by cd, we're done */
f00eae14 333 if ((cmdp = cmdlookup(name, 0)) != NULL) {
99512ac4
PA
334 if (cmdp->cmdtype == CMDFUNCTION && act & DO_NOFUNC)
335 cmdp = NULL;
336 else
337 goto success;
338 }
984263bc 339
99512ac4
PA
340 /* Check for builtin next */
341 if ((i = find_builtin(name, &spec)) >= 0) {
984263bc
MD
342 INTOFF;
343 cmdp = cmdlookup(name, 1);
99512ac4
PA
344 if (cmdp->cmdtype == CMDFUNCTION)
345 cmdp = &loc_cmd;
984263bc
MD
346 cmdp->cmdtype = CMDBUILTIN;
347 cmdp->param.index = i;
0d5aaed6 348 cmdp->special = spec;
984263bc
MD
349 INTON;
350 goto success;
351 }
352
353 /* We have to search path. */
984263bc
MD
354
355 e = ENOENT;
934863d7 356 idx = -1;
984263bc
MD
357loop:
358 while ((fullname = padvance(&path, name)) != NULL) {
359 stunalloc(fullname);
934863d7 360 idx++;
984263bc 361 if (pathopt) {
99512ac4 362 if (prefix("func", pathopt)) {
984263bc
MD
363 /* handled below */
364 } else {
365 goto loop; /* ignore unimplemented options */
366 }
367 }
f00eae14
PA
368 if (fullname[0] != '/')
369 cd = 1;
984263bc
MD
370 if (stat(fullname, &statb) < 0) {
371 if (errno != ENOENT && errno != ENOTDIR)
372 e = errno;
373 goto loop;
374 }
375 e = EACCES; /* if we fail, this will be the error */
376 if (!S_ISREG(statb.st_mode))
377 goto loop;
378 if (pathopt) { /* this is a %func directory */
379 stalloc(strlen(fullname) + 1);
380 readcmdfile(fullname);
381 if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
382 error("%s not defined in %s", name, fullname);
383 stunalloc(fullname);
384 goto success;
385 }
386#ifdef notdef
387 if (statb.st_uid == geteuid()) {
388 if ((statb.st_mode & 0100) == 0)
389 goto loop;
390 } else if (statb.st_gid == getegid()) {
391 if ((statb.st_mode & 010) == 0)
392 goto loop;
393 } else {
394 if ((statb.st_mode & 01) == 0)
395 goto loop;
396 }
397#endif
398 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
399 INTOFF;
400 cmdp = cmdlookup(name, 1);
99512ac4
PA
401 if (cmdp->cmdtype == CMDFUNCTION)
402 cmdp = &loc_cmd;
984263bc 403 cmdp->cmdtype = CMDNORMAL;
934863d7 404 cmdp->param.index = idx;
984263bc
MD
405 INTON;
406 goto success;
407 }
408
99512ac4 409 if (act & DO_ERR) {
010f7689
PA
410 if (e == ENOENT || e == ENOTDIR)
411 outfmt(out2, "%s: not found\n", name);
412 else
413 outfmt(out2, "%s: %s\n", name, strerror(e));
414 }
984263bc 415 entry->cmdtype = CMDUNKNOWN;
99512ac4 416 entry->u.index = 0;
984263bc
MD
417 return;
418
419success:
4b1ea71d 420 if (cmdp) {
f00eae14
PA
421 if (cd)
422 cmdtable_cd = 1;
4b1ea71d
SS
423 entry->cmdtype = cmdp->cmdtype;
424 entry->u = cmdp->param;
0d5aaed6 425 entry->special = cmdp->special;
4b1ea71d
SS
426 } else
427 entry->cmdtype = CMDUNKNOWN;
984263bc
MD
428}
429
430
431
432/*
433 * Search the table of builtin commands.
434 */
435
436int
99512ac4 437find_builtin(const char *name, int *special)
984263bc
MD
438{
439 const struct builtincmd *bp;
440
441 for (bp = builtincmd ; bp->name ; bp++) {
0d5aaed6
PA
442 if (*bp->name == *name && equal(bp->name, name)) {
443 *special = bp->special;
984263bc 444 return bp->code;
0d5aaed6 445 }
984263bc
MD
446 }
447 return -1;
448}
449
450
451
452/*
f00eae14
PA
453 * Called when a cd is done. If any entry in cmdtable depends on the current
454 * directory, simply clear cmdtable completely.
984263bc
MD
455 */
456
457void
458hashcd(void)
459{
f00eae14
PA
460 if (cmdtable_cd)
461 clearcmdentry();
984263bc
MD
462}
463
464
465
466/*
467 * Called before PATH is changed. The argument is the new value of PATH;
468 * pathval() still returns the old value at this point. Called with
469 * interrupts off.
470 */
471
472void
99512ac4 473changepath(const char *newval __unused)
984263bc 474{
99512ac4 475 clearcmdentry();
984263bc
MD
476}
477
478
479/*
480 * Clear out command entries. The argument specifies the first entry in
481 * PATH which has changed.
482 */
483
484void
99512ac4 485clearcmdentry(void)
984263bc
MD
486{
487 struct tblentry **tblp;
488 struct tblentry **pp;
489 struct tblentry *cmdp;
490
491 INTOFF;
492 for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
493 pp = tblp;
494 while ((cmdp = *pp) != NULL) {
99512ac4 495 if (cmdp->cmdtype == CMDNORMAL) {
984263bc 496 *pp = cmdp->next;
984263bc
MD
497 ckfree(cmdp);
498 } else {
499 pp = &cmdp->next;
500 }
501 }
502 }
f00eae14 503 cmdtable_cd = 0;
984263bc
MD
504 INTON;
505}
506
507
984263bc
MD
508/*
509 * Locate a command in the command hash table. If "add" is nonzero,
510 * add the command to the table if it is not already present. The
511 * variable "lastcmdentry" is set to point to the address of the link
512 * pointing to the entry, so that delete_cmd_entry can delete the
513 * entry.
514 */
515
99512ac4 516static struct tblentry **lastcmdentry;
984263bc
MD
517
518
99512ac4
PA
519static struct tblentry *
520cmdlookup(const char *name, int add)
984263bc
MD
521{
522 int hashval;
99512ac4 523 const char *p;
984263bc
MD
524 struct tblentry *cmdp;
525 struct tblentry **pp;
526
527 p = name;
528 hashval = *p << 4;
529 while (*p)
530 hashval += *p++;
531 hashval &= 0x7FFF;
532 pp = &cmdtable[hashval % CMDTABLESIZE];
533 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
534 if (equal(cmdp->cmdname, name))
535 break;
536 pp = &cmdp->next;
537 }
538 if (add && cmdp == NULL) {
539 INTOFF;
f00eae14 540 cmdp = *pp = ckmalloc(sizeof (struct tblentry)
984263bc
MD
541 + strlen(name) + 1);
542 cmdp->next = NULL;
543 cmdp->cmdtype = CMDUNKNOWN;
984263bc
MD
544 strcpy(cmdp->cmdname, name);
545 INTON;
546 }
547 lastcmdentry = pp;
548 return cmdp;
549}
550
551/*
552 * Delete the command entry returned on the last lookup.
553 */
554
99512ac4 555static void
984263bc
MD
556delete_cmd_entry(void)
557{
558 struct tblentry *cmdp;
559
560 INTOFF;
561 cmdp = *lastcmdentry;
562 *lastcmdentry = cmdp->next;
563 ckfree(cmdp);
564 INTON;
565}
566
567
568
569/*
570 * Add a new command entry, replacing any existing command entry for
571 * the same name.
572 */
573
b6bd99f6 574static void
99512ac4 575addcmdentry(const char *name, struct cmdentry *entry)
984263bc
MD
576{
577 struct tblentry *cmdp;
578
579 INTOFF;
580 cmdp = cmdlookup(name, 1);
581 if (cmdp->cmdtype == CMDFUNCTION) {
99512ac4 582 unreffunc(cmdp->param.func);
984263bc
MD
583 }
584 cmdp->cmdtype = entry->cmdtype;
585 cmdp->param = entry->u;
586 INTON;
587}
588
589
590/*
591 * Define a shell function.
592 */
593
594void
99512ac4 595defun(const char *name, union node *func)
984263bc
MD
596{
597 struct cmdentry entry;
598
599 INTOFF;
600 entry.cmdtype = CMDFUNCTION;
601 entry.u.func = copyfunc(func);
602 addcmdentry(name, &entry);
603 INTON;
604}
605
606
607/*
608 * Delete a function if it exists.
609 */
610
611int
99512ac4 612unsetfunc(const char *name)
984263bc
MD
613{
614 struct tblentry *cmdp;
615
616 if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
99512ac4 617 unreffunc(cmdp->param.func);
984263bc 618 delete_cmd_entry();
984263bc 619 }
31b6347c 620 return (0);
984263bc
MD
621}
622
f00eae14
PA
623
624/*
625 * Check if a function by a certain name exists.
626 */
627int
628isfunc(const char *name)
629{
630 struct tblentry *cmdp;
631 cmdp = cmdlookup(name, 0);
632 return (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION);
633}
634
635
984263bc 636/*
0d5aaed6
PA
637 * Shared code for the following builtin commands:
638 * type, command -v, command -V
984263bc
MD
639 */
640
641int
99512ac4 642typecmd_impl(int argc, char **argv, int cmd, const char *path)
984263bc
MD
643{
644 struct cmdentry entry;
645 struct tblentry *cmdp;
492efe05 646 const char * const *pp;
984263bc
MD
647 struct alias *ap;
648 int i;
934863d7 649 int err = 0;
984263bc 650
99512ac4
PA
651 if (path != pathval())
652 clearcmdentry();
0d5aaed6 653
99512ac4 654 for (i = 1; i < argc; i++) {
984263bc 655 /* First look at the keywords */
492efe05 656 for (pp = parsekwd; *pp; pp++)
984263bc
MD
657 if (**pp == *argv[i] && equal(*pp, argv[i]))
658 break;
659
660 if (*pp) {
0d5aaed6
PA
661 if (cmd == TYPECMD_SMALLV)
662 out1fmt("%s\n", argv[i]);
663 else
99512ac4 664 out1fmt("%s is a shell keyword\n", argv[i]);
984263bc
MD
665 continue;
666 }
667
668 /* Then look at the aliases */
669 if ((ap = lookupalias(argv[i], 1)) != NULL) {
0d5aaed6
PA
670 if (cmd == TYPECMD_SMALLV)
671 out1fmt("alias %s='%s'\n", argv[i], ap->val);
672 else
99512ac4
PA
673 out1fmt("%s is an alias for %s\n", argv[i],
674 ap->val);
984263bc
MD
675 continue;
676 }
677
678 /* Then check if it is a tracked alias */
679 if ((cmdp = cmdlookup(argv[i], 0)) != NULL) {
680 entry.cmdtype = cmdp->cmdtype;
681 entry.u = cmdp->param;
99512ac4 682 entry.special = cmdp->special;
984263bc
MD
683 }
684 else {
685 /* Finally use brute force */
99512ac4 686 find_command(argv[i], &entry, 0, path);
984263bc
MD
687 }
688
689 switch (entry.cmdtype) {
690 case CMDNORMAL: {
691 if (strchr(argv[i], '/') == NULL) {
99512ac4 692 const char *path2 = path;
492efe05 693 char *name;
984263bc
MD
694 int j = entry.u.index;
695 do {
99512ac4 696 name = padvance(&path2, argv[i]);
984263bc
MD
697 stunalloc(name);
698 } while (--j >= 0);
0d5aaed6
PA
699 if (cmd == TYPECMD_SMALLV)
700 out1fmt("%s\n", name);
701 else
99512ac4 702 out1fmt("%s is%s %s\n", argv[i],
0d5aaed6
PA
703 (cmdp && cmd == TYPECMD_TYPE) ?
704 " a tracked alias for" : "",
705 name);
984263bc 706 } else {
b25bde5a 707 if (access(argv[i], X_OK) == 0) {
0d5aaed6
PA
708 if (cmd == TYPECMD_SMALLV)
709 out1fmt("%s\n", argv[i]);
710 else
99512ac4
PA
711 out1fmt("%s is %s\n", argv[i],
712 argv[i]);
ee44a32d
PA
713 } else {
714 if (cmd != TYPECMD_SMALLV)
99512ac4
PA
715 outfmt(out2, "%s: %s\n",
716 argv[i], strerror(errno));
934863d7 717 err |= 127;
b25bde5a 718 }
984263bc
MD
719 }
720 break;
721 }
722 case CMDFUNCTION:
0d5aaed6
PA
723 if (cmd == TYPECMD_SMALLV)
724 out1fmt("%s\n", argv[i]);
725 else
99512ac4 726 out1fmt("%s is a shell function\n", argv[i]);
984263bc
MD
727 break;
728
729 case CMDBUILTIN:
0d5aaed6
PA
730 if (cmd == TYPECMD_SMALLV)
731 out1fmt("%s\n", argv[i]);
99512ac4
PA
732 else if (entry.special)
733 out1fmt("%s is a special shell builtin\n",
734 argv[i]);
0d5aaed6 735 else
99512ac4 736 out1fmt("%s is a shell builtin\n", argv[i]);
984263bc
MD
737 break;
738
739 default:
0d5aaed6 740 if (cmd != TYPECMD_SMALLV)
99512ac4 741 outfmt(out2, "%s: not found\n", argv[i]);
934863d7 742 err |= 127;
984263bc
MD
743 break;
744 }
745 }
99512ac4
PA
746
747 if (path != pathval())
748 clearcmdentry();
749
934863d7 750 return err;
984263bc 751}
0d5aaed6
PA
752
753/*
754 * Locate and print what a word is...
755 */
756
757int
758typecmd(int argc, char **argv)
759{
99512ac4 760 return typecmd_impl(argc, argv, TYPECMD_TYPE, bltinlookup("PATH", 1));
0d5aaed6 761}