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