c_sh.c
1/* $OpenBSD: c_sh.c,v 1.65 2023/09/14 18:32:03 cheloha Exp $ */
2
3/*
4 * built-in Bourne commands
5 */
6
7#include <sys/resource.h>
8#include <sys/stat.h>
9#include <sys/time.h>
10
11#include <ctype.h>
12#include <errno.h>
13#include <fcntl.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <unistd.h>
18
19#include "sh.h"
20
21static void p_tv(struct shf *, int, struct timeval *, int, char *, char *);
22static void p_ts(struct shf *, int, struct timespec *, int, char *, char *);
23
24/* :, false and true */
25int
26c_label(char **wp)
27{
28 return wp[0][0] == 'f' ? 1 : 0;
29}
30
31int
32c_shift(char **wp)
33{
34 struct block *l = genv->loc;
35 int n;
36 int64_t val;
37 char *arg;
38
39 if (ksh_getopt(wp, &builtin_opt, null) == '?')
40 return 1;
41 arg = wp[builtin_opt.optind];
42
43 if (arg) {
44 evaluate(arg, &val, KSH_UNWIND_ERROR, false);
45 n = val;
46 } else
47 n = 1;
48 if (n < 0) {
49 bi_errorf("%s: bad number", arg);
50 return (1);
51 }
52 if (l->argc < n) {
53 bi_errorf("nothing to shift");
54 return (1);
55 }
56 l->argv[n] = l->argv[0];
57 l->argv += n;
58 l->argc -= n;
59 return 0;
60}
61
62int
63c_umask(char **wp)
64{
65 int i;
66 char *cp;
67 int symbolic = 0;
68 mode_t old_umask;
69 int optc;
70
71 while ((optc = ksh_getopt(wp, &builtin_opt, "S")) != -1)
72 switch (optc) {
73 case 'S':
74 symbolic = 1;
75 break;
76 case '?':
77 return 1;
78 }
79 cp = wp[builtin_opt.optind];
80 if (cp == NULL) {
81 old_umask = umask(0);
82 umask(old_umask);
83 if (symbolic) {
84 char buf[18];
85 int j;
86
87 old_umask = ~old_umask;
88 cp = buf;
89 for (i = 0; i < 3; i++) {
90 *cp++ = "ugo"[i];
91 *cp++ = '=';
92 for (j = 0; j < 3; j++)
93 if (old_umask & (1 << (8 - (3*i + j))))
94 *cp++ = "rwx"[j];
95 *cp++ = ',';
96 }
97 cp[-1] = '\0';
98 shprintf("%s\n", buf);
99 } else
100 shprintf("%#3.3o\n", old_umask);
101 } else {
102 mode_t new_umask;
103
104 if (digit(*cp)) {
105 for (new_umask = 0; *cp >= '0' && *cp <= '7'; cp++)
106 new_umask = new_umask * 8 + (*cp - '0');
107 if (*cp) {
108 bi_errorf("bad number");
109 return 1;
110 }
111 } else {
112 /* symbolic format */
113 int positions, new_val;
114 char op;
115
116 old_umask = umask(0);
117 umask(old_umask); /* in case of error */
118 old_umask = ~old_umask;
119 new_umask = old_umask;
120 positions = 0;
121 while (*cp) {
122 while (*cp && strchr("augo", *cp))
123 switch (*cp++) {
124 case 'a':
125 positions |= 0111;
126 break;
127 case 'u':
128 positions |= 0100;
129 break;
130 case 'g':
131 positions |= 0010;
132 break;
133 case 'o':
134 positions |= 0001;
135 break;
136 }
137 if (!positions)
138 positions = 0111; /* default is a */
139 if (!strchr("=+-", op = *cp))
140 break;
141 cp++;
142 new_val = 0;
143 while (*cp && strchr("rwxugoXs", *cp))
144 switch (*cp++) {
145 case 'r': new_val |= 04; break;
146 case 'w': new_val |= 02; break;
147 case 'x': new_val |= 01; break;
148 case 'u': new_val |= old_umask >> 6;
149 break;
150 case 'g': new_val |= old_umask >> 3;
151 break;
152 case 'o': new_val |= old_umask >> 0;
153 break;
154 case 'X': if (old_umask & 0111)
155 new_val |= 01;
156 break;
157 case 's': /* ignored */
158 break;
159 }
160 new_val = (new_val & 07) * positions;
161 switch (op) {
162 case '-':
163 new_umask &= ~new_val;
164 break;
165 case '=':
166 new_umask = new_val |
167 (new_umask & ~(positions * 07));
168 break;
169 case '+':
170 new_umask |= new_val;
171 }
172 if (*cp == ',') {
173 positions = 0;
174 cp++;
175 } else if (!strchr("=+-", *cp))
176 break;
177 }
178 if (*cp) {
179 bi_errorf("bad mask");
180 return 1;
181 }
182 new_umask = ~new_umask;
183 }
184 umask(new_umask);
185 }
186 return 0;
187}
188
189int
190c_dot(char **wp)
191{
192 char *file, *cp;
193 char **argv;
194 int argc;
195 int i;
196 int err;
197
198 if (ksh_getopt(wp, &builtin_opt, null) == '?')
199 return 1;
200
201 if ((cp = wp[builtin_opt.optind]) == NULL)
202 return 0;
203 file = search(cp, search_path, R_OK, &err);
204 if (file == NULL) {
205 bi_errorf("%s: %s", cp, err ? strerror(err) : "not found");
206 return 1;
207 }
208
209 /* Set positional parameters? */
210 if (wp[builtin_opt.optind + 1]) {
211 argv = wp + builtin_opt.optind;
212 argv[0] = genv->loc->argv[0]; /* preserve $0 */
213 for (argc = 0; argv[argc + 1]; argc++)
214 ;
215 } else {
216 argc = 0;
217 argv = NULL;
218 }
219 i = include(file, argc, argv, 0);
220 if (i < 0) { /* should not happen */
221 bi_errorf("%s: %s", cp, strerror(errno));
222 return 1;
223 }
224 return i;
225}
226
227int
228c_wait(char **wp)
229{
230 int rv = 0;
231 int sig;
232
233 if (ksh_getopt(wp, &builtin_opt, null) == '?')
234 return 1;
235 wp += builtin_opt.optind;
236 if (*wp == NULL) {
237 while (waitfor(NULL, &sig) >= 0)
238 ;
239 rv = sig;
240 } else {
241 for (; *wp; wp++)
242 rv = waitfor(*wp, &sig);
243 if (rv < 0)
244 rv = sig ? sig : 127; /* magic exit code: bad job-id */
245 }
246 return rv;
247}
248
249int
250c_read(char **wp)
251{
252 int c = 0;
253 int expand = 1, savehist = 0;
254 int expanding;
255 int ecode = 0;
256 char *cp;
257 int fd = 0;
258 struct shf *shf;
259 int optc;
260 const char *emsg;
261 XString cs, xs;
262 struct tbl *vp;
263 char *xp = NULL;
264
265 while ((optc = ksh_getopt(wp, &builtin_opt, "prsu,")) != -1)
266 switch (optc) {
267 case 'p':
268 if ((fd = coproc_getfd(R_OK, &emsg)) < 0) {
269 bi_errorf("-p: %s", emsg);
270 return 1;
271 }
272 break;
273 case 'r':
274 expand = 0;
275 break;
276 case 's':
277 savehist = 1;
278 break;
279 case 'u':
280 if (!*(cp = builtin_opt.optarg))
281 fd = 0;
282 else if ((fd = check_fd(cp, R_OK, &emsg)) < 0) {
283 bi_errorf("-u: %s: %s", cp, emsg);
284 return 1;
285 }
286 break;
287 case '?':
288 return 1;
289 }
290 wp += builtin_opt.optind;
291
292 if (*wp == NULL)
293 *--wp = "REPLY";
294
295 /* Since we can't necessarily seek backwards on non-regular files,
296 * don't buffer them so we can't read too much.
297 */
298 shf = shf_reopen(fd, SHF_RD | SHF_INTERRUPT | can_seek(fd), shl_spare);
299
300 if ((cp = strchr(*wp, '?')) != NULL) {
301 *cp = 0;
302 if (isatty(fd)) {
303 /* at&t ksh says it prints prompt on fd if it's open
304 * for writing and is a tty, but it doesn't do it
305 * (it also doesn't check the interactive flag,
306 * as is indicated in the Kornshell book).
307 */
308 shellf("%s", cp+1);
309 }
310 }
311
312 /* If we are reading from the co-process for the first time,
313 * make sure the other side of the pipe is closed first. This allows
314 * the detection of eof.
315 *
316 * This is not compatible with at&t ksh... the fd is kept so another
317 * coproc can be started with same output, however, this means eof
318 * can't be detected... This is why it is closed here.
319 * If this call is removed, remove the eof check below, too.
320 * coproc_readw_close(fd);
321 */
322
323 if (savehist)
324 Xinit(xs, xp, 128, ATEMP);
325 expanding = 0;
326 Xinit(cs, cp, 128, ATEMP);
327 for (; *wp != NULL; wp++) {
328 for (cp = Xstring(cs, cp); ; ) {
329 if (c == '\n' || c == EOF)
330 break;
331 while (1) {
332 c = shf_getc(shf);
333 if (c == '\0')
334 continue;
335 if (c == EOF && shf_error(shf) &&
336 shf->errno_ == EINTR) {
337 /* Was the offending signal one that
338 * would normally kill a process?
339 * If so, pretend the read was killed.
340 */
341 ecode = fatal_trap_check();
342
343 /* non fatal (eg, CHLD), carry on */
344 if (!ecode) {
345 shf_clearerr(shf);
346 continue;
347 }
348 }
349 break;
350 }
351 if (savehist) {
352 Xcheck(xs, xp);
353 Xput(xs, xp, c);
354 }
355 Xcheck(cs, cp);
356 if (expanding) {
357 expanding = 0;
358 if (c == '\n') {
359 c = 0;
360 if (Flag(FTALKING_I) && isatty(fd)) {
361 /* set prompt in case this is
362 * called from .profile or $ENV
363 */
364 set_prompt(PS2);
365 pprompt(prompt, 0);
366 }
367 } else if (c != EOF)
368 Xput(cs, cp, c);
369 continue;
370 }
371 if (expand && c == '\\') {
372 expanding = 1;
373 continue;
374 }
375 if (c == '\n' || c == EOF)
376 break;
377 if (ctype(c, C_IFS)) {
378 if (Xlength(cs, cp) == 0 && ctype(c, C_IFSWS))
379 continue;
380 if (wp[1])
381 break;
382 }
383 Xput(cs, cp, c);
384 }
385 /* strip trailing IFS white space from last variable */
386 if (!wp[1])
387 while (Xlength(cs, cp) && ctype(cp[-1], C_IFS) &&
388 ctype(cp[-1], C_IFSWS))
389 cp--;
390 Xput(cs, cp, '\0');
391 vp = global(*wp);
392 /* Must be done before setting export. */
393 if (vp->flag & RDONLY) {
394 shf_flush(shf);
395 bi_errorf("%s is read only", *wp);
396 return 1;
397 }
398 if (Flag(FEXPORT))
399 typeset(*wp, EXPORT, 0, 0, 0);
400 if (!setstr(vp, Xstring(cs, cp), KSH_RETURN_ERROR)) {
401 shf_flush(shf);
402 return 1;
403 }
404 }
405
406 shf_flush(shf);
407 if (savehist) {
408 Xput(xs, xp, '\0');
409 source->line++;
410 histsave(source->line, Xstring(xs, xp), 1);
411 Xfree(xs, xp);
412 }
413 /* if this is the co-process fd, close the file descriptor
414 * (can get eof if and only if all processes are have died, ie,
415 * coproc.njobs is 0 and the pipe is closed).
416 */
417 if (c == EOF && !ecode)
418 coproc_read_close(fd);
419
420 return ecode ? ecode : c == EOF;
421}
422
423int
424c_eval(char **wp)
425{
426 struct source *s;
427 struct source *saves = source;
428 int savef;
429 int rv;
430
431 if (ksh_getopt(wp, &builtin_opt, null) == '?')
432 return 1;
433 s = pushs(SWORDS, ATEMP);
434 s->u.strv = wp + builtin_opt.optind;
435 if (!Flag(FPOSIX)) {
436 /*
437 * Handle case where the command is empty due to failed
438 * command substitution, eg, eval "$(false)".
439 * In this case, shell() will not set/change exstat (because
440 * compiled tree is empty), so will use this value.
441 * subst_exstat is cleared in execute(), so should be 0 if
442 * there were no substitutions.
443 *
444 * A strict reading of POSIX says we don't do this (though
445 * it is traditionally done). [from 1003.2-1992]
446 * 3.9.1: Simple Commands
447 * ... If there is a command name, execution shall
448 * continue as described in 3.9.1.1. If there
449 * is no command name, but the command contained a command
450 * substitution, the command shall complete with the exit
451 * status of the last command substitution
452 * 3.9.1.1: Command Search and Execution
453 * ...(1)...(a) If the command name matches the name of
454 * a special built-in utility, that special built-in
455 * utility shall be invoked.
456 * 3.14.5: Eval
457 * ... If there are no arguments, or only null arguments,
458 * eval shall return an exit status of zero.
459 */
460 exstat = subst_exstat;
461 }
462
463 savef = Flag(FERREXIT);
464 Flag(FERREXIT) = 0;
465 rv = shell(s, false);
466 Flag(FERREXIT) = savef;
467 source = saves;
468 afree(s, ATEMP);
469 return (rv);
470}
471
472int
473c_trap(char **wp)
474{
475 int i;
476 char *s;
477 Trap *p;
478
479 if (ksh_getopt(wp, &builtin_opt, null) == '?')
480 return 1;
481 wp += builtin_opt.optind;
482
483 if (*wp == NULL) {
484 for (p = sigtraps, i = NSIG+1; --i >= 0; p++) {
485 if (p->trap != NULL) {
486 shprintf("trap -- ");
487 print_value_quoted(p->trap);
488 shprintf(" %s\n", p->name);
489 }
490 }
491 return 0;
492 }
493
494 /*
495 * Use case sensitive lookup for first arg so the
496 * command 'exit' isn't confused with the pseudo-signal
497 * 'EXIT'.
498 */
499 s = (gettrap(*wp, false) == NULL) ? *wp++ : NULL; /* get command */
500 if (s != NULL && s[0] == '-' && s[1] == '\0')
501 s = NULL;
502
503 /* set/clear traps */
504 while (*wp != NULL) {
505 p = gettrap(*wp++, true);
506 if (p == NULL) {
507 bi_errorf("bad signal %s", wp[-1]);
508 return 1;
509 }
510 settrap(p, s);
511 }
512 return 0;
513}
514
515int
516c_exitreturn(char **wp)
517{
518 int how = LEXIT;
519 int n;
520 char *arg;
521
522 if (ksh_getopt(wp, &builtin_opt, null) == '?')
523 return 1;
524 arg = wp[builtin_opt.optind];
525
526 if (arg) {
527 if (!getn(arg, &n)) {
528 exstat = 1;
529 warningf(true, "%s: bad number", arg);
530 } else
531 exstat = n;
532 }
533 if (wp[0][0] == 'r') { /* return */
534 struct env *ep;
535
536 /* need to tell if this is exit or return so trap exit will
537 * work right (POSIX)
538 */
539 for (ep = genv; ep; ep = ep->oenv)
540 if (STOP_RETURN(ep->type)) {
541 how = LRETURN;
542 break;
543 }
544 }
545
546 if (how == LEXIT && !really_exit && j_stopped_running()) {
547 really_exit = 1;
548 how = LSHELL;
549 }
550
551 quitenv(NULL); /* get rid of any i/o redirections */
552 unwind(how);
553 /* NOTREACHED */
554 return 0;
555}
556
557int
558c_brkcont(char **wp)
559{
560 int n, quit;
561 struct env *ep, *last_ep = NULL;
562 char *arg;
563
564 if (ksh_getopt(wp, &builtin_opt, null) == '?')
565 return 1;
566 arg = wp[builtin_opt.optind];
567
568 if (!arg)
569 n = 1;
570 else if (!bi_getn(arg, &n))
571 return 1;
572 quit = n;
573 if (quit <= 0) {
574 /* at&t ksh does this for non-interactive shells only - weird */
575 bi_errorf("%s: bad value", arg);
576 return 1;
577 }
578
579 /* Stop at E_NONE, E_PARSE, E_FUNC, or E_INCL */
580 for (ep = genv; ep && !STOP_BRKCONT(ep->type); ep = ep->oenv)
581 if (ep->type == E_LOOP) {
582 if (--quit == 0)
583 break;
584 ep->flags |= EF_BRKCONT_PASS;
585 last_ep = ep;
586 }
587
588 if (quit) {
589 /* at&t ksh doesn't print a message - just does what it
590 * can. We print a message 'cause it helps in debugging
591 * scripts, but don't generate an error (ie, keep going).
592 */
593 if (n == quit) {
594 warningf(true, "%s: cannot %s", wp[0], wp[0]);
595 return 0;
596 }
597 /* POSIX says if n is too big, the last enclosing loop
598 * shall be used. Doesn't say to print an error but we
599 * do anyway 'cause the user messed up.
600 */
601 if (last_ep)
602 last_ep->flags &= ~EF_BRKCONT_PASS;
603 warningf(true, "%s: can only %s %d level(s)",
604 wp[0], wp[0], n - quit);
605 }
606
607 unwind(*wp[0] == 'b' ? LBREAK : LCONTIN);
608 /* NOTREACHED */
609}
610
611int
612c_set(char **wp)
613{
614 int argi, setargs;
615 struct block *l = genv->loc;
616 char **owp = wp;
617
618 if (wp[1] == NULL) {
619 static const char *const args [] = { "set", "-", NULL };
620 return c_typeset((char **) args);
621 }
622
623 argi = parse_args(wp, OF_SET, &setargs);
624 if (argi < 0)
625 return 1;
626 /* set $# and $* */
627 if (setargs) {
628 owp = wp += argi - 1;
629 wp[0] = l->argv[0]; /* save $0 */
630 while (*++wp != NULL)
631 *wp = str_save(*wp, &l->area);
632 l->argc = wp - owp - 1;
633 l->argv = areallocarray(NULL, l->argc+2, sizeof(char *), &l->area);
634 for (wp = l->argv; (*wp++ = *owp++) != NULL; )
635 ;
636 }
637 /* POSIX says set exit status is 0, but old scripts that use
638 * getopt(1), use the construct: set -- `getopt ab:c "$@"`
639 * which assumes the exit value set will be that of the ``
640 * (subst_exstat is cleared in execute() so that it will be 0
641 * if there are no command substitutions).
642 */
643 return Flag(FPOSIX) ? 0 : subst_exstat;
644}
645
646int
647c_unset(char **wp)
648{
649 char *id;
650 int optc, unset_var = 1;
651
652 while ((optc = ksh_getopt(wp, &builtin_opt, "fv")) != -1)
653 switch (optc) {
654 case 'f':
655 unset_var = 0;
656 break;
657 case 'v':
658 unset_var = 1;
659 break;
660 case '?':
661 return 1;
662 }
663 wp += builtin_opt.optind;
664 for (; (id = *wp) != NULL; wp++)
665 if (unset_var) { /* unset variable */
666 struct tbl *vp = global(id);
667
668 if ((vp->flag&RDONLY)) {
669 bi_errorf("%s is read only", vp->name);
670 return 1;
671 }
672 unset(vp, strchr(id, '[') ? 1 : 0);
673 } else { /* unset function */
674 define(id, NULL);
675 }
676 return 0;
677}
678
679#ifndef TIMEVAL_TO_TIMESPEC
680#define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
681 (ts)->tv_sec = (tv)->tv_sec; \
682 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
683} while (0)
684#endif
685
686static void
687p_tv(struct shf *shf, int posix, struct timeval *tv, int width, char *prefix,
688 char *suffix)
689{
690 struct timespec ts;
691
692 TIMEVAL_TO_TIMESPEC(tv, &ts);
693 p_ts(shf, posix, &ts, width, prefix, suffix);
694}
695
696static void
697p_ts(struct shf *shf, int posix, struct timespec *ts, int width, char *prefix,
698 char *suffix)
699{
700 if (posix)
701 shf_fprintf(shf, "%s%*lld.%02ld%s", prefix ? prefix : "",
702 width, (long long)ts->tv_sec, ts->tv_nsec / 10000000,
703 suffix);
704 else
705 shf_fprintf(shf, "%s%*lldm%02lld.%02lds%s", prefix ? prefix : "",
706 width, (long long)ts->tv_sec / 60,
707 (long long)ts->tv_sec % 60,
708 ts->tv_nsec / 10000000, suffix);
709}
710
711
712int
713c_times(char **wp)
714{
715 struct rusage usage;
716
717 (void) getrusage(RUSAGE_SELF, &usage);
718 p_tv(shl_stdout, 0, &usage.ru_utime, 0, NULL, " ");
719 p_tv(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n");
720
721 (void) getrusage(RUSAGE_CHILDREN, &usage);
722 p_tv(shl_stdout, 0, &usage.ru_utime, 0, NULL, " ");
723 p_tv(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n");
724
725 return 0;
726}
727
728/*
729 * time pipeline (really a statement, not a built-in command)
730 */
731int
732timex(struct op *t, int f, volatile int *xerrok)
733{
734#define TF_NOARGS BIT(0)
735#define TF_NOREAL BIT(1) /* don't report real time */
736#define TF_POSIX BIT(2) /* report in posix format */
737 int rv = 0;
738 struct rusage ru0, ru1, cru0, cru1;
739 struct timeval usrtime, systime;
740 struct timespec ts0, ts1, ts2;
741 int tf = 0;
742 extern struct timeval j_usrtime, j_systime; /* computed by j_wait */
743
744 clock_gettime(CLOCK_MONOTONIC, &ts0);
745 getrusage(RUSAGE_SELF, &ru0);
746 getrusage(RUSAGE_CHILDREN, &cru0);
747 if (t->left) {
748 /*
749 * Two ways of getting cpu usage of a command: just use t0
750 * and t1 (which will get cpu usage from other jobs that
751 * finish while we are executing t->left), or get the
752 * cpu usage of t->left. at&t ksh does the former, while
753 * pdksh tries to do the later (the j_usrtime hack doesn't
754 * really work as it only counts the last job).
755 */
756 timerclear(&j_usrtime);
757 timerclear(&j_systime);
758 rv = execute(t->left, f | XTIME, xerrok);
759 if (t->left->type == TCOM)
760 tf |= t->left->str[0];
761 clock_gettime(CLOCK_MONOTONIC, &ts1);
762 getrusage(RUSAGE_SELF, &ru1);
763 getrusage(RUSAGE_CHILDREN, &cru1);
764 } else
765 tf = TF_NOARGS;
766
767 if (tf & TF_NOARGS) { /* ksh93 - report shell times (shell+kids) */
768 tf |= TF_NOREAL;
769 timeradd(&ru0.ru_utime, &cru0.ru_utime, &usrtime);
770 timeradd(&ru0.ru_stime, &cru0.ru_stime, &systime);
771 } else {
772 timersub(&ru1.ru_utime, &ru0.ru_utime, &usrtime);
773 timeradd(&usrtime, &j_usrtime, &usrtime);
774 timersub(&ru1.ru_stime, &ru0.ru_stime, &systime);
775 timeradd(&systime, &j_systime, &systime);
776 }
777
778 if (!(tf & TF_NOREAL)) {
779 timespecsub(&ts1, &ts0, &ts2);
780 if (tf & TF_POSIX)
781 p_ts(shl_out, 1, &ts2, 5, "real ", "\n");
782 else
783 p_ts(shl_out, 0, &ts2, 5, NULL, " real ");
784 }
785 if (tf & TF_POSIX)
786 p_tv(shl_out, 1, &usrtime, 5, "user ", "\n");
787 else
788 p_tv(shl_out, 0, &usrtime, 5, NULL, " user ");
789 if (tf & TF_POSIX)
790 p_tv(shl_out, 1, &systime, 5, "sys ", "\n");
791 else
792 p_tv(shl_out, 0, &systime, 5, NULL, " system\n");
793 shf_flush(shl_out);
794
795 return rv;
796}
797
798void
799timex_hook(struct op *t, char **volatile *app)
800{
801 char **wp = *app;
802 int optc;
803 int i, j;
804 Getopt opt;
805
806 ksh_getopt_reset(&opt, 0);
807 opt.optind = 0; /* start at the start */
808 while ((optc = ksh_getopt(wp, &opt, ":p")) != -1)
809 switch (optc) {
810 case 'p':
811 t->str[0] |= TF_POSIX;
812 break;
813 case '?':
814 errorf("time: -%s unknown option", opt.optarg);
815 case ':':
816 errorf("time: -%s requires an argument",
817 opt.optarg);
818 }
819 /* Copy command words down over options. */
820 if (opt.optind != 0) {
821 for (i = 0; i < opt.optind; i++)
822 afree(wp[i], ATEMP);
823 for (i = 0, j = opt.optind; (wp[i] = wp[j]); i++, j++)
824 ;
825 }
826 if (!wp[0])
827 t->str[0] |= TF_NOARGS;
828 *app = wp;
829}
830
831/* exec with no args - args case is taken care of in comexec() */
832int
833c_exec(char **wp)
834{
835 int i;
836
837 /* make sure redirects stay in place */
838 if (genv->savefd != NULL) {
839 for (i = 0; i < NUFILE; i++) {
840 if (genv->savefd[i] > 0)
841 close(genv->savefd[i]);
842 /*
843 * For ksh keep anything > 2 private,
844 * for sh, let them be (POSIX says what
845 * happens is unspecified and the bourne shell
846 * keeps them open).
847 */
848 if (!Flag(FSH) && i > 2 && genv->savefd[i])
849 fcntl(i, F_SETFD, FD_CLOEXEC);
850 }
851 genv->savefd = NULL;
852 }
853 return 0;
854}
855
856static int
857c_suspend(char **wp)
858{
859 if (wp[1] != NULL) {
860 bi_errorf("too many arguments");
861 return 1;
862 }
863 if (Flag(FLOGIN)) {
864 /* Can't suspend an orphaned process group. */
865 pid_t parent = getppid();
866 if (getpgid(parent) == getpgid(0) ||
867 getsid(parent) != getsid(0)) {
868 bi_errorf("can't suspend a login shell");
869 return 1;
870 }
871 }
872 j_suspend();
873 return 0;
874}
875
876/* dummy function, special case in comexec() */
877int
878c_builtin(char **wp)
879{
880 return 0;
881}
882
883extern int c_test(char **wp); /* in c_test.c */
884extern int c_ulimit(char **wp); /* in c_ulimit.c */
885
886/* A leading = means assignments before command are kept;
887 * a leading * means a POSIX special builtin;
888 * a leading + means a POSIX regular builtin
889 * (* and + should not be combined).
890 */
891const struct builtin shbuiltins [] = {
892 {"*=.", c_dot},
893 {"*=:", c_label},
894 {"[", c_test},
895 {"*=break", c_brkcont},
896 {"=builtin", c_builtin},
897 {"*=continue", c_brkcont},
898 {"*=eval", c_eval},
899 {"*=exec", c_exec},
900 {"*=exit", c_exitreturn},
901 {"+false", c_label},
902 {"*=return", c_exitreturn},
903 {"*=set", c_set},
904 {"*=shift", c_shift},
905 {"*=times", c_times},
906 {"*=trap", c_trap},
907 {"+=wait", c_wait},
908 {"+read", c_read},
909 {"test", c_test},
910 {"+true", c_label},
911 {"ulimit", c_ulimit},
912 {"+umask", c_umask},
913 {"*=unset", c_unset},
914 {"suspend", c_suspend},
915 {NULL, NULL}
916};