1 /* vi: set sw=4 ts=4: */
3 * Minix shell port for busybox
5 * This version of the Minix shell was adapted for use in busybox
6 * by Erik Andersen <andersen@codepoet.org>
8 * - backtick expansion did not work properly
9 * Jonas Holmberg <jonas.holmberg@axis.com>
10 * Robert Schwebel <r.schwebel@pengutronix.de>
11 * Erik Andersen <andersen@codepoet.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Original copyright notice is retained at the end of this file.
44 #include <sys/times.h>
45 #include <sys/types.h>
52 /* -------- sh.h -------- */
58 #define NPUSH 8 /* limit to input nesting */
61 #define NOFILE 20 /* Number of open files */
62 #define NUFILE 10 /* Number of user-accessible files */
63 #define FDBASE 10 /* First file usable by Shell */
66 * values returned by wait
68 #define WAITSIG(s) ((s)&0177)
69 #define WAITVAL(s) (((s)>>8)&0377)
70 #define WAITCORE(s) (((s)&0200)!=0)
73 * library and system definitions
75 typedef void xint; /* base type of jmp_buf, for not broken compilers */
83 #define NOBLOCK ((struct op *)NULL)
84 #define NOWORD ((char *)NULL)
85 #define NOWORDS ((char **)NULL)
86 #define NOPIPE ((int *)NULL)
89 * Description of a command or an operation on commands.
90 * Might eventually use a union.
93 int type; /* operation type, see below */
94 char **words; /* arguments to a command */
95 struct ioword **ioact; /* IO actions (eg, < > >>) */
98 char *str; /* identifier for case and for */
101 #define TCOM 1 /* command */
102 #define TPAREN 2 /* (c-list) */
103 #define TPIPE 3 /* a | b */
104 #define TLIST 4 /* a [&;] b */
105 #define TOR 5 /* || */
106 #define TAND 6 /* && */
114 #define TPAT 14 /* pattern in case */
115 #define TBRACE 15 /* {c-list} */
116 #define TASYNC 16 /* c & */
119 * actions determining the environment of a process
121 #define BIT(i) (1<<(i))
122 #define FEXEC BIT(0) /* execute without forking */
125 * flags to control evaluation of words
127 #define DOSUB 1 /* interpret $, `, and quotes */
128 #define DOBLANK 2 /* perform blank interpretation */
129 #define DOGLOB 4 /* interpret [?* */
130 #define DOKEY 8 /* move words with `=' to 2nd arg. list */
131 #define DOTRIM 16 /* trim resulting string */
133 #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
139 static int interactive; /* Is this an interactive shell */
141 static int multiline; /* \n changed to ; */
142 static struct op *outtree; /* result from parser */
146 static struct brkcon *brklist;
148 static int newfile(char *s);
149 static char *findeq(char *cp);
150 static char *cclass(char *p, int sub);
151 static void initarea(void);
152 extern int msh_main(int argc, char **argv);
157 struct brkcon *nextlev;
164 short io_unit; /* unit affected */
165 short io_flag; /* action (below) */
166 char *io_name; /* file name */
169 #define IOREAD 1 /* < */
170 #define IOHERE 2 /* << (here file) */
171 #define IOWRITE 4 /* > */
172 #define IOCAT 8 /* >> */
173 #define IOXHERE 16 /* ${}, ` in << */
174 #define IODUP 32 /* >&digit */
175 #define IOCLOSE 64 /* >&- */
177 #define IODEFAULT (-1) /* token for default IO unit */
179 static struct wdblock *wdlist;
180 static struct wdblock *iolist;
183 * parsing & execution environment
197 * -k: look for name=value everywhere on command line
199 * -t: exit after reading and executing one command
202 * -u: unset variables net diagnostic
206 static char *null; /* null value for variable */
207 static int intr; /* interrupt pending */
209 static char *trap[_NSIG + 1];
210 static char ourtrap[_NSIG + 1];
211 static int trapset; /* trap pending */
213 static int heedint; /* heed interrupt signals */
215 static int yynerrs; /* yacc */
217 static char line[LINELIM];
223 static int (*inbuilt(char *s)) (struct op *);
226 static char *rexecve(char *c, char **v, char **envp);
227 static char *space(int n);
228 static char *strsave(char *s, int a);
229 static char *evalstr(char *cp, int f);
230 static char *putn(int n);
231 static char *itoa(int n);
232 static char *unquote(char *as);
233 static struct var *lookup(char *n);
234 static int rlookup(char *n);
235 static struct wdblock *glob(char *cp, struct wdblock *wb);
236 static int my_getc(int ec);
237 static int subgetc(int ec, int quoted);
238 static char **makenv(void);
239 static char **eval(char **ap, int f);
240 static int setstatus(int s);
241 static int waitfor(int lastpid, int canintr);
243 static void onintr(int s); /* SIGINT handler */
245 static int newenv(int f);
246 static void quitenv(void);
247 static void err(char *s);
248 static int anys(char *s1, char *s2);
249 static int any(int c, char *s);
250 static void next(int f);
251 static void setdash(void);
252 static void onecommand(void);
253 static void runtrap(int i);
254 static int gmatch(char *s, char *p);
259 static void leave(void); /* abort shell (or fail in subshell) */
260 static void fail(void); /* fail but return to process next command */
261 static void warn(char *s);
262 static void sig(int i); /* default signal handler */
266 /* -------- area stuff -------- */
268 #define REGSIZE sizeof(struct region)
270 //#define SHRINKBY 64
274 #define ALIGN (sizeof(int)-1)
284 /* -------- grammar stuff -------- */
309 #define YYERRCODE 300
312 #define CONTIN 01 /* skip new lines to complete command */
314 #define SYNTAXERR zzerr()
315 static struct op *pipeline(int cf);
316 static struct op *andor(void);
317 static struct op *c_list(void);
318 static int synio(int cf);
319 static void musthave(int c, int cf);
320 static struct op *simple(void);
321 static struct op *nested(int type, int mark);
322 static struct op *command(int cf);
323 static struct op *dogroup(int onlydone);
324 static struct op *thenpart(void);
325 static struct op *elsepart(void);
326 static struct op *caselist(void);
327 static struct op *casepart(void);
328 static char **pattern(void);
329 static char **wordlist(void);
330 static struct op *list(struct op *t1, struct op *t2);
331 static struct op *block(int type, struct op *t1, struct op *t2, char **wp);
332 static struct op *newtp(void);
333 static struct op *namelist(struct op *t);
334 static char **copyw(void);
335 static void word(char *cp);
336 static struct ioword **copyio(void);
337 static struct ioword *io(int u, int f, char *cp);
338 static void zzerr(void);
339 static void yyerror(char *s);
340 static int yylex(int cf);
341 static int collect(int c, int c1);
342 static int dual(int c);
343 static void diag(int ec);
344 static char *tree(unsigned size);
346 /* -------- var.h -------- */
355 #define COPYV 1 /* flag to setval, suggesting copy */
356 #define RONLY 01 /* variable is read-only */
357 #define EXPORT 02 /* variable is to be exported */
358 #define GETCELL 04 /* name & value space was got with getcell */
360 static struct var *vlist; /* dictionary */
362 static struct var *homedir; /* home directory */
363 static struct var *prompt; /* main prompt */
364 static struct var *cprompt; /* continuation prompt */
365 static struct var *path; /* search path for commands */
366 static struct var *shell; /* shell to interpret command files */
367 static struct var *ifs; /* field separators */
369 static int yyparse(void);
370 static struct var *lookup(char *n);
371 static void setval(struct var *vp, char *val);
372 static void nameval(struct var *vp, char *val, char *name);
373 static void export(struct var *vp);
374 static void ronly(struct var *vp);
375 static int isassign(char *s);
376 static int checkname(char *cp);
377 static int assign(char *s, int cf);
378 static void putvlist(int f, int out);
379 static int eqname(char *n1, char *n2);
381 static int execute(struct op *t, int *pin, int *pout, int act);
383 /* -------- io.h -------- */
386 unsigned id; /* buffer id */
387 char buf[512]; /* buffer */
388 char *bufp; /* pointer into buffer */
389 char *ebufp; /* pointer to end of buffer */
392 /* possible arguments to an IO function */
396 int afile; /* file descriptor */
397 unsigned afid; /* buffer id */
398 long afpos; /* file position */
399 struct iobuf *afbuf; /* buffer for this file */
402 //static struct ioarg ioargstack[NPUSH];
403 #define AFID_NOBUF (~0)
406 /* an input generator's state */
408 int (*iofn) (struct ioarg *, struct io *);
411 char prev; /* previous character read by readc() */
412 char nlcount; /* for `'s */
413 char xchar; /* for `'s */
414 char task; /* reason for pushed IO */
417 //static struct io iostack[NPUSH];
418 #define XOTHER 0 /* none of the below */
419 #define XDOLL 1 /* expanding ${} */
420 #define XGRAVE 2 /* expanding `'s */
421 #define XIO 3 /* file IO */
423 /* in substitution */
424 #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
427 * input generators for IO structure
429 static int nlchar(struct ioarg *ap);
430 static int strchar(struct ioarg *ap);
431 static int qstrchar(struct ioarg *ap);
432 static int filechar(struct ioarg *ap);
433 static int herechar(struct ioarg *ap);
434 static int linechar(struct ioarg *ap);
435 static int gravechar(struct ioarg *ap, struct io *iop);
436 static int qgravechar(struct ioarg *ap, struct io *iop);
437 static int dolchar(struct ioarg *ap);
438 static int wdchar(struct ioarg *ap);
439 static void scraphere(void);
440 static void freehere(int area);
441 static void gethere(void);
442 static void markhere(char *s, struct ioword *iop);
443 static int herein(char *hname, int xdoll);
444 static int run(struct ioarg *argp, int (*f) (struct ioarg *));
449 static int eofc(void);
450 static int readc(void);
451 static void unget(int c);
452 static void ioecho(int c);
453 static void prs(char *s);
454 static void prn(unsigned u);
455 static void closef(int i);
456 static void closeall(void);
461 static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
462 static int remap(int fd);
463 static int openpipe(int *pv);
464 static void closepipe(int *pv);
465 static struct io *setbase(struct io *ip);
467 static struct ioarg temparg; /* temporary for PUSHIO */
469 #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
470 #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
472 /* -------- word.h -------- */
474 #define NSTART 16 /* default number of words to allow for initially */
479 /* bounds are arbitrary */
483 static struct wdblock *addword(char *wd, struct wdblock *wb);
484 static struct wdblock *newword(int nw);
485 static char **getwords(struct wdblock *wb);
487 /* -------- area.h -------- */
492 static char *getcell(unsigned nbytes);
493 static void garbage(void);
494 static void setarea(char *cp, int a);
495 static int getarea(char *cp);
496 static void freearea(int a);
497 static void freecell(char *cp);
498 static int areanum; /* current allocation area */
500 #define NEW(type) (type *)getcell(sizeof(type))
501 #define DELETE(obj) freecell((char *)obj)
504 /* -------- misc stuff -------- */
506 static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp,
508 static int iosetup(struct ioword *iop, int pipein, int pipeout);
509 static void echo(char **wp);
510 static struct op **find1case(struct op *t, char *w);
511 static struct op *findcase(struct op *t, char *w);
512 static void brkset(struct brkcon *bc);
513 static int dolabel(struct op *t);
514 static int dohelp(struct op *t);
515 static int dochdir(struct op *t);
516 static int doshift(struct op *t);
517 static int dologin(struct op *t);
518 static int doumask(struct op *t);
519 static int doexec(struct op *t);
520 static int dodot(struct op *t);
521 static int dowait(struct op *t);
522 static int doread(struct op *t);
523 static int doeval(struct op *t);
524 static int dotrap(struct op *t);
525 static int getsig(char *s);
526 static void setsig(int n, sighandler_t f);
527 static int getn(char *as);
528 static int dobreak(struct op *t);
529 static int docontinue(struct op *t);
530 static int brkcontin(char *cp, int val);
531 static int doexit(struct op *t);
532 static int doexport(struct op *t);
533 static int doreadonly(struct op *t);
534 static void rdexp(char **wp, void (*f) (struct var *), int key);
535 static void badid(char *s);
536 static int doset(struct op *t);
537 static void varput(char *s, int out);
538 static int dotimes(struct op *t);
539 static int expand(char *cp, struct wdblock **wbp, int f);
540 static char *blank(int f);
541 static int dollar(int quoted);
542 static int grave(int quoted);
543 static void globname(char *we, char *pp);
544 static char *generate(char *start1, char *end1, char *middle, char *end);
545 static int anyspcl(struct wdblock *wb);
546 static int xstrcmp(char *p1, char *p2);
547 static void glob0(char *a0, unsigned int a1, int a2,
548 int (*a3) (char *, char *));
549 static void glob1(char *base, char *lim);
550 static void glob2(char *i, char *j);
551 static void glob3(char *i, char *j, char *k);
552 static void readhere(char **name, char *s, int ec);
553 static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
554 static int xxchar(struct ioarg *ap);
559 struct ioword *h_iop;
563 static char *signame[] = {
566 (char *) NULL, /* interrupt */
568 "Illegal instruction",
572 "Floating Point Exception",
577 (char *) NULL, /* broken pipe */
582 #define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
588 static struct res restab[] = {
614 int (*builtinfunc) (struct op * t);
616 static const struct builtincmd builtincmds[] = {
621 {"continue", docontinue},
625 {"export", doexport},
630 {"readonly", doreadonly},
641 extern char **environ; /* environment pointer */
646 static int interactive; /* Is this an interactive shell */
648 static int multiline; /* \n changed to ; */
649 static struct op *outtree; /* result from parser */
652 static struct brkcon *brklist;
654 static struct wdblock *wdlist;
655 static struct wdblock *iolist;
656 static char *trap[_NSIG + 1];
657 static char ourtrap[_NSIG + 1];
658 static int trapset; /* trap pending */
659 static int yynerrs; /* yacc */
660 static char line[LINELIM];
661 static struct var *vlist; /* dictionary */
662 static struct var *homedir; /* home directory */
663 static struct var *prompt; /* main prompt */
664 static struct var *cprompt; /* continuation prompt */
665 static struct var *path; /* search path for commands */
666 static struct var *shell; /* shell to interpret command files */
667 static struct var *ifs; /* field separators */
668 static struct ioarg ioargstack[NPUSH];
669 static struct io iostack[NPUSH];
670 static int areanum; /* current allocation area */
673 static char flags['z' - 'a' + 1];
674 static char *flag = flags - 'a';
675 static char *elinep = line + sizeof(line) - 5;
676 static char *null = "";
677 static int heedint = 1;
678 static struct env e =
679 { line, iostack, iostack - 1, (xint *) NULL, FDBASE,
680 (struct env *) NULL };
681 static void (*qflag) (int) = SIG_IGN;
685 static int iounit = IODEFAULT;
686 static YYSTYPE yylval;
687 static struct iobuf sharedbuf = { AFID_NOBUF };
688 static struct iobuf mainbuf = { AFID_NOBUF };
689 static unsigned bufid = AFID_ID; /* buffer id counter */
690 static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 };
691 static struct here *inhere; /* list of hear docs while parsing */
692 static struct here *acthere; /* list of active here documents */
693 static struct region *areabot; /* bottom of area */
694 static struct region *areatop; /* top of area */
695 static struct region *areanxt; /* starting point of scan */
697 static void *brkaddr;
700 #ifdef CONFIG_FEATURE_COMMAND_EDITING
701 static char *current_prompt;
704 /* -------- sh.c -------- */
710 extern int msh_main(int argc, char **argv)
716 int (*iof) (struct ioarg *);
719 if ((ap = environ) != NULL) {
721 assign(*ap++, !COPYV);
722 for (ap = environ; *ap;)
723 export(lookup(*ap++));
728 shell = lookup("SHELL");
729 if (shell->value == null)
730 setval(shell, DEFAULT_SHELL);
733 homedir = lookup("HOME");
734 if (homedir->value == null)
735 setval(homedir, "/");
738 setval(lookup("$"), putn(getpid()));
740 path = lookup("PATH");
741 if (path->value == null) {
743 setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
745 setval(path, "/bin:/usr/bin");
750 if (ifs->value == null)
751 setval(ifs, " \t\n");
753 prompt = lookup("PS1");
754 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
755 if (prompt->value == null)
757 setval(prompt, "$ ");
758 if (geteuid() == 0) {
759 setval(prompt, "# ");
760 prompt->status &= ~EXPORT;
762 cprompt = lookup("PS2");
763 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
764 if (cprompt->value == null)
766 setval(cprompt, "> ");
772 if (argv[0][0] == '-' && argv[0][1] != '\0') {
773 for (s = argv[0] + 1; *s; s++)
776 prompt->status &= ~EXPORT;
777 cprompt->status &= ~EXPORT;
782 PUSHIO(aword, *++argv, iof = nlchar);
794 prompt->status &= ~EXPORT;
802 if (*s >= 'a' && *s <= 'z')
809 if (iof == filechar && --argc > 0) {
812 prompt->status &= ~EXPORT;
813 cprompt->status &= ~EXPORT;
814 if (newfile(name = *++argv))
819 if (e.iop < iostack) {
820 PUSHIO(afile, 0, iof);
821 if (isatty(0) && isatty(1) && !cflag) {
823 #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
824 printf("\n\n" BB_BANNER " Built-in shell (msh)\n");
825 printf("Enter 'help' for a list of built-in commands.\n\n");
829 signal(SIGQUIT, qflag);
830 if (name && name[0] == '-') {
832 if ((f = open(".profile", 0)) >= 0)
834 if ((f = open("/etc/profile", 0)) >= 0)
838 signal(SIGTERM, sig);
839 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
840 signal(SIGINT, onintr);
845 for (ap = ++argv; --argc > 0;) {
846 if (assign(*ap = *argv++, !COPYV)) {
847 dolc--; /* keyword */
853 setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
856 if (interactive && e.iop <= iostack) {
857 #ifdef CONFIG_FEATURE_COMMAND_EDITING
858 current_prompt = prompt->value;
864 /* Ensure that getenv("PATH") stays current */
865 setenv("PATH", path->value, 1);
869 static void setdash()
873 char m['z' - 'a' + 1];
876 for (c = 'a'; c <= 'z'; c++)
880 setval(lookup("-"), m);
883 static int newfile(s)
888 if (strcmp(s, "-") != 0) {
892 err(": cannot open");
901 static void onecommand()
921 setjmp(failpt = m1); /* Bruce Evans' fix */
922 if (setjmp(failpt = m1) || yyparse() || intr) {
926 if (!interactive && intr)
937 execute(outtree, NOPIPE, NOPIPE, 0);
938 if (!interactive && intr) {
942 if ((i = trapset) != 0) {
988 e.iop = e.iobase = iostack;
994 register struct env *ep;
1000 ep = (struct env *) space(sizeof(*ep));
1012 static void quitenv()
1014 register struct env *ep;
1017 if ((ep = e.oenv) != NULL) {
1020 /* should close `'d files */
1022 while (--fd >= e.iofd)
1028 * Is any character from s1 in s2?
1030 static int anys(s1, s2)
1031 register char *s1, *s2;
1040 * Is character c in s?
1042 static int any(c, s)
1052 static char *putn(n)
1058 static char *itoa(n)
1063 snprintf(s, sizeof(s), "%u", n);
1067 static void next(int f)
1069 PUSHIO(afile, f, filechar);
1072 static void onintr(s)
1073 int s; /* ANSI C requires a parameter */
1075 signal(SIGINT, onintr);
1082 } else if (heedint) {
1088 static char *space(n)
1093 if ((cp = getcell(n)) == 0)
1094 err("out of string space");
1098 static char *strsave(s, a)
1102 register char *cp, *xp;
1104 if ((cp = space(strlen(s) + 1)) != NULL) {
1105 setarea((char *) cp, a);
1106 for (xp = cp; (*xp++ = *s++) != '\0';);
1122 static void runtrap(i)
1127 if ((trapstr = trap[i]) == NULL)
1131 RUN(aword, trapstr, nlchar);
1134 /* -------- var.c -------- */
1137 * Find the given name in the dictionary
1138 * and return its value. If the name was
1139 * not previously there, enter it now and
1140 * return a null value.
1142 static struct var *lookup(n)
1145 register struct var *vp;
1148 static struct var dummy;
1152 for (c = 0; isdigit(*n) && c < 1000; n++)
1153 c = c * 10 + *n - '0';
1154 dummy.status = RONLY;
1155 dummy.value = c <= dolc ? dolv[c] : null;
1158 for (vp = vlist; vp; vp = vp->next)
1159 if (eqname(vp->name, n))
1162 vp = (struct var *) space(sizeof(*vp));
1163 if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) {
1164 dummy.name = dummy.value = "";
1167 for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++);
1171 setarea((char *) vp, 0);
1172 setarea((char *) vp->name, 0);
1175 vp->status = GETCELL;
1181 * give variable at `vp' the value `val'.
1183 static void setval(vp, val)
1187 nameval(vp, val, (char *) NULL);
1191 * if name is not NULL, it must be
1192 * a prefix of the space `val',
1194 * this is all so that exporting
1195 * values is reasonably painless.
1197 static void nameval(vp, val, name)
1198 register struct var *vp;
1201 register char *cp, *xp;
1205 if (vp->status & RONLY) {
1206 for (xp = vp->name; *xp && *xp != '=';)
1207 putc(*xp++, stderr);
1208 err(" is read-only");
1213 xp = space(strlen(vp->name) + strlen(val) + 2);
1216 /* make string: name=value */
1217 setarea((char *) xp, 0);
1219 for (cp = vp->name; (*xp = *cp++) && *xp != '='; xp++);
1223 for (cp = val; (*xp++ = *cp++) != '\0';);
1227 if (vp->status & GETCELL)
1228 freecell(vp->name); /* form new string `name=value' */
1234 static void export(vp)
1237 vp->status |= EXPORT;
1240 static void ronly(vp)
1243 if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
1244 vp->status |= RONLY;
1247 static int isassign(s)
1250 if (!isalpha((int) *s) && *s != '_')
1252 for (; *s != '='; s++)
1253 if (*s == 0 || (!isalnum(*s) && *s != '_'))
1258 static int assign(s, cf)
1265 if (!isalpha(*s) && *s != '_')
1267 for (cp = s; *cp != '='; cp++)
1268 if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
1271 nameval(vp, ++cp, cf == COPYV ? (char *) NULL : s);
1273 vp->status &= ~GETCELL;
1277 static int checkname(cp)
1280 if (!isalpha(*cp++) && *(cp - 1) != '_')
1283 if (!isalnum(*cp++) && *(cp - 1) != '_')
1288 static void putvlist(f, out)
1289 register int f, out;
1291 register struct var *vp;
1293 for (vp = vlist; vp; vp = vp->next)
1294 if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
1295 if (vp->status & EXPORT)
1296 write(out, "export ", 7);
1297 if (vp->status & RONLY)
1298 write(out, "readonly ", 9);
1299 write(out, vp->name, (int) (findeq(vp->name) - vp->name));
1300 write(out, "\n", 1);
1304 static int eqname(n1, n2)
1305 register char *n1, *n2;
1307 for (; *n1 != '=' && *n1 != 0; n1++)
1310 return (*n2 == 0 || *n2 == '=');
1313 static char *findeq(cp)
1316 while (*cp != '\0' && *cp != '=')
1321 /* -------- gmatch.c -------- */
1323 * int gmatch(string, pattern)
1324 * char *string, *pattern;
1326 * Match a pattern as in sh(1).
1331 #define QMASK (CMASK&~QUOTE)
1332 #define NOT '!' /* might use ^ */
1334 static int gmatch(s, p)
1335 register char *s, *p;
1337 register int sc, pc;
1339 if (s == NULL || p == NULL)
1341 while ((pc = *p++ & CMASK) != '\0') {
1345 if ((p = cclass(p, sc)) == NULL)
1357 if (*p == '\0' || gmatch(s, p))
1359 } while (*s++ != '\0');
1363 if (sc != (pc & ~QUOTE))
1370 static char *cclass(p, sub)
1374 register int c, d, not, found;
1376 if ((not = *p == NOT) != 0)
1381 return ((char *) NULL);
1383 if (p[1] == '-' && p[2] != ']') {
1388 if (c == sub || (c <= sub && sub <= d))
1390 } while (*++p != ']');
1391 return (found ? p + 1 : (char *) NULL);
1395 /* -------- area.c -------- */
1398 * All memory between (char *)areabot and (char *)(areatop+1) is
1399 * exclusively administered by the area management routines.
1400 * It is assumed that sbrk() and brk() manipulate the high end.
1403 #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
1405 static void initarea()
1407 brkaddr = malloc(65000);
1408 brktop = brkaddr + 65000;
1410 while ((int) sbrk(0) & ALIGN)
1412 areabot = (struct region *) sbrk(REGSIZE);
1414 areabot->next = areabot;
1415 areabot->area = BUSY;
1420 char *getcell(nbytes)
1423 register int nregio;
1424 register struct region *p, *q;
1431 /* silly and defeats the algorithm */
1433 * round upwards and add administration area
1435 nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1;
1436 for (p = areanxt;;) {
1437 if (p->area > areanum) {
1441 while ((q = p->next)->area > areanum && q != areanxt)
1444 * exit loop if cell big enough
1446 if (q >= p + nregio)
1453 i = nregio >= GROWBY ? nregio : GROWBY;
1454 p = (struct region *) sbrk(i * REGSIZE);
1455 if (p == (struct region *) -1)
1456 return ((char *) NULL);
1460 abort(); /* allocated areas are contiguous */
1470 * we found a FREE area big enough, pointed to by 'p', and up to 'q'
1472 areanxt = p + nregio;
1475 * split into requested area and rest
1477 if (areanxt + 1 > q) {
1479 abort(); /* insufficient space left for admin */
1482 areanxt->area = FREE;
1486 return ((char *) (p + 1));
1489 static void freecell(cp)
1492 register struct region *p;
1494 if ((p = (struct region *) cp) != NULL) {
1502 static void freearea(a)
1505 register struct region *p, *top;
1508 for (p = areabot; p != top; p = p->next)
1513 static void setarea(cp, a)
1517 register struct region *p;
1519 if ((p = (struct region *) cp) != NULL)
1526 return ((struct region *) cp - 1)->area;
1529 static void garbage()
1531 register struct region *p, *q, *top;
1534 for (p = areabot; p != top; p = p->next) {
1535 if (p->area > areanum) {
1536 while ((q = p->next)->area > areanum)
1542 if (areatop >= q + SHRINKBY && q->area > areanum) {
1543 brk((char *) (q + 1));
1551 /* -------- csyn.c -------- */
1553 * shell: syntax (C version)
1564 return (yynerrs != 0);
1567 static struct op *pipeline(cf)
1570 register struct op *t, *p;
1575 while ((c = yylex(0)) == '|') {
1576 if ((p = command(CONTIN)) == NULL)
1578 if (t->type != TPAREN && t->type != TCOM) {
1579 /* shell statement */
1580 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1582 t = block(TPIPE, t, p, NOWORDS);
1589 static struct op *andor()
1591 register struct op *t, *p;
1596 while ((c = yylex(0)) == LOGAND || c == LOGOR) {
1597 if ((p = pipeline(CONTIN)) == NULL)
1599 t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS);
1606 static struct op *c_list()
1608 register struct op *t, *p;
1613 if ((peeksym = yylex(0)) == '&')
1614 t = block(TASYNC, t, NOBLOCK, NOWORDS);
1615 while ((c = yylex(0)) == ';' || c == '&'
1616 || (multiline && c == '\n')) {
1617 if ((p = andor()) == NULL)
1619 if ((peeksym = yylex(0)) == '&')
1620 p = block(TASYNC, p, NOBLOCK, NOWORDS);
1629 static int synio(cf)
1632 register struct ioword *iop;
1636 if ((c = yylex(cf)) != '<' && c != '>') {
1642 iop = io(iounit, i, yylval.cp);
1645 markhere(yylval.cp, iop);
1649 static void musthave(c, cf)
1652 if ((peeksym = yylex(cf)) != c)
1657 static struct op *simple()
1659 register struct op *t;
1663 switch (peeksym = yylex(0)) {
1684 static struct op *nested(type, mark)
1687 register struct op *t;
1693 return (block(type, t, NOBLOCK, NOWORDS));
1696 static struct op *command(cf)
1699 register struct op *t;
1700 struct wdblock *iosave;
1709 switch (c = yylex(cf)) {
1712 if ((t = simple()) == NULL) {
1714 return ((struct op *) NULL);
1721 t = nested(TPAREN, ')');
1725 t = nested(TBRACE, '}');
1735 t->words = wordlist();
1736 if ((c = yylex(0)) != '\n' && c != ';')
1738 t->left = dogroup(0);
1746 t->type = c == WHILE ? TWHILE : TUNTIL;
1748 t->right = dogroup(1);
1760 musthave(IN, CONTIN);
1762 t->left = caselist();
1772 t->right = thenpart();
1783 static struct op *dogroup(onlydone)
1787 register struct op *mylist;
1790 if (c == DONE && onlydone)
1791 return ((struct op *) NULL);
1799 static struct op *thenpart()
1802 register struct op *t;
1804 if ((c = yylex(0)) != THEN) {
1806 return ((struct op *) NULL);
1811 if (t->left == NULL)
1813 t->right = elsepart();
1817 static struct op *elsepart()
1820 register struct op *t;
1822 switch (c = yylex(0)) {
1824 if ((t = c_list()) == NULL)
1832 t->right = thenpart();
1837 return ((struct op *) NULL);
1841 static struct op *caselist()
1843 register struct op *t;
1846 while ((peeksym = yylex(CONTIN)) != ESAC)
1847 t = list(t, casepart());
1851 static struct op *casepart()
1853 register struct op *t;
1857 t->words = pattern();
1860 if ((peeksym = yylex(CONTIN)) != ESAC)
1861 musthave(BREAK, CONTIN);
1865 static char **pattern()
1874 } while ((c = yylex(0)) == '|');
1880 static char **wordlist()
1884 if ((c = yylex(0)) != IN) {
1886 return ((char **) NULL);
1889 while ((c = yylex(0)) == WORD)
1897 * supporting functions
1899 static struct op *list(t1, t2)
1900 register struct op *t1, *t2;
1906 return (block(TLIST, t1, t2, NOWORDS));
1909 static struct op *block(type, t1, t2, wp)
1914 register struct op *t;
1924 static int rlookup(n)
1927 register struct res *rp;
1929 for (rp = restab; rp->r_name; rp++)
1930 if (strcmp(rp->r_name, n) == 0)
1935 static struct op *newtp()
1937 register struct op *t;
1939 t = (struct op *) tree(sizeof(*t));
1949 static struct op *namelist(t)
1950 register struct op *t;
1953 iolist = addword((char *) NULL, iolist);
1954 t->ioact = copyio();
1957 if (t->type != TCOM) {
1958 if (t->type != TPAREN && t->ioact != NULL) {
1959 t = block(TPAREN, t, NOBLOCK, NOWORDS);
1960 t->ioact = t->left->ioact;
1961 t->left->ioact = NULL;
1970 static char **copyw()
1974 wd = getwords(wdlist);
1979 static void word(cp)
1982 wdlist = addword(cp, wdlist);
1985 static struct ioword **copyio()
1987 register struct ioword **iop;
1989 iop = (struct ioword **) getwords(iolist);
1994 static struct ioword *io(u, f, cp)
1999 register struct ioword *iop;
2001 iop = (struct ioword *) tree(sizeof(*iop));
2005 iolist = addword((char *) iop, iolist);
2011 yyerror("syntax error");
2014 static void yyerror(s)
2018 if (interactive && e.iop <= iostack) {
2020 while (eofc() == 0 && yylex(0) != '\n');
2026 static int yylex(cf)
2032 if ((c = peeksym) > 0) {
2045 while ((c = my_getc(0)) == ' ' || c == '\t');
2048 if (any(c, "0123456789")) {
2049 unget(c1 = my_getc(0));
2050 if (c1 == '<' || c1 == '>') {
2060 while ((c = my_getc(0)) != 0 && c != '\n');
2069 if ((c = my_getc(0)) == '{') {
2070 if ((c = collect(c, '}')) != '\0')
2079 if ((c = collect(c, c)) != '\0')
2086 if ((c1 = dual(c)) != '\0') {
2104 if (multiline || cf & CONTIN) {
2105 if (interactive && e.iop <= iostack) {
2106 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2107 current_prompt = cprompt->value;
2109 prs(cprompt->value);
2126 while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n"))
2127 if (e.linep >= elinep)
2128 err("word too long");
2132 if (any(c, "\"'`$"))
2135 if (atstart && (c = rlookup(line)) != 0) {
2139 yylval.cp = strsave(line, areanum);
2143 static int collect(c, c1)
2149 while ((c = my_getc(c1)) != c1) {
2158 if (interactive && c == '\n' && e.iop <= iostack) {
2159 #ifdef CONFIG_FEATURE_COMMAND_EDITING
2160 current_prompt = cprompt->value;
2162 prs(cprompt->value);
2175 register char *cp = s;
2180 if ((c = rlookup(s)) == 0)
2185 static void diag(ec)
2191 if (c == '>' || c == '<') {
2194 yylval.i = ec == '>' ? IOWRITE | IOCAT : IOHERE;
2197 yylval.i = ec == '>' ? IOWRITE : IOREAD;
2198 if (c != '&' || yylval.i == IOHERE)
2204 static char *tree(size)
2209 if ((t = getcell(size)) == NULL) {
2210 prs("command line too complicated\n");
2220 /* -------- exec.c -------- */
2227 static int execute(t, pin, pout, act)
2228 register struct op *t;
2232 register struct op *t1;
2233 volatile int i, rv, a;
2234 char *cp, **wp, **wp2;
2239 /* Avoid longjmp clobbering */
2248 wp = (wp2 = t->words) != NULL
2249 ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
2254 rv = execute(t->left, pin, pout, 0);
2261 rv = forkexec(t, pin, pout, act, wp, &child);
2273 if ((rv = openpipe(pv)) < 0)
2275 pv[0] = remap(pv[0]);
2276 pv[1] = remap(pv[1]);
2277 (void) execute(t->left, pin, pv, 0);
2278 rv = execute(t->right, pv, pout, 0);
2283 (void) execute(t->left, pin, pout, 0);
2284 rv = execute(t->right, pin, pout, 0);
2289 int hinteractive = interactive;
2293 interactive = hinteractive;
2295 setval(lookup("!"), putn(i));
2306 signal(SIGINT, SIG_IGN);
2307 signal(SIGQUIT, SIG_IGN);
2309 signal(SIGTERM, SIG_DFL);
2313 open("/dev/null", 0);
2315 exit(execute(t->left, pin, pout, FEXEC));
2322 rv = execute(t->left, pin, pout, 0);
2323 if ((t1 = t->right) != NULL && (rv == 0) == (t->type == TAND))
2324 rv = execute(t1, pin, pout, 0);
2334 while (*wp++ != NULL);
2336 vp = lookup(t->str);
2337 while (setjmp(bc.brkpt))
2341 for (t1 = t->left; i-- && *wp != NULL;) {
2343 rv = execute(t1, pin, pout, 0);
2345 brklist = brklist->nextlev;
2350 while (setjmp(bc.brkpt))
2355 while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
2356 rv = execute(t->right, pin, pout, 0);
2357 brklist = brklist->nextlev;
2362 if (t->right != NULL) {
2363 rv = !execute(t->left, pin, pout, 0) ?
2364 execute(t->right->left, pin, pout, 0) :
2365 execute(t->right->right, pin, pout, 0);
2370 if ((cp = evalstr(t->str, DOSUB | DOTRIM)) == 0)
2372 if ((t1 = findcase(t->left, cp)) != NULL)
2373 rv = execute(t1, pin, pout, 0);
2378 if (iopp = t->ioact)
2380 if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
2385 if (rv >= 0 && (t1 = t->left))
2386 rv = execute(t1, pin, pout, 0);
2396 if (interactive && intr) {
2400 if ((i = trapset) != 0) {
2408 forkexec(register struct op *t, int *pin, int *pout, int act, char **wp,
2412 int (*shcom) (struct op *) = NULL;
2415 struct ioword **iopp;
2425 struct brkcon *hbrklist;
2429 /* Avoid longjmp clobbering */
2442 rv = -1; /* system-detected error */
2443 if (t->type == TCOM) {
2444 while ((cp = *wp++) != NULL);
2447 /* strip all initial assignments */
2448 /* not correct wrt PATH=yyy command etc */
2450 echo(cp ? wp : owp);
2451 if (cp == NULL && t->ioact == NULL) {
2452 while ((cp = *owp++) != NULL && assign(cp, COPYV));
2453 return (setstatus(0));
2454 } else if (cp != NULL)
2455 shcom = inbuilt(cp);
2459 if (shcom == NULL && (f & FEXEC) == 0) {
2465 hinteractive = interactive;
2472 /* who wrote this crappy non vfork safe shit? */
2477 interactive = hinteractive;
2487 return (pout == NULL ? setstatus(waitfor(i, 0)) : 0);
2491 signal(SIGINT, SIG_IGN);
2492 signal(SIGQUIT, SIG_IGN);
2502 while ((cp = *owp++) != NULL && assign(cp, COPYV))
2506 if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
2507 err("piping to/from shell builtins not yet done");
2519 if ((iopp = t->ioact) != NULL) {
2520 if (shcom != NULL && shcom != doexec) {
2522 err(": cannot redirect shell command");
2526 if (iosetup(*iopp++, pin != NULL, pout != NULL))
2530 return (setstatus((*shcom) (t)));
2531 /* should use FIOCEXCL */
2532 for (i = FDBASE; i < NOFILE; i++)
2535 signal(SIGINT, SIG_DFL);
2536 signal(SIGQUIT, SIG_DFL);
2538 if (t->type == TPAREN)
2539 exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
2543 cp = rexecve(wp[0], wp, makenv());
2555 * 0< 1> are ignored as required
2558 static int iosetup(iop, pipein, pipeout)
2559 register struct ioword *iop;
2560 int pipein, pipeout;
2562 register int u = -1;
2563 char *cp = NULL, *msg;
2565 if (iop->io_unit == IODEFAULT) /* take default */
2566 iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
2567 if (pipein && iop->io_unit == 0)
2569 if (pipeout && iop->io_unit == 1)
2571 msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create";
2572 if ((iop->io_flag & IOHERE) == 0) {
2574 if ((cp = evalstr(cp, DOSUB | DOTRIM)) == NULL)
2577 if (iop->io_flag & IODUP) {
2578 if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
2580 err(": illegal >& argument");
2584 iop->io_flag = IOCLOSE;
2585 iop->io_flag &= ~(IOREAD | IOWRITE);
2587 switch (iop->io_flag) {
2593 case IOHERE | IOXHERE:
2594 u = herein(iop->io_name, iop->io_flag & IOXHERE);
2598 case IOWRITE | IOCAT:
2599 if ((u = open(cp, 1)) >= 0) {
2600 lseek(u, (long) 0, 2);
2604 u = creat(cp, 0666);
2608 u = dup2(*cp - '0', iop->io_unit);
2612 close(iop->io_unit);
2621 if (u != iop->io_unit) {
2622 dup2(u, iop->io_unit);
2629 static void echo(wp)
2635 for (i = 0; wp[i]; i++) {
2643 static struct op **find1case(t, w)
2647 register struct op *t1;
2649 register char **wp, *cp;
2652 return ((struct op **) NULL);
2653 if (t->type == TLIST) {
2654 if ((tp = find1case(t->left, w)) != NULL)
2656 t1 = t->right; /* TPAT */
2659 for (wp = t1->words; *wp;)
2660 if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp))
2662 return ((struct op **) NULL);
2665 static struct op *findcase(t, w)
2669 register struct op **tp;
2671 return ((tp = find1case(t, w)) != NULL ? *tp : (struct op *) NULL);
2675 * Enter a new loop level (marked for break/continue).
2677 static void brkset(bc)
2680 bc->nextlev = brklist;
2685 * Wait for the last process created.
2686 * Print a message for each process found
2687 * that was killed by a signal.
2688 * Ignore interrupt signals while waiting
2689 * unless `canintr' is true.
2691 static int waitfor(lastpid, canintr)
2692 register int lastpid;
2695 register int pid, rv;
2697 int oheedint = heedint;
2704 if (errno != EINTR || canintr)
2707 if ((rv = WAITSIG(s)) != 0) {
2709 if (signame[rv] != NULL) {
2710 if (pid != lastpid) {
2717 if (pid != lastpid) {
2726 prs(" - core dumped");
2727 if (rv >= NSIGNAL || signame[rv])
2733 } while (pid != lastpid);
2748 static int setstatus(s)
2752 setval(lookup("?"), putn(s));
2757 * PATH-searching interface to execve.
2758 * If getenv("PATH") were kept up-to-date,
2759 * execvp might be used.
2761 static char *rexecve(c, v, envp)
2762 char *c, **v, **envp;
2765 register char *sp, *tp;
2766 int eacces = 0, asis = 0;
2768 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2772 if (find_applet_by_name(name)) {
2773 /* We have to exec here since we vforked. Running
2774 * run_applet_by_name() won't work and bad things
2776 execve("/proc/self/exe", v, envp);
2777 execve("busybox", v, envp);
2781 sp = any('/', c) ? "" : path->value;
2783 while (asis || *sp != '\0') {
2786 for (; *sp != '\0'; tp++)
2787 if ((*tp = *sp++) == ':') {
2793 for (i = 0; (*tp++ = c[i++]) != '\0';);
2795 execve(e.linep, v, envp);
2801 execve(DEFAULT_SHELL, v, envp);
2803 return ("no Shell");
2806 return ((char *) bb_msg_memory_exhausted);
2809 return ("argument list too long");
2816 return (errno == ENOENT ? "not found" : "cannot execute");
2820 * Run the command produced by generator `f'
2821 * applied to stream `arg'.
2823 static int run(struct ioarg *argp, int (*f) (struct ioarg *))
2826 struct wdblock *swdlist;
2827 struct wdblock *siolist;
2833 /* Avoid longjmp clobbering */
2843 if (newenv(setjmp(errpt = ev)) == 0) {
2849 if (setjmp(failpt = rt) == 0 && yyparse() == 0)
2850 rv = execute(outtree, NOPIPE, NOPIPE, 0);
2857 freearea(areanum--);
2861 /* -------- do.c -------- */
2864 * built-in commands: doX
2867 static int dohelp(struct op *t)
2870 const struct builtincmd *x;
2872 printf("\nBuilt-in commands:\n");
2873 printf("-------------------\n");
2875 for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) {
2878 col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
2884 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2887 const struct BB_applet *applet;
2888 extern const struct BB_applet applets[];
2889 extern const size_t NUM_APPLETS;
2891 for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) {
2895 col += printf("%s%s", ((col == 0) ? "\t" : " "), applet->name);
2904 return EXIT_SUCCESS;
2909 static int dolabel(struct op *t)
2914 static int dochdir(t)
2915 register struct op *t;
2917 register char *cp, *er;
2919 if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
2920 er = ": no home directory";
2921 else if (chdir(cp) < 0)
2922 er = ": bad directory";
2925 prs(cp != NULL ? cp : "cd");
2930 static int doshift(t)
2931 register struct op *t;
2935 n = t->words[1] ? getn(t->words[1]) : 1;
2937 err("nothing to shift");
2943 setval(lookup("#"), putn(dolc));
2948 * execute login and newgrp directly
2950 static int dologin(t)
2956 signal(SIGINT, SIG_DFL);
2957 signal(SIGQUIT, SIG_DFL);
2959 cp = rexecve(t->words[0], t->words, makenv());
2966 static int doumask(t)
2967 register struct op *t;
2972 if ((cp = t->words[1]) == NULL) {
2975 for (n = 3 * 4; (n -= 3) >= 0;)
2976 putc('0' + ((i >> n) & 07), stderr);
2979 for (n = 0; *cp >= '0' && *cp <= '9'; cp++)
2980 n = n * 8 + (*cp - '0');
2986 static int doexec(t)
2987 register struct op *t;
2994 for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++);
2999 if (setjmp(failpt = ex) == 0)
3000 execute(t, NOPIPE, NOPIPE, FEXEC);
3010 register char *sp, *tp;
3013 if ((cp = t->words[1]) == NULL)
3015 sp = any('/', cp) ? ":" : path->value;
3018 while (*sp && (*tp = *sp++) != ':')
3022 for (i = 0; (*tp++ = cp[i++]) != '\0';);
3023 if ((i = open(e.linep, 0)) >= 0) {
3034 static int dowait(t)
3040 if ((cp = t->words[1]) != NULL) {
3046 setstatus(waitfor(i, 1));
3050 static int doread(t)
3053 register char *cp, **wp;
3054 register int nb = 0;
3055 register int nl = 0;
3057 if (t->words[1] == NULL) {
3058 err("Usage: read name ...");
3061 for (wp = t->words + 1; *wp; wp++) {
3062 for (cp = e.linep; !nl && cp < elinep - 1; cp++)
3063 if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
3064 (nl = (*cp == '\n')) || (wp[1] && any(*cp, ifs->value)))
3069 setval(lookup(*wp), e.linep);
3074 static int doeval(t)
3075 register struct op *t;
3077 return (RUN(awordlist, t->words + 1, wdchar));
3080 static int dotrap(t)
3081 register struct op *t;
3084 register int resetsig;
3086 if (t->words[1] == NULL) {
3087 for (i = 0; i <= _NSIG; i++)
3096 resetsig = isdigit(*t->words[1]);
3097 for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
3098 n = getsig(t->words[i]);
3102 if (*t->words[1] != '\0') {
3103 trap[n] = strsave(t->words[1], 0);
3112 setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL);
3120 static int getsig(s)
3125 if ((n = getn(s)) < 0 || n > _NSIG) {
3126 err("trap: bad signal number");
3132 static void setsig(register int n, sighandler_t f)
3136 if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
3154 for (n = 0; isdigit(*s); s++)
3155 n = (n * 10) + (*s - '0');
3158 err(": bad number");
3163 static int dobreak(t)
3166 return (brkcontin(t->words[1], 1));
3169 static int docontinue(t)
3172 return (brkcontin(t->words[1], 0));
3175 static int brkcontin(cp, val)
3179 register struct brkcon *bc;
3182 nl = cp == NULL ? 1 : getn(cp);
3186 if ((bc = brklist) == NULL)
3188 brklist = bc->nextlev;
3191 err("bad break/continue level");
3195 longjmp(bc->brkpt, 1);
3199 static int doexit(t)
3205 if ((cp = t->words[1]) != NULL)
3206 setstatus(getn(cp));
3212 static int doexport(t)
3215 rdexp(t->words + 1, export, EXPORT);
3219 static int doreadonly(t)
3222 rdexp(t->words + 1, ronly, RONLY);
3226 static void rdexp(char **wp, void (*f) (struct var *), int key)
3229 for (; *wp != NULL; wp++) {
3230 if (isassign(*wp)) {
3234 for (cp = *wp; *cp != '='; cp++);
3246 static void badid(s)
3250 err(": bad identifier");
3254 register struct op *t;
3256 register struct var *vp;
3260 if ((cp = t->words[1]) == NULL) {
3261 for (vp = vlist; vp; vp = vp->next)
3262 varput(vp->name, 1);
3266 /* bad: t->words++; */
3267 for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++);
3269 flag['x'] = flag['v'] = 0;
3279 if (*cp >= 'a' && *cp <= 'z')
3286 t->words[0] = dolv[0];
3287 for (n = 1; t->words[n]; n++)
3288 setarea((char *) t->words[n], 0);
3291 setval(lookup("#"), putn(dolc));
3292 setarea((char *) (dolv - 1), 0);
3297 static void varput(s, out)
3301 if (isalnum(*s) || *s == '_') {
3302 write(out, s, strlen(s));
3303 write(out, "\n", 1);
3309 * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
3310 * This file contains code for the times builtin.
3312 static int dotimes(struct op *t)
3315 long int clk_tck = sysconf(_SC_CLK_TCK);
3318 printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
3319 (int) (buf.tms_utime / clk_tck / 60),
3320 ((double) buf.tms_utime) / clk_tck,
3321 (int) (buf.tms_stime / clk_tck / 60),
3322 ((double) buf.tms_stime) / clk_tck,
3323 (int) (buf.tms_cutime / clk_tck / 60),
3324 ((double) buf.tms_cutime) / clk_tck,
3325 (int) (buf.tms_cstime / clk_tck / 60),
3326 ((double) buf.tms_cstime) / clk_tck);
3331 static int (*inbuilt(char *s)) (struct op *) {
3332 const struct builtincmd *bp;
3334 for (bp = builtincmds; bp->name != NULL; bp++)
3335 if (strcmp(bp->name, s) == 0)
3336 return (bp->builtinfunc);
3341 /* -------- eval.c -------- */
3346 * blank interpretation
3351 static char **eval(char **ap, int f)
3359 /* Avoid longjmp clobbering */
3366 if (newenv(setjmp(errpt = ev)) == 0) {
3367 while (*ap && isassign(*ap))
3368 expand(*ap++, &wb, f & ~DOGLOB);
3370 for (wf = ap; *wf; wf++) {
3372 expand(*wf, &wb, f & ~DOGLOB);
3375 for (wb = addword((char *) 0, wb); *ap; ap++) {
3376 if (!flag['k'] || !isassign(*ap))
3377 expand(*ap, &wb, f & ~DOKEY);
3379 wb = addword((char *) 0, wb);
3384 return (gflg ? (char **) NULL : wp);
3388 * Make the exported environment from the exported
3389 * names in the dictionary. Keyword assignments
3390 * will already have been done.
3392 static char **makenv()
3394 register struct wdblock *wb;
3395 register struct var *vp;
3398 for (vp = vlist; vp; vp = vp->next)
3399 if (vp->status & EXPORT)
3400 wb = addword(vp->name, wb);
3401 wb = addword((char *) 0, wb);
3402 return (getwords(wb));
3405 static char *evalstr(cp, f)
3412 if (expand(cp, &wb, f)) {
3413 if (wb == NULL || wb->w_nword == 0
3414 || (cp = wb->w_words[0]) == NULL)
3422 static int expand(char *cp, register struct wdblock **wbp, int f)
3427 /* Avoid longjmp clobbering */
3433 if (!anys("$`'\"", cp) &&
3434 !anys(ifs->value, cp) && ((f & DOGLOB) == 0 || !anys("[*?", cp))) {
3435 cp = strsave(cp, areanum);
3438 *wbp = addword(cp, *wbp);
3441 if (newenv(setjmp(errpt = ev)) == 0) {
3442 PUSHIO(aword, cp, strchar);
3444 while ((cp = blank(f)) && gflg == 0) {
3446 cp = strsave(cp, areanum);
3447 if ((f & DOGLOB) == 0) {
3450 *wbp = addword(cp, *wbp);
3452 *wbp = glob(cp, *wbp);
3461 * Blank interpretation and quoting
3463 static char *blank(f)
3468 int scanequals, foundequals;
3471 scanequals = f & DOKEY;
3475 switch (c = subgetc('"', foundequals)) {
3483 if (f & DOBLANK && any(c, ifs->value))
3492 for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
3495 if (c == '\'' || !any(c, "$`\""))
3502 if (!isalpha(c) && c != '_')
3505 c = subgetc('"', foundequals);
3507 f & (DOBLANK && any(c, ifs->value)) ||
3508 (!INSUB() && any(c, "\"'"))) {
3519 } else if (!isalnum(c) && c != '_')
3529 * Get characters, substituting for ` and $
3531 static int subgetc(ec, quoted)
3539 if (!INSUB() && ec != '\'') {
3541 if (grave(quoted) == 0)
3543 e.iop->task = XGRAVE;
3546 if (c == '$' && (c = dollar(quoted)) == 0) {
3547 e.iop->task = XDOLL;
3555 * Prepare to generate the string returned by ${} substitution.
3557 static int dollar(quoted)
3563 register char *s, c, *cp = NULL;
3570 if (isalpha(c) || c == '_') {
3571 while ((c = readc()) != 0 && (isalnum(c) || c == '_'))
3572 if (e.linep < elinep)
3579 otask = e.iop->task;
3580 e.iop->task = XOTHER;
3581 while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n')
3582 if (e.linep < elinep)
3585 e.iop->task = otask;
3592 if (e.linep >= elinep) {
3593 err("string in ${} too long");
3599 for (cp = s + 1; *cp; cp++)
3600 if (any(*cp, "=-+?")) {
3605 if (s[1] == 0 && (*s == '*' || *s == '@')) {
3607 /* currently this does not distinguish $* and $@ */
3608 /* should check dollar */
3610 PUSHIO(awordlist, dolv + 1, dolchar);
3612 } else { /* trap the nasty ${=} */
3618 if ((dolp = vp->value) == null) {
3622 err("cannot use ${...=...} with $n");
3631 dolp = strsave(cp, areanum);
3636 prs("missing value for ");
3643 } else if (c == '+')
3644 dolp = strsave(cp, areanum);
3645 if (flag['u'] && dolp == null) {
3646 prs("unset variable: ");
3651 PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
3656 * Run the command in `...` and read its output.
3659 static int grave(quoted)
3666 static char child_cmd[LINELIM];
3672 char *argument_list[4];
3675 /* Avoid longjmp clobbering */
3679 for (cp = e.iop->argp->aword; *cp != '`'; cp++)
3681 err("no closing `");
3685 /* string copy with dollar expansion */
3686 src = e.iop->argp->aword;
3691 while ((*src != '`') && (count < LINELIM)) {
3696 if (*src == '$' && !ignore && !ignore_once) {
3698 char var_name[LINELIM];
3699 char alt_value[LINELIM];
3712 var_name[var_index++] = *src++;
3713 while (isalnum(*src))
3714 var_name[var_index++] = *src++;
3715 var_name[var_index] = 0;
3728 err("unclosed ${\n");
3733 while (*src && (*src != '}')) {
3734 alt_value[alt_index++] = *src++;
3736 alt_value[alt_index] = 0;
3738 err("unclosed ${\n");
3745 vp = lookup(var_name);
3746 if (vp->value != null)
3747 value = (operator == '+') ? alt_value : vp->value;
3748 else if (operator == '?') {
3751 } else if (alt_index && (operator != '+')) {
3753 if (operator == '=')
3758 while (*value && (count < LINELIM)) {
3770 if (openpipe(pf) < 0)
3772 while ((i = vfork()) == -1 && errno == EAGAIN);
3775 err((char *) bb_msg_memory_exhausted);
3779 waitpid(i, NULL, 0);
3780 e.iop->argp->aword = ++cp;
3782 PUSHIO(afile, remap(pf[0]),
3783 (int (*)(struct ioarg *)) ((quoted) ? qgravechar :
3787 /* allow trapped signals */
3788 /* XXX - Maybe this signal stuff should go as well? */
3789 for (j = 0; j <= _NSIG; j++)
3790 if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
3796 argument_list[0] = (char *) DEFAULT_SHELL;
3797 argument_list[1] = "-c";
3798 argument_list[2] = child_cmd;
3799 argument_list[3] = 0;
3801 prs(rexecve(argument_list[0], argument_list, makenv()));
3806 static char *unquote(as)
3811 if ((s = as) != NULL)
3817 /* -------- glob.c -------- */
3823 #define scopy(x) strsave((x), areanum)
3825 #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
3827 static struct wdblock *cl, *nl;
3828 static char spcl[] = "[?*";
3830 static struct wdblock *glob(cp, wb)
3840 for (pp = cp; *pp; pp++)
3843 else if (!any(*pp & ~QUOTE, spcl))
3846 for (cl = addword(scopy(cp), (struct wdblock *) 0); anyspcl(cl);
3848 nl = newword(cl->w_nword * 2);
3849 for (i = 0; i < cl->w_nword; i++) { /* for each argument */
3850 for (pp = cl->w_words[i]; *pp; pp++)
3851 if (any(*pp, spcl)) {
3852 globname(cl->w_words[i], pp);
3856 nl = addword(scopy(cl->w_words[i]), nl);
3858 for (i = 0; i < cl->w_nword; i++)
3859 DELETE(cl->w_words[i]);
3862 for (i = 0; i < cl->w_nword; i++)
3863 unquote(cl->w_words[i]);
3864 glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
3866 for (i = 0; i < cl->w_nword; i++)
3867 wb = addword(cl->w_words[i], wb);
3872 wb = addword(unquote(cp), wb);
3876 static void globname(we, pp)
3880 register char *np, *cp;
3881 char *name, *gp, *dp;
3885 char dname[NAME_MAX + 1];
3888 for (np = we; np != pp; pp--)
3891 for (dp = cp = space((int) (pp - np) + 3); np < pp;)
3895 for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';)
3904 dname[NAME_MAX] = '\0';
3905 while ((de = readdir(dirp)) != NULL) {
3906 /* XXX Hmmm... What this could be? (abial) */
3908 if (ent[j].d_ino == 0)
3911 strncpy(dname, de->d_name, NAME_MAX);
3912 if (dname[0] == '.')
3915 for (k = 0; k < NAME_MAX; k++)
3916 if (any(dname[k], spcl))
3918 if (gmatch(dname, gp)) {
3919 name = generate(we, pp, dname, np);
3920 if (*np && !anys(np, spcl)) {
3921 if (stat(name, &dbuf)) {
3926 nl = addword(name, nl);
3935 * generate a pathname as below.
3936 * start..end1 / middle end
3937 * the slashes come for free
3939 static char *generate(start1, end1, middle, end)
3941 register char *end1;
3945 register char *op, *xp;
3948 space((int) (end1 - start1) + strlen(middle) + strlen(end) + 2);
3949 for (xp = start1; xp != end1;)
3951 for (xp = middle; (*op++ = *xp++) != '\0';);
3953 for (xp = end; (*op++ = *xp++) != '\0';);
3957 static int anyspcl(wb)
3958 register struct wdblock *wb;
3964 for (i = 0; i < wb->w_nword; i++)
3965 if (anys(spcl, *wd++))
3970 static int xstrcmp(p1, p2)
3973 return (strcmp(*(char **) p1, *(char **) p2));
3976 /* -------- word.c -------- */
3978 static struct wdblock *newword(nw)
3981 register struct wdblock *wb;
3983 wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *));
3989 static struct wdblock *addword(wd, wb)
3991 register struct wdblock *wb;
3993 register struct wdblock *wb2;
3997 wb = newword(NSTART);
3998 if ((nw = wb->w_nword) >= wb->w_bsize) {
3999 wb2 = newword(nw * 2);
4000 memcpy((char *) wb2->w_words, (char *) wb->w_words,
4001 nw * sizeof(char *));
4006 wb->w_words[wb->w_nword++] = wd;
4012 register struct wdblock *wb;
4018 return ((char **) NULL);
4019 if (wb->w_nword == 0) {
4021 return ((char **) NULL);
4023 wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
4024 memcpy((char *) wd, (char *) wb->w_words, nb);
4025 DELETE(wb); /* perhaps should done by caller */
4029 int (*func) (char *, char *);
4032 static void glob0(a0, a1, a2, a3)
4036 int (*a3) (char *, char *);
4040 glob1(a0, a0 + a1 * a2);
4043 static void glob1(base, lim)
4046 register char *i, *j;
4056 if ((n = (int) (lim - base)) <= v2)
4058 n = v2 * (n / (2 * v2));
4059 hptr = lptr = base + n;
4064 if ((c = (*func) (i, lptr)) == 0) {
4065 glob2(i, lptr -= v2);
4076 if ((c = (*func) (hptr, j)) == 0) {
4077 glob2(hptr += v2, j);
4082 glob3(i, hptr += v2, j);
4097 if (lptr - base >= lim - hptr) {
4098 glob1(hptr + v2, lim);
4108 glob3(j, lptr -= v2, i);
4113 static void glob2(i, j)
4116 register char *index1, *index2, c;
4124 *index1++ = *index2;
4129 static void glob3(i, j, k)
4132 register char *index1, *index2, *index3;
4142 *index1++ = *index3;
4143 *index3++ = *index2;
4148 /* -------- io.c -------- */
4154 static int my_getc(int ec)
4158 if (e.linep > elinep) {
4159 while ((c = readc()) != '\n' && c);
4160 err("input line too long");
4165 if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
4168 if (c == '\n' && ec != '\"')
4169 return (my_getc(ec));
4176 static void unget(c)
4179 if (e.iop >= e.iobase)
4185 return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
4192 for (; e.iop >= e.iobase; e.iop--)
4193 if ((c = e.iop->peekc) != '\0') {
4197 if (e.iop->prev != 0) {
4198 if ((c = (*e.iop->iofn) (e.iop->argp, e.iop)) != '\0') {
4203 if (e.iop == iostack)
4205 return (e.iop->prev = c);
4206 } else if (e.iop->task == XIO && e.iop->prev != '\n') {
4208 if (e.iop == iostack)
4213 if (e.iop->task == XIO) {
4215 return e.iop->prev = 0;
4216 if (interactive && e.iop == iostack + 1) {
4217 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4218 current_prompt = prompt->value;
4225 if (e.iop >= iostack)
4232 static void ioecho(c)
4236 write(2, &c, sizeof c);
4239 static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
4241 if (++e.iop >= &iostack[NPUSH]) {
4243 err("Shell input nested too deeply");
4247 e.iop->iofn = (int (*)(struct ioarg *, struct io *)) fn;
4249 if (argp->afid != AFID_NOBUF)
4252 e.iop->argp = ioargstack + (e.iop - iostack);
4253 *e.iop->argp = *argp;
4254 e.iop->argp->afbuf = e.iop == &iostack[0] ? &mainbuf : &sharedbuf;
4255 if (isatty(e.iop->argp->afile) == 0 &&
4256 (e.iop == &iostack[0] ||
4257 lseek(e.iop->argp->afile, 0L, 1) != -1)) {
4258 if (++bufid == AFID_NOBUF)
4260 e.iop->argp->afid = bufid;
4264 e.iop->prev = ~'\n';
4268 if (fn == filechar || fn == linechar)
4270 else if (fn == (int (*)(struct ioarg *)) gravechar
4271 || fn == (int (*)(struct ioarg *)) qgravechar)
4272 e.iop->task = XGRAVE;
4274 e.iop->task = XOTHER;
4277 static struct io *setbase(ip)
4280 register struct io *xp;
4288 * Input generating functions
4292 * Produce the characters of a string, then a newline, then EOF.
4294 static int nlchar(ap)
4295 register struct ioarg *ap;
4299 if (ap->aword == NULL)
4301 if ((c = *ap->aword++) == 0) {
4309 * Given a list of words, produce the characters
4310 * in them, with a space after each word.
4312 static int wdchar(ap)
4313 register struct ioarg *ap;
4318 if ((wl = ap->awordlist) == NULL)
4321 if ((c = *(*wl)++) != 0)
4326 ap->awordlist = NULL;
4331 * Return the characters of a list of words,
4332 * producing a space between them.
4334 static int dolchar(ap)
4335 register struct ioarg *ap;
4339 if ((wp = *ap->awordlist++) != NULL) {
4340 PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar);
4346 static int xxchar(ap)
4347 register struct ioarg *ap;
4351 if (ap->aword == NULL)
4353 if ((c = *ap->aword++) == '\0') {
4361 * Produce the characters from a single word (string).
4363 static int strchar(ap)
4364 register struct ioarg *ap;
4368 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4374 * Produce quoted characters from a single word (string).
4376 static int qstrchar(ap)
4377 register struct ioarg *ap;
4381 if (ap->aword == NULL || (c = *ap->aword++) == 0)
4387 * Return the characters from a file.
4389 static int filechar(ap)
4390 register struct ioarg *ap;
4394 struct iobuf *bp = ap->afbuf;
4396 if (ap->afid != AFID_NOBUF) {
4397 if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
4399 lseek(ap->afile, ap->afpos, 0);
4400 i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
4406 bp->ebufp = (bp->bufp = bp->buf) + i;
4409 return *bp->bufp++ & 0177;
4411 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4412 if (interactive && isatty(ap->afile)) {
4413 static char mycommand[BUFSIZ];
4414 static int position = 0, size = 0;
4416 while (size == 0 || position >= size) {
4417 cmdedit_read_input(current_prompt, mycommand);
4418 size = strlen(mycommand);
4421 c = mycommand[position];
4427 i = safe_read(ap->afile, &c, sizeof(c));
4428 return (i == sizeof(c) ? c & 0177 : (closef(ap->afile), 0));
4433 * Return the characters from a here temp file.
4435 static int herechar(ap)
4436 register struct ioarg *ap;
4441 if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
4450 * Return the characters produced by a process (`...`).
4451 * Quote them if required, and remove any trailing newline characters.
4453 static int gravechar(ap, iop)
4459 if ((c = qgravechar(ap, iop) & ~QUOTE) == '\n')
4464 static int qgravechar(ap, iop)
4465 register struct ioarg *ap;
4473 return ('\n' | QUOTE);
4477 } else if ((c = filechar(ap)) == '\n') {
4479 while ((c = filechar(ap)) == '\n')
4487 return (c != 0 ? c | QUOTE : 0);
4491 * Return a single command (usually the first line) from a file.
4493 static int linechar(ap)
4494 register struct ioarg *ap;
4498 if ((c = filechar(ap)) == '\n') {
4501 ap->afile = -1; /* illegal value */
4511 write(2, s, strlen(s));
4520 static void closef(i)
4527 static void closeall()
4531 for (u = NUFILE; u < NOFILE;)
4536 * remap fd into Shell's fd space
4538 static int remap(fd)
4545 for (i = 0; i < NOFILE; i++)
4550 } while (fd >= 0 && fd < e.iofd);
4551 for (i = 0; i < NOFILE; i++)
4555 err("too many files open in shell");
4560 static int openpipe(pv)
4565 if ((i = pipe(pv)) < 0)
4566 err("can't create pipe - try again");
4570 static void closepipe(pv)
4579 /* -------- here.c -------- */
4585 static void markhere(s, iop)
4589 register struct here *h, *lh;
4591 h = (struct here *) space(sizeof(struct here));
4594 h->h_tag = evalstr(s, DOSUB);
4603 for (lh = inhere; lh != NULL; lh = lh->h_next)
4604 if (lh->h_next == 0) {
4608 iop->io_flag |= IOHERE | IOXHERE;
4609 for (s = h->h_tag; *s; s++)
4611 iop->io_flag &= ~IOXHERE;
4614 h->h_dosub = iop->io_flag & IOXHERE;
4617 static void gethere()
4619 register struct here *h, *hp;
4621 /* Scan here files first leaving inhere list in place */
4622 for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
4623 readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub ? 0 : '\'');
4625 /* Make inhere list active - keep list intact for scraphere */
4627 hp->h_next = acthere;
4633 static void readhere(name, s, ec)
4639 char tname[30] = ".msh_XXXXXX";
4642 char myline[LINELIM + 1];
4645 tf = mkstemp(tname);
4648 *name = strsave(tname, areanum);
4649 if (newenv(setjmp(errpt = ev)) != 0)
4652 pushio(e.iop->argp, (int (*)(struct ioarg *)) e.iop->iofn);
4655 if (interactive && e.iop <= iostack) {
4656 #ifdef CONFIG_FEATURE_COMMAND_EDITING
4657 current_prompt = cprompt->value;
4659 prs(cprompt->value);
4663 while ((c = my_getc(ec)) != '\n' && c) {
4666 if (thenext >= &myline[LINELIM]) {
4673 if (strcmp(s, myline) == 0 || c == 0)
4676 write(tf, myline, (int) (thenext - myline));
4679 prs("here document `");
4689 * open here temp file.
4690 * if unquoted here, expand here temp file into second temp file.
4692 static int herein(hname, xdoll)
4700 /* Avoid longjmp clobbering */
4705 hf = open(hname, 0);
4710 char tname[30] = ".msh_XXXXXX";
4713 tf = mkstemp(tname);
4716 if (newenv(setjmp(errpt = ev)) == 0) {
4717 PUSHIO(afile, hf, herechar);
4719 while ((c = subgetc(0, 0)) != 0) {
4721 write(tf, &c, sizeof c);
4727 tf = open(tname, 0);
4734 static void scraphere()
4736 register struct here *h;
4738 for (h = inhere; h != NULL; h = h->h_next) {
4739 if (h->h_iop && h->h_iop->io_name)
4740 unlink(h->h_iop->io_name);
4745 /* unlink here temp files before a freearea(area) */
4746 static void freehere(area)
4749 register struct here *h, *hl;
4752 for (h = acthere; h != NULL; h = h->h_next)
4753 if (getarea((char *) h) >= area) {
4754 if (h->h_iop->io_name != NULL)
4755 unlink(h->h_iop->io_name);
4757 acthere = h->h_next;
4759 hl->h_next = h->h_next;
4767 * Copyright (c) 1987,1997, Prentice Hall
4768 * All rights reserved.
4770 * Redistribution and use of the MINIX operating system in source and
4771 * binary forms, with or without modification, are permitted provided
4772 * that the following conditions are met:
4774 * Redistributions of source code must retain the above copyright
4775 * notice, this list of conditions and the following disclaimer.
4777 * Redistributions in binary form must reproduce the above
4778 * copyright notice, this list of conditions and the following
4779 * disclaimer in the documentation and/or other materials provided
4780 * with the distribution.
4782 * Neither the name of Prentice Hall nor the names of the software
4783 * authors or contributors may be used to endorse or promote
4784 * products derived from this software without specific prior
4785 * written permission.
4787 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
4788 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
4789 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4790 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4791 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
4792 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4793 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4794 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4795 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4796 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
4797 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
4798 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.