Home | History | Annotate | Line # | Download | only in sh
eval.c revision 1.119
      1 /*	$NetBSD: eval.c,v 1.119 2016/03/16 21:20:59 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
     39 #else
     40 __RCSID("$NetBSD: eval.c,v 1.119 2016/03/16 21:20:59 christos Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <stdbool.h>
     45 #include <stdlib.h>
     46 #include <signal.h>
     47 #include <stdio.h>
     48 #include <string.h>
     49 #include <errno.h>
     50 #include <limits.h>
     51 #include <unistd.h>
     52 #include <sys/fcntl.h>
     53 #include <sys/times.h>
     54 #include <sys/param.h>
     55 #include <sys/types.h>
     56 #include <sys/wait.h>
     57 #include <sys/sysctl.h>
     58 
     59 /*
     60  * Evaluate a command.
     61  */
     62 
     63 #include "shell.h"
     64 #include "nodes.h"
     65 #include "syntax.h"
     66 #include "expand.h"
     67 #include "parser.h"
     68 #include "jobs.h"
     69 #include "eval.h"
     70 #include "builtins.h"
     71 #include "options.h"
     72 #include "exec.h"
     73 #include "redir.h"
     74 #include "input.h"
     75 #include "output.h"
     76 #include "trap.h"
     77 #include "var.h"
     78 #include "memalloc.h"
     79 #include "error.h"
     80 #include "show.h"
     81 #include "mystring.h"
     82 #include "main.h"
     83 #ifndef SMALL
     84 #include "nodenames.h"
     85 #include "myhistedit.h"
     86 #endif
     87 
     88 
     89 /* flags in argument to evaltree */
     90 #define EV_EXIT 01		/* exit after evaluating tree */
     91 #define EV_TESTED 02		/* exit status is checked; ignore -e flag */
     92 #define EV_BACKCMD 04		/* command executing within back quotes */
     93 
     94 STATIC enum skipstate evalskip;	/* != SKIPNONE if we are skipping commands */
     95 STATIC int skipcount;		/* number of levels to skip */
     96 STATIC int loopnest;		/* current loop nesting level */
     97 STATIC int funcnest;		/* depth of function calls */
     98 STATIC int builtin_flags;	/* evalcommand flags for builtins */
     99 /*
    100  * Base function nesting level inside a dot command.  Set to 0 initially
    101  * and to (funcnest + 1) before every dot command to enable
    102  *   1) detection of being in a file sourced by a dot command and
    103  *   2) counting of function nesting in that file for the implementation
    104  *      of the return command.
    105  * The value is reset to its previous value after the dot command.
    106  */
    107 STATIC int dot_funcnest;
    108 
    109 
    110 const char *commandname;
    111 struct strlist *cmdenviron;
    112 int exitstatus;			/* exit status of last command */
    113 int back_exitstatus;		/* exit status of backquoted command */
    114 
    115 
    116 STATIC void evalloop(union node *, int);
    117 STATIC void evalfor(union node *, int);
    118 STATIC void evalcase(union node *, int);
    119 STATIC void evalsubshell(union node *, int);
    120 STATIC void expredir(union node *);
    121 STATIC void evalpipe(union node *);
    122 STATIC void evalcommand(union node *, int, struct backcmd *);
    123 STATIC void prehash(union node *);
    124 
    125 STATIC char *find_dot_file(char *);
    126 
    127 /*
    128  * Called to reset things after an exception.
    129  */
    130 
    131 #ifdef mkinit
    132 INCLUDE "eval.h"
    133 
    134 RESET {
    135 	reset_eval();
    136 }
    137 
    138 SHELLPROC {
    139 	exitstatus = 0;
    140 }
    141 #endif
    142 
    143 void
    144 reset_eval(void)
    145 {
    146 	evalskip = SKIPNONE;
    147 	dot_funcnest = 0;
    148 	loopnest = 0;
    149 	funcnest = 0;
    150 }
    151 
    152 static int
    153 sh_pipe(int fds[2])
    154 {
    155 	int nfd;
    156 
    157 	if (pipe(fds))
    158 		return -1;
    159 
    160 	if (fds[0] < 3) {
    161 		nfd = fcntl(fds[0], F_DUPFD, 3);
    162 		if (nfd != -1) {
    163 			close(fds[0]);
    164 			fds[0] = nfd;
    165 		}
    166 	}
    167 
    168 	if (fds[1] < 3) {
    169 		nfd = fcntl(fds[1], F_DUPFD, 3);
    170 		if (nfd != -1) {
    171 			close(fds[1]);
    172 			fds[1] = nfd;
    173 		}
    174 	}
    175 	return 0;
    176 }
    177 
    178 
    179 /*
    180  * The eval commmand.
    181  */
    182 
    183 int
    184 evalcmd(int argc, char **argv)
    185 {
    186         char *p;
    187         char *concat;
    188         char **ap;
    189 
    190         if (argc > 1) {
    191                 p = argv[1];
    192                 if (argc > 2) {
    193                         STARTSTACKSTR(concat);
    194                         ap = argv + 2;
    195                         for (;;) {
    196                                 while (*p)
    197                                         STPUTC(*p++, concat);
    198                                 if ((p = *ap++) == NULL)
    199                                         break;
    200                                 STPUTC(' ', concat);
    201                         }
    202                         STPUTC('\0', concat);
    203                         p = grabstackstr(concat);
    204                 }
    205                 evalstring(p, builtin_flags & EV_TESTED);
    206         }
    207         return exitstatus;
    208 }
    209 
    210 
    211 /*
    212  * Execute a command or commands contained in a string.
    213  */
    214 
    215 void
    216 evalstring(char *s, int flag)
    217 {
    218 	union node *n;
    219 	struct stackmark smark;
    220 
    221 	setstackmark(&smark);
    222 	setinputstring(s, 1);
    223 
    224 	while ((n = parsecmd(0)) != NEOF) {
    225 		TRACE(("evalstring: "); showtree(n));
    226 		if (nflag == 0)
    227 			evaltree(n, flag);
    228 		popstackmark(&smark);
    229 	}
    230 	popfile();
    231 	popstackmark(&smark);
    232 }
    233 
    234 
    235 
    236 /*
    237  * Evaluate a parse tree.  The value is left in the global variable
    238  * exitstatus.
    239  */
    240 
    241 void
    242 evaltree(union node *n, int flags)
    243 {
    244 	bool do_etest;
    245 
    246 	do_etest = false;
    247 	if (n == NULL || nflag) {
    248 		TRACE(("evaltree(%s) called\n", n == NULL ? "NULL" : "-n"));
    249 		if (nflag == 0)
    250 			exitstatus = 0;
    251 		goto out;
    252 	}
    253 #ifndef SMALL
    254 	displayhist = 1;	/* show history substitutions done with fc */
    255 #endif
    256 #ifdef NODETYPENAME
    257 	TRACE(("pid %d, evaltree(%p: %s(%d), %#x) called\n",
    258 	    getpid(), n, NODETYPENAME(n->type), n->type, flags));
    259 #else
    260 	TRACE(("pid %d, evaltree(%p: %d, %#x) called\n",
    261 	    getpid(), n, n->type, flags));
    262 #endif
    263 	switch (n->type) {
    264 	case NSEMI:
    265 		evaltree(n->nbinary.ch1, flags & EV_TESTED);
    266 		if (nflag || evalskip)
    267 			goto out;
    268 		evaltree(n->nbinary.ch2, flags);
    269 		break;
    270 	case NAND:
    271 		evaltree(n->nbinary.ch1, EV_TESTED);
    272 		if (nflag || evalskip || exitstatus != 0)
    273 			goto out;
    274 		evaltree(n->nbinary.ch2, flags);
    275 		break;
    276 	case NOR:
    277 		evaltree(n->nbinary.ch1, EV_TESTED);
    278 		if (nflag || evalskip || exitstatus == 0)
    279 			goto out;
    280 		evaltree(n->nbinary.ch2, flags);
    281 		break;
    282 	case NREDIR:
    283 		expredir(n->nredir.redirect);
    284 		redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP);
    285 		evaltree(n->nredir.n, flags);
    286 		popredir();
    287 		break;
    288 	case NSUBSHELL:
    289 		evalsubshell(n, flags);
    290 		do_etest = !(flags & EV_TESTED);
    291 		break;
    292 	case NBACKGND:
    293 		evalsubshell(n, flags);
    294 		break;
    295 	case NIF: {
    296 		evaltree(n->nif.test, EV_TESTED);
    297 		if (nflag || evalskip)
    298 			goto out;
    299 		if (exitstatus == 0)
    300 			evaltree(n->nif.ifpart, flags);
    301 		else if (n->nif.elsepart)
    302 			evaltree(n->nif.elsepart, flags);
    303 		else
    304 			exitstatus = 0;
    305 		break;
    306 	}
    307 	case NWHILE:
    308 	case NUNTIL:
    309 		evalloop(n, flags);
    310 		break;
    311 	case NFOR:
    312 		evalfor(n, flags);
    313 		break;
    314 	case NCASE:
    315 		evalcase(n, flags);
    316 		break;
    317 	case NDEFUN:
    318 		defun(n->narg.text, n->narg.next);
    319 		exitstatus = 0;
    320 		break;
    321 	case NNOT:
    322 		evaltree(n->nnot.com, EV_TESTED);
    323 		exitstatus = !exitstatus;
    324 		break;
    325 	case NPIPE:
    326 		evalpipe(n);
    327 		do_etest = !(flags & EV_TESTED);
    328 		break;
    329 	case NCMD:
    330 		evalcommand(n, flags, NULL);
    331 		do_etest = !(flags & EV_TESTED);
    332 		break;
    333 	default:
    334 #ifdef NODETYPENAME
    335 		out1fmt("Node type = %d(%s)\n", n->type, NODETYPENAME(n->type));
    336 #else
    337 		out1fmt("Node type = %d\n", n->type);
    338 #endif
    339 		flushout(&output);
    340 		break;
    341 	}
    342 out:
    343 	if (pendingsigs)
    344 		dotrap();
    345 	if ((flags & EV_EXIT) != 0 || (eflag && exitstatus != 0 && do_etest))
    346 		exitshell(exitstatus);
    347 }
    348 
    349 
    350 STATIC void
    351 evalloop(union node *n, int flags)
    352 {
    353 	int status;
    354 
    355 	loopnest++;
    356 	status = 0;
    357 
    358 #ifdef NODETYPENAME
    359 	TRACE(("evalloop %s: ", NODETYPENAME(n->type)));
    360 #else
    361 	TRACE(("evalloop %s: ", n->type == NWHILE ? "while" : "until"));
    362 #endif
    363 	TRACE((""); showtree(n->nbinary.ch1));
    364 	TRACE(("evalloop    do: "); showtree(n->nbinary.ch2));
    365 	TRACE(("evalloop  done\n"));
    366 
    367 	for (;;) {
    368 		evaltree(n->nbinary.ch1, EV_TESTED);
    369 		if (nflag)
    370 			break;
    371 		if (evalskip) {
    372 skipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
    373 				evalskip = SKIPNONE;
    374 				continue;
    375 			}
    376 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    377 				evalskip = SKIPNONE;
    378 			break;
    379 		}
    380 		if (n->type == NWHILE) {
    381 			if (exitstatus != 0)
    382 				break;
    383 		} else {
    384 			if (exitstatus == 0)
    385 				break;
    386 		}
    387 		evaltree(n->nbinary.ch2, flags & EV_TESTED);
    388 		status = exitstatus;
    389 		if (evalskip)
    390 			goto skipping;
    391 	}
    392 	loopnest--;
    393 	exitstatus = status;
    394 }
    395 
    396 
    397 
    398 STATIC void
    399 evalfor(union node *n, int flags)
    400 {
    401 	struct arglist arglist;
    402 	union node *argp;
    403 	struct strlist *sp;
    404 	struct stackmark smark;
    405 	int status;
    406 
    407 	status = nflag ? exitstatus : 0;
    408 
    409 	setstackmark(&smark);
    410 	arglist.lastp = &arglist.list;
    411 	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
    412 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    413 		if (evalskip)
    414 			goto out;
    415 	}
    416 	*arglist.lastp = NULL;
    417 
    418 	loopnest++;
    419 	for (sp = arglist.list ; sp ; sp = sp->next) {
    420 		setvar(n->nfor.var, sp->text, 0);
    421 		evaltree(n->nfor.body, flags & EV_TESTED);
    422 		status = exitstatus;
    423 		if (nflag)
    424 			break;
    425 		if (evalskip) {
    426 			if (evalskip == SKIPCONT && --skipcount <= 0) {
    427 				evalskip = SKIPNONE;
    428 				continue;
    429 			}
    430 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    431 				evalskip = SKIPNONE;
    432 			break;
    433 		}
    434 	}
    435 	loopnest--;
    436 	exitstatus = status;
    437 out:
    438 	popstackmark(&smark);
    439 }
    440 
    441 
    442 
    443 STATIC void
    444 evalcase(union node *n, int flags)
    445 {
    446 	union node *cp;
    447 	union node *patp;
    448 	struct arglist arglist;
    449 	struct stackmark smark;
    450 	int status = 0;
    451 
    452 	setstackmark(&smark);
    453 	arglist.lastp = &arglist.list;
    454 	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
    455 	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
    456 		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
    457 			if (casematch(patp, arglist.list->text)) {
    458 				if (evalskip == 0) {
    459 					evaltree(cp->nclist.body, flags);
    460 					status = exitstatus;
    461 				}
    462 				goto out;
    463 			}
    464 		}
    465 	}
    466 out:
    467 	exitstatus = status;
    468 	popstackmark(&smark);
    469 }
    470 
    471 
    472 
    473 /*
    474  * Kick off a subshell to evaluate a tree.
    475  */
    476 
    477 STATIC void
    478 evalsubshell(union node *n, int flags)
    479 {
    480 	struct job *jp;
    481 	int backgnd = (n->type == NBACKGND);
    482 
    483 	expredir(n->nredir.redirect);
    484 	INTOFF;
    485 	jp = makejob(n, 1);
    486 	if (forkshell(jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
    487 		INTON;
    488 		if (backgnd)
    489 			flags &=~ EV_TESTED;
    490 		redirect(n->nredir.redirect, REDIR_KEEP);
    491 		/* never returns */
    492 		evaltree(n->nredir.n, flags | EV_EXIT);
    493 	}
    494 	exitstatus = backgnd ? 0 : waitforjob(jp);
    495 	INTON;
    496 }
    497 
    498 
    499 
    500 /*
    501  * Compute the names of the files in a redirection list.
    502  */
    503 
    504 STATIC void
    505 expredir(union node *n)
    506 {
    507 	union node *redir;
    508 
    509 	for (redir = n ; redir ; redir = redir->nfile.next) {
    510 		struct arglist fn;
    511 		fn.lastp = &fn.list;
    512 		switch (redir->type) {
    513 		case NFROMTO:
    514 		case NFROM:
    515 		case NTO:
    516 		case NCLOBBER:
    517 		case NAPPEND:
    518 			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
    519 			redir->nfile.expfname = fn.list->text;
    520 			break;
    521 		case NFROMFD:
    522 		case NTOFD:
    523 			if (redir->ndup.vname) {
    524 				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
    525 				fixredir(redir, fn.list->text, 1);
    526 			}
    527 			break;
    528 		}
    529 	}
    530 }
    531 
    532 
    533 
    534 /*
    535  * Evaluate a pipeline.  All the processes in the pipeline are children
    536  * of the process creating the pipeline.  (This differs from some versions
    537  * of the shell, which make the last process in a pipeline the parent
    538  * of all the rest.)
    539  */
    540 
    541 STATIC void
    542 evalpipe(union node *n)
    543 {
    544 	struct job *jp;
    545 	struct nodelist *lp;
    546 	int pipelen;
    547 	int prevfd;
    548 	int pip[2];
    549 
    550 	TRACE(("evalpipe(0x%lx) called\n", (long)n));
    551 	pipelen = 0;
    552 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
    553 		pipelen++;
    554 	INTOFF;
    555 	jp = makejob(n, pipelen);
    556 	prevfd = -1;
    557 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
    558 		prehash(lp->n);
    559 		pip[1] = -1;
    560 		if (lp->next) {
    561 			if (sh_pipe(pip) < 0) {
    562 				if (prevfd >= 0)
    563 					close(prevfd);
    564 				error("Pipe call failed");
    565 			}
    566 		}
    567 		if (forkshell(jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
    568 			INTON;
    569 			if (prevfd > 0) {
    570 				close(0);
    571 				copyfd(prevfd, 0, 1, 0);
    572 				close(prevfd);
    573 			}
    574 			if (pip[1] >= 0) {
    575 				close(pip[0]);
    576 				if (pip[1] != 1) {
    577 					close(1);
    578 					copyfd(pip[1], 1, 1, 0);
    579 					close(pip[1]);
    580 				}
    581 			}
    582 			evaltree(lp->n, EV_EXIT);
    583 		}
    584 		if (prevfd >= 0)
    585 			close(prevfd);
    586 		prevfd = pip[0];
    587 		close(pip[1]);
    588 	}
    589 	if (n->npipe.backgnd == 0) {
    590 		exitstatus = waitforjob(jp);
    591 		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
    592 	} else
    593 		exitstatus = 0;
    594 	INTON;
    595 }
    596 
    597 
    598 
    599 /*
    600  * Execute a command inside back quotes.  If it's a builtin command, we
    601  * want to save its output in a block obtained from malloc.  Otherwise
    602  * we fork off a subprocess and get the output of the command via a pipe.
    603  * Should be called with interrupts off.
    604  */
    605 
    606 void
    607 evalbackcmd(union node *n, struct backcmd *result)
    608 {
    609 	int pip[2];
    610 	struct job *jp;
    611 	struct stackmark smark;		/* unnecessary */
    612 
    613 	setstackmark(&smark);
    614 	result->fd = -1;
    615 	result->buf = NULL;
    616 	result->nleft = 0;
    617 	result->jp = NULL;
    618 	if (nflag || n == NULL) {
    619 		goto out;
    620 	}
    621 #ifdef notyet
    622 	/*
    623 	 * For now we disable executing builtins in the same
    624 	 * context as the shell, because we are not keeping
    625 	 * enough state to recover from changes that are
    626 	 * supposed only to affect subshells. eg. echo "`cd /`"
    627 	 */
    628 	if (n->type == NCMD) {
    629 		exitstatus = oexitstatus;
    630 		evalcommand(n, EV_BACKCMD, result);
    631 	} else
    632 #endif
    633 	{
    634 		INTOFF;
    635 		if (sh_pipe(pip) < 0)
    636 			error("Pipe call failed");
    637 		jp = makejob(n, 1);
    638 		if (forkshell(jp, n, FORK_NOJOB) == 0) {
    639 			FORCEINTON;
    640 			close(pip[0]);
    641 			if (pip[1] != 1) {
    642 				close(1);
    643 				copyfd(pip[1], 1, 1, 0);
    644 				close(pip[1]);
    645 			}
    646 			eflag = 0;
    647 			evaltree(n, EV_EXIT);
    648 			/* NOTREACHED */
    649 		}
    650 		close(pip[1]);
    651 		result->fd = pip[0];
    652 		result->jp = jp;
    653 		INTON;
    654 	}
    655 out:
    656 	popstackmark(&smark);
    657 	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
    658 		result->fd, result->buf, result->nleft, result->jp));
    659 }
    660 
    661 static const char *
    662 syspath(void)
    663 {
    664 	static char *sys_path = NULL;
    665 	static int mib[] = {CTL_USER, USER_CS_PATH};
    666 	static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
    667 	size_t len;
    668 
    669 	if (sys_path == NULL) {
    670 		if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
    671 		    (sys_path = ckmalloc(len + 5)) != NULL &&
    672 		    sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
    673 			memcpy(sys_path, "PATH=", 5);
    674 		} else {
    675 			ckfree(sys_path);
    676 			/* something to keep things happy */
    677 			sys_path = def_path;
    678 		}
    679 	}
    680 	return sys_path;
    681 }
    682 
    683 static int
    684 parse_command_args(int argc, char **argv, int *use_syspath)
    685 {
    686 	int sv_argc = argc;
    687 	char *cp, c;
    688 
    689 	*use_syspath = 0;
    690 
    691 	for (;;) {
    692 		argv++;
    693 		if (--argc == 0)
    694 			break;
    695 		cp = *argv;
    696 		if (*cp++ != '-')
    697 			break;
    698 		if (*cp == '-' && cp[1] == 0) {
    699 			argv++;
    700 			argc--;
    701 			break;
    702 		}
    703 		while ((c = *cp++)) {
    704 			switch (c) {
    705 			case 'p':
    706 				*use_syspath = 1;
    707 				break;
    708 			default:
    709 				/* run 'typecmd' for other options */
    710 				return 0;
    711 			}
    712 		}
    713 	}
    714 	return sv_argc - argc;
    715 }
    716 
    717 int vforked = 0;
    718 extern char *trap[];
    719 
    720 /*
    721  * Execute a simple command.
    722  */
    723 
    724 STATIC void
    725 evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
    726 {
    727 	struct stackmark smark;
    728 	union node *argp;
    729 	struct arglist arglist;
    730 	struct arglist varlist;
    731 	volatile int flags = flgs;
    732 	char ** volatile argv;
    733 	volatile int argc;
    734 	char **envp;
    735 	int varflag;
    736 	struct strlist *sp;
    737 	volatile int mode;
    738 	int pip[2];
    739 	struct cmdentry cmdentry;
    740 	struct job * volatile jp;
    741 	struct jmploc jmploc;
    742 	struct jmploc *volatile savehandler = NULL;
    743 	const char *volatile savecmdname;
    744 	volatile struct shparam saveparam;
    745 	struct localvar *volatile savelocalvars;
    746 	volatile int e;
    747 	char * volatile lastarg;
    748 	const char * volatile path = pathval();
    749 	volatile int temp_path;
    750 
    751 	vforked = 0;
    752 	/* First expand the arguments. */
    753 	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
    754 	setstackmark(&smark);
    755 	back_exitstatus = 0;
    756 
    757 	arglist.lastp = &arglist.list;
    758 	varflag = 1;
    759 	/* Expand arguments, ignoring the initial 'name=value' ones */
    760 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    761 		char *p = argp->narg.text;
    762 		if (varflag && is_name(*p)) {
    763 			do {
    764 				p++;
    765 			} while (is_in_name(*p));
    766 			if (*p == '=')
    767 				continue;
    768 		}
    769 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    770 		varflag = 0;
    771 	}
    772 	*arglist.lastp = NULL;
    773 
    774 	expredir(cmd->ncmd.redirect);
    775 
    776 	/* Now do the initial 'name=value' ones we skipped above */
    777 	varlist.lastp = &varlist.list;
    778 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    779 		char *p = argp->narg.text;
    780 		if (!is_name(*p))
    781 			break;
    782 		do
    783 			p++;
    784 		while (is_in_name(*p));
    785 		if (*p != '=')
    786 			break;
    787 		expandarg(argp, &varlist, EXP_VARTILDE);
    788 	}
    789 	*varlist.lastp = NULL;
    790 
    791 	argc = 0;
    792 	for (sp = arglist.list ; sp ; sp = sp->next)
    793 		argc++;
    794 	argv = stalloc(sizeof (char *) * (argc + 1));
    795 
    796 	for (sp = arglist.list ; sp ; sp = sp->next) {
    797 		TRACE(("evalcommand arg: %s\n", sp->text));
    798 		*argv++ = sp->text;
    799 	}
    800 	*argv = NULL;
    801 	lastarg = NULL;
    802 	if (iflag && funcnest == 0 && argc > 0)
    803 		lastarg = argv[-1];
    804 	argv -= argc;
    805 
    806 	/* Print the command if xflag is set. */
    807 	if (xflag) {
    808 		char sep = 0;
    809 		out2str(ps4val());
    810 		for (sp = varlist.list ; sp ; sp = sp->next) {
    811 			char *p;
    812 
    813 			if (sep != 0)
    814 				outc(sep, &errout);
    815 
    816 			/*
    817 			 * The "var=" part should not be quoted, regardless
    818 			 * of the value, or it would not represent an
    819 			 * assignment, but rather a command
    820 			 */
    821 			p = strchr(sp->text, '=');
    822 			if (p != NULL) {
    823 				*p = '\0';	/*XXX*/
    824 				out2shstr(sp->text);
    825 				out2c('=');
    826 				*p++ = '=';	/*XXX*/
    827 			} else
    828 				p = sp->text;
    829 			out2shstr(p);
    830 			sep = ' ';
    831 		}
    832 		for (sp = arglist.list ; sp ; sp = sp->next) {
    833 			if (sep != 0)
    834 				outc(sep, &errout);
    835 			out2shstr(sp->text);
    836 			sep = ' ';
    837 		}
    838 		outc('\n', &errout);
    839 		flushout(&errout);
    840 	}
    841 
    842 	/* Now locate the command. */
    843 	if (argc == 0) {
    844 		cmdentry.cmdtype = CMDSPLBLTIN;
    845 		cmdentry.u.bltin = bltincmd;
    846 	} else {
    847 		static const char PATH[] = "PATH=";
    848 		int cmd_flags = DO_ERR;
    849 
    850 		/*
    851 		 * Modify the command lookup path, if a PATH= assignment
    852 		 * is present
    853 		 */
    854 		for (sp = varlist.list; sp; sp = sp->next)
    855 			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
    856 				path = sp->text + sizeof(PATH) - 1;
    857 
    858 		do {
    859 			int argsused, use_syspath;
    860 			find_command(argv[0], &cmdentry, cmd_flags, path);
    861 			if (cmdentry.cmdtype == CMDUNKNOWN) {
    862 				exitstatus = 127;
    863 				flushout(&errout);
    864 				goto out;
    865 			}
    866 
    867 			/* implement the 'command' builtin here */
    868 			if (cmdentry.cmdtype != CMDBUILTIN ||
    869 			    cmdentry.u.bltin != bltincmd)
    870 				break;
    871 			cmd_flags |= DO_NOFUNC;
    872 			argsused = parse_command_args(argc, argv, &use_syspath);
    873 			if (argsused == 0) {
    874 				/* use 'type' builting to display info */
    875 				cmdentry.u.bltin = typecmd;
    876 				break;
    877 			}
    878 			argc -= argsused;
    879 			argv += argsused;
    880 			if (use_syspath)
    881 				path = syspath() + 5;
    882 		} while (argc != 0);
    883 		if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
    884 			/* posix mandates that 'command <splbltin>' act as if
    885 			   <splbltin> was a normal builtin */
    886 			cmdentry.cmdtype = CMDBUILTIN;
    887 	}
    888 
    889 	/* Fork off a child process if necessary. */
    890 	if (cmd->ncmd.backgnd || (trap[0] && (flags & EV_EXIT) != 0)
    891 	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
    892 	 || ((flags & EV_BACKCMD) != 0
    893 	    && ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN)
    894 		 || cmdentry.u.bltin == dotcmd
    895 		 || cmdentry.u.bltin == evalcmd))) {
    896 		INTOFF;
    897 		jp = makejob(cmd, 1);
    898 		mode = cmd->ncmd.backgnd;
    899 		if (flags & EV_BACKCMD) {
    900 			mode = FORK_NOJOB;
    901 			if (sh_pipe(pip) < 0)
    902 				error("Pipe call failed");
    903 		}
    904 #ifdef DO_SHAREDVFORK
    905 		/* It is essential that if DO_SHAREDVFORK is defined that the
    906 		 * child's address space is actually shared with the parent as
    907 		 * we rely on this.
    908 		 */
    909 		if (usefork == 0 && cmdentry.cmdtype == CMDNORMAL) {
    910 			pid_t	pid;
    911 			int serrno;
    912 
    913 			savelocalvars = localvars;
    914 			localvars = NULL;
    915 			vforked = 1;
    916 			switch (pid = vfork()) {
    917 			case -1:
    918 				serrno = errno;
    919 				TRACE(("Vfork failed, errno=%d\n", serrno));
    920 				INTON;
    921 				error("Cannot vfork (%s)", strerror(serrno));
    922 				break;
    923 			case 0:
    924 				/* Make sure that exceptions only unwind to
    925 				 * after the vfork(2)
    926 				 */
    927 				if (setjmp(jmploc.loc)) {
    928 					if (exception == EXSHELLPROC) {
    929 						/* We can't progress with the vfork,
    930 						 * so, set vforked = 2 so the parent
    931 						 * knows, and _exit();
    932 						 */
    933 						vforked = 2;
    934 						_exit(0);
    935 					} else {
    936 						_exit(exerrno);
    937 					}
    938 				}
    939 				savehandler = handler;
    940 				handler = &jmploc;
    941 				listmklocal(varlist.list, VEXPORT | VNOFUNC);
    942 				forkchild(jp, cmd, mode, vforked);
    943 				break;
    944 			default:
    945 				handler = savehandler;	/* restore from vfork(2) */
    946 				poplocalvars();
    947 				localvars = savelocalvars;
    948 				if (vforked == 2) {
    949 					vforked = 0;
    950 
    951 					(void)waitpid(pid, NULL, 0);
    952 					/* We need to progress in a normal fork fashion */
    953 					goto normal_fork;
    954 				}
    955 				vforked = 0;
    956 				forkparent(jp, cmd, mode, pid);
    957 				goto parent;
    958 			}
    959 		} else {
    960 normal_fork:
    961 #endif
    962 			if (forkshell(jp, cmd, mode) != 0)
    963 				goto parent;	/* at end of routine */
    964 			FORCEINTON;
    965 #ifdef DO_SHAREDVFORK
    966 		}
    967 #endif
    968 		if (flags & EV_BACKCMD) {
    969 			if (!vforked) {
    970 				FORCEINTON;
    971 			}
    972 			close(pip[0]);
    973 			if (pip[1] != 1) {
    974 				close(1);
    975 				copyfd(pip[1], 1, 1, 0);
    976 				close(pip[1]);
    977 			}
    978 		}
    979 		flags |= EV_EXIT;
    980 	}
    981 
    982 	/* This is the child process if a fork occurred. */
    983 	/* Execute the command. */
    984 	switch (cmdentry.cmdtype) {
    985 	case CMDFUNCTION:
    986 #ifdef DEBUG
    987 		trputs("Shell function:  ");  trargs(argv);
    988 #endif
    989 		redirect(cmd->ncmd.redirect, REDIR_PUSH);
    990 		saveparam = shellparam;
    991 		shellparam.malloc = 0;
    992 		shellparam.reset = 1;
    993 		shellparam.nparam = argc - 1;
    994 		shellparam.p = argv + 1;
    995 		shellparam.optnext = NULL;
    996 		INTOFF;
    997 		savelocalvars = localvars;
    998 		localvars = NULL;
    999 		INTON;
   1000 		if (setjmp(jmploc.loc)) {
   1001 			if (exception == EXSHELLPROC) {
   1002 				freeparam((volatile struct shparam *)
   1003 				    &saveparam);
   1004 			} else {
   1005 				freeparam(&shellparam);
   1006 				shellparam = saveparam;
   1007 			}
   1008 			poplocalvars();
   1009 			localvars = savelocalvars;
   1010 			handler = savehandler;
   1011 			longjmp(handler->loc, 1);
   1012 		}
   1013 		savehandler = handler;
   1014 		handler = &jmploc;
   1015 		listmklocal(varlist.list, VEXPORT);
   1016 		/* stop shell blowing its stack */
   1017 		if (++funcnest > 1000)
   1018 			error("too many nested function calls");
   1019 		evaltree(cmdentry.u.func, flags & EV_TESTED);
   1020 		funcnest--;
   1021 		INTOFF;
   1022 		poplocalvars();
   1023 		localvars = savelocalvars;
   1024 		freeparam(&shellparam);
   1025 		shellparam = saveparam;
   1026 		handler = savehandler;
   1027 		popredir();
   1028 		INTON;
   1029 		if (evalskip == SKIPFUNC) {
   1030 			evalskip = SKIPNONE;
   1031 			skipcount = 0;
   1032 		}
   1033 		if (flags & EV_EXIT)
   1034 			exitshell(exitstatus);
   1035 		break;
   1036 
   1037 	case CMDBUILTIN:
   1038 	case CMDSPLBLTIN:
   1039 #ifdef DEBUG
   1040 		trputs("builtin command:  ");  trargs(argv);
   1041 #endif
   1042 		mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
   1043 		if (flags == EV_BACKCMD) {
   1044 			memout.nleft = 0;
   1045 			memout.nextc = memout.buf;
   1046 			memout.bufsize = 64;
   1047 			mode |= REDIR_BACKQ;
   1048 		}
   1049 		e = -1;
   1050 		savehandler = handler;
   1051 		savecmdname = commandname;
   1052 		handler = &jmploc;
   1053 		temp_path = 0;
   1054 		if (!setjmp(jmploc.loc)) {
   1055 			/* We need to ensure the command hash table isn't
   1056 			 * corruped by temporary PATH assignments.
   1057 			 * However we must ensure the 'local' command works!
   1058 			 */
   1059 			if (path != pathval() && (cmdentry.u.bltin == hashcmd ||
   1060 			    cmdentry.u.bltin == typecmd)) {
   1061 				savelocalvars = localvars;
   1062 				localvars = 0;
   1063 				temp_path = 1;
   1064 				mklocal(path - 5 /* PATH= */, 0);
   1065 			}
   1066 			redirect(cmd->ncmd.redirect, mode);
   1067 
   1068 			/* exec is a special builtin, but needs this list... */
   1069 			cmdenviron = varlist.list;
   1070 			/* we must check 'readonly' flag for all builtins */
   1071 			listsetvar(varlist.list,
   1072 				cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
   1073 			commandname = argv[0];
   1074 			/* initialize nextopt */
   1075 			argptr = argv + 1;
   1076 			optptr = NULL;
   1077 			/* and getopt */
   1078 			optreset = 1;
   1079 			optind = 1;
   1080 			builtin_flags = flags;
   1081 			exitstatus = cmdentry.u.bltin(argc, argv);
   1082 		} else {
   1083 			e = exception;
   1084 			exitstatus = e == EXINT ? SIGINT + 128 :
   1085 					e == EXEXEC ? exerrno : 2;
   1086 		}
   1087 		handler = savehandler;
   1088 		flushall();
   1089 		out1 = &output;
   1090 		out2 = &errout;
   1091 		freestdout();
   1092 		if (temp_path) {
   1093 			poplocalvars();
   1094 			localvars = savelocalvars;
   1095 		}
   1096 		cmdenviron = NULL;
   1097 		if (e != EXSHELLPROC) {
   1098 			commandname = savecmdname;
   1099 			if (flags & EV_EXIT)
   1100 				exitshell(exitstatus);
   1101 		}
   1102 		if (e != -1) {
   1103 			if ((e != EXERROR && e != EXEXEC)
   1104 			    || cmdentry.cmdtype == CMDSPLBLTIN)
   1105 				exraise(e);
   1106 			FORCEINTON;
   1107 		}
   1108 		if (cmdentry.u.bltin != execcmd)
   1109 			popredir();
   1110 		if (flags == EV_BACKCMD) {
   1111 			backcmd->buf = memout.buf;
   1112 			backcmd->nleft = memout.nextc - memout.buf;
   1113 			memout.buf = NULL;
   1114 		}
   1115 		break;
   1116 
   1117 	default:
   1118 #ifdef DEBUG
   1119 		trputs("normal command:  ");  trargs(argv);
   1120 #endif
   1121 		redirect(cmd->ncmd.redirect,
   1122 		    (vforked ? REDIR_VFORK : 0) | REDIR_KEEP);
   1123 		if (!vforked)
   1124 			for (sp = varlist.list ; sp ; sp = sp->next)
   1125 				setvareq(sp->text, VEXPORT|VSTACK);
   1126 		envp = environment();
   1127 		shellexec(argv, envp, path, cmdentry.u.index, vforked);
   1128 		break;
   1129 	}
   1130 	goto out;
   1131 
   1132 parent:	/* parent process gets here (if we forked) */
   1133 	exitstatus = 0;		/* if not altered just below */
   1134 	if (mode == FORK_FG) {	/* argument to fork */
   1135 		exitstatus = waitforjob(jp);
   1136 	} else if (mode == FORK_NOJOB) {
   1137 		backcmd->fd = pip[0];
   1138 		close(pip[1]);
   1139 		backcmd->jp = jp;
   1140 	}
   1141 	FORCEINTON;
   1142 
   1143 out:
   1144 	if (lastarg)
   1145 		/* dsl: I think this is intended to be used to support
   1146 		 * '_' in 'vi' command mode during line editing...
   1147 		 * However I implemented that within libedit itself.
   1148 		 */
   1149 		setvar("_", lastarg, 0);
   1150 	popstackmark(&smark);
   1151 }
   1152 
   1153 
   1154 /*
   1155  * Search for a command.  This is called before we fork so that the
   1156  * location of the command will be available in the parent as well as
   1157  * the child.  The check for "goodname" is an overly conservative
   1158  * check that the name will not be subject to expansion.
   1159  */
   1160 
   1161 STATIC void
   1162 prehash(union node *n)
   1163 {
   1164 	struct cmdentry entry;
   1165 
   1166 	if (n && n->type == NCMD && n->ncmd.args)
   1167 		if (goodname(n->ncmd.args->narg.text))
   1168 			find_command(n->ncmd.args->narg.text, &entry, 0,
   1169 				     pathval());
   1170 }
   1171 
   1172 STATIC int
   1173 in_function(void)
   1174 {
   1175 	return funcnest;
   1176 }
   1177 
   1178 STATIC enum skipstate
   1179 current_skipstate(void)
   1180 {
   1181 	return evalskip;
   1182 }
   1183 
   1184 STATIC void
   1185 stop_skipping(void)
   1186 {
   1187 	evalskip = SKIPNONE;
   1188 	skipcount = 0;
   1189 }
   1190 
   1191 /*
   1192  * Builtin commands.  Builtin commands whose functions are closely
   1193  * tied to evaluation are implemented here.
   1194  */
   1195 
   1196 /*
   1197  * No command given.
   1198  */
   1199 
   1200 int
   1201 bltincmd(int argc, char **argv)
   1202 {
   1203 	/*
   1204 	 * Preserve exitstatus of a previous possible redirection
   1205 	 * as POSIX mandates
   1206 	 */
   1207 	return back_exitstatus;
   1208 }
   1209 
   1210 
   1211 /*
   1212  * Handle break and continue commands.  Break, continue, and return are
   1213  * all handled by setting the evalskip flag.  The evaluation routines
   1214  * above all check this flag, and if it is set they start skipping
   1215  * commands rather than executing them.  The variable skipcount is
   1216  * the number of loops to break/continue, or the number of function
   1217  * levels to return.  (The latter is always 1.)  It should probably
   1218  * be an error to break out of more loops than exist, but it isn't
   1219  * in the standard shell so we don't make it one here.
   1220  */
   1221 
   1222 int
   1223 breakcmd(int argc, char **argv)
   1224 {
   1225 	int n = argc > 1 ? number(argv[1]) : 1;
   1226 
   1227 	if (n > loopnest)
   1228 		n = loopnest;
   1229 	if (n > 0) {
   1230 		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
   1231 		skipcount = n;
   1232 	}
   1233 	return 0;
   1234 }
   1235 
   1236 int
   1237 dotcmd(int argc, char **argv)
   1238 {
   1239 	exitstatus = 0;
   1240 
   1241 	if (argc >= 2) {		/* That's what SVR2 does */
   1242 		char *fullname;
   1243 		/*
   1244 		 * dot_funcnest needs to be 0 when not in a dotcmd, so it
   1245 		 * cannot be restored with (funcnest + 1).
   1246 		 */
   1247 		int dot_funcnest_old;
   1248 		struct stackmark smark;
   1249 
   1250 		setstackmark(&smark);
   1251 		fullname = find_dot_file(argv[1]);
   1252 		setinputfile(fullname, 1);
   1253 		commandname = fullname;
   1254 		dot_funcnest_old = dot_funcnest;
   1255 		dot_funcnest = funcnest + 1;
   1256 		cmdloop(0);
   1257 		dot_funcnest = dot_funcnest_old;
   1258 		popfile();
   1259 		popstackmark(&smark);
   1260 	}
   1261 	return exitstatus;
   1262 }
   1263 
   1264 /*
   1265  * Take commands from a file.  To be compatible we should do a path
   1266  * search for the file, which is necessary to find sub-commands.
   1267  */
   1268 
   1269 STATIC char *
   1270 find_dot_file(char *basename)
   1271 {
   1272 	char *fullname;
   1273 	const char *path = pathval();
   1274 	struct stat statb;
   1275 
   1276 	/* don't try this for absolute or relative paths */
   1277 	if (strchr(basename, '/'))
   1278 		return basename;
   1279 
   1280 	while ((fullname = padvance(&path, basename)) != NULL) {
   1281 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
   1282 			/*
   1283 			 * Don't bother freeing here, since it will
   1284 			 * be freed by the caller.
   1285 			 */
   1286 			return fullname;
   1287 		}
   1288 		stunalloc(fullname);
   1289 	}
   1290 
   1291 	/* not found in the PATH */
   1292 	error("%s: not found", basename);
   1293 	/* NOTREACHED */
   1294 }
   1295 
   1296 
   1297 
   1298 /*
   1299  * The return command.
   1300  *
   1301  * Quoth the POSIX standard:
   1302  *   The return utility shall cause the shell to stop executing the current
   1303  *   function or dot script. If the shell is not currently executing
   1304  *   a function or dot script, the results are unspecified.
   1305  *
   1306  * As for the unspecified part, there seems to be no de-facto standard: bash
   1307  * ignores the return with a warning, zsh ignores the return in interactive
   1308  * mode but seems to liken it to exit in a script.  (checked May 2014)
   1309  *
   1310  * We choose to silently ignore the return.  Older versions of this shell
   1311  * set evalskip to SKIPFILE causing the shell to (indirectly) exit.  This
   1312  * had at least the problem of circumventing the check for stopped jobs,
   1313  * which would occur for exit or ^D.
   1314  */
   1315 
   1316 int
   1317 returncmd(int argc, char **argv)
   1318 {
   1319 	int ret = argc > 1 ? number(argv[1]) : exitstatus;
   1320 
   1321 	if ((dot_funcnest == 0 && funcnest)
   1322 	    || (dot_funcnest > 0 && funcnest - (dot_funcnest - 1) > 0)) {
   1323 		evalskip = SKIPFUNC;
   1324 		skipcount = 1;
   1325 	} else if (dot_funcnest > 0) {
   1326 		evalskip = SKIPFILE;
   1327 		skipcount = 1;
   1328 	} else {
   1329 		/* XXX: should a warning be issued? */
   1330 		ret = 0;
   1331 	}
   1332 
   1333 	return ret;
   1334 }
   1335 
   1336 
   1337 int
   1338 falsecmd(int argc, char **argv)
   1339 {
   1340 	return 1;
   1341 }
   1342 
   1343 
   1344 int
   1345 truecmd(int argc, char **argv)
   1346 {
   1347 	return 0;
   1348 }
   1349 
   1350 
   1351 int
   1352 execcmd(int argc, char **argv)
   1353 {
   1354 	if (argc > 1) {
   1355 		struct strlist *sp;
   1356 
   1357 		iflag = 0;		/* exit on error */
   1358 		mflag = 0;
   1359 		optschanged();
   1360 		for (sp = cmdenviron; sp; sp = sp->next)
   1361 			setvareq(sp->text, VEXPORT|VSTACK);
   1362 		shellexec(argv + 1, environment(), pathval(), 0, 0);
   1363 	}
   1364 	return 0;
   1365 }
   1366 
   1367 static int
   1368 conv_time(clock_t ticks, char *seconds, size_t l)
   1369 {
   1370 	static clock_t tpm = 0;
   1371 	clock_t mins;
   1372 	int i;
   1373 
   1374 	if (!tpm)
   1375 		tpm = sysconf(_SC_CLK_TCK) * 60;
   1376 
   1377 	mins = ticks / tpm;
   1378 	snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
   1379 
   1380 	if (seconds[0] == '6' && seconds[1] == '0') {
   1381 		/* 59.99995 got rounded up... */
   1382 		mins++;
   1383 		strlcpy(seconds, "0.0", l);
   1384 		return mins;
   1385 	}
   1386 
   1387 	/* suppress trailing zeros */
   1388 	i = strlen(seconds) - 1;
   1389 	for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
   1390 		seconds[i] = 0;
   1391 	return mins;
   1392 }
   1393 
   1394 int
   1395 timescmd(int argc, char **argv)
   1396 {
   1397 	struct tms tms;
   1398 	int u, s, cu, cs;
   1399 	char us[8], ss[8], cus[8], css[8];
   1400 
   1401 	nextopt("");
   1402 
   1403 	times(&tms);
   1404 
   1405 	u = conv_time(tms.tms_utime, us, sizeof(us));
   1406 	s = conv_time(tms.tms_stime, ss, sizeof(ss));
   1407 	cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
   1408 	cs = conv_time(tms.tms_cstime, css, sizeof(css));
   1409 
   1410 	outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
   1411 		u, us, s, ss, cu, cus, cs, css);
   1412 
   1413 	return 0;
   1414 }
   1415