3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
45 #include <sys/resource.h>
46 #include <sys/wait.h> /* For WIFSIGNALED(status) */
73 #include "myhistedit.h"
77 int evalskip; /* set if we are skipping commands */
78 int skipcount; /* number of levels to skip */
79 MKINIT int loopnest; /* current loop nesting level */
80 int funcnest; /* depth of function calls */
81 static int builtin_flags; /* evalcommand flags for builtins */
85 struct strlist *cmdenviron;
86 int exitstatus; /* exit status of last command */
87 int oexitstatus; /* saved exit status */
90 static void evalloop(union node *, int);
91 static void evalfor(union node *, int);
92 static void evalcase(union node *, int);
93 static void evalsubshell(union node *, int);
94 static void evalredir(union node *, int);
95 static void expredir(union node *);
96 static void evalpipe(union node *);
97 static int is_valid_fast_cmdsubst(union node *n);
98 static void evalcommand(union node *, int, struct backcmd *);
99 static void prehash(union node *);
103 * Called to reset things after an exception.
123 evalcmd(int argc, char **argv)
132 STARTSTACKSTR(concat);
136 if ((p = *ap++) == NULL)
140 STPUTC('\0', concat);
141 p = grabstackstr(concat);
143 evalstring(p, builtin_flags);
151 * Execute a command or commands contained in a string.
155 evalstring(char *s, int flags)
158 struct stackmark smark;
162 flags_exit = flags & EV_EXIT;
165 setstackmark(&smark);
166 setinputstring(s, 1);
167 while ((n = parsecmd(0)) != NEOF) {
168 if (n != NULL && !nflag) {
169 if (flags_exit && preadateof())
170 evaltree(n, flags | EV_EXIT);
175 popstackmark(&smark);
178 popstackmark(&smark);
187 * Evaluate a parse tree. The value is left in the global variable
192 evaltree(union node *n, int flags)
199 TRACE(("evaltree(NULL) called\n"));
206 displayhist = 1; /* show history substitutions done with fc */
208 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
211 evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
214 next = n->nbinary.ch2;
217 evaltree(n->nbinary.ch1, EV_TESTED);
218 if (evalskip || exitstatus != 0) {
221 next = n->nbinary.ch2;
224 evaltree(n->nbinary.ch1, EV_TESTED);
225 if (evalskip || exitstatus == 0)
227 next = n->nbinary.ch2;
233 evalsubshell(n, flags);
234 do_etest = !(flags & EV_TESTED);
237 evalsubshell(n, flags);
240 evaltree(n->nif.test, EV_TESTED);
244 next = n->nif.ifpart;
245 else if (n->nif.elsepart)
246 next = n->nif.elsepart;
253 evalloop(n, flags & ~EV_EXIT);
256 evalfor(n, flags & ~EV_EXIT);
262 defun(n->narg.text, n->narg.next);
266 evaltree(n->nnot.com, EV_TESTED);
267 exitstatus = !exitstatus;
272 do_etest = !(flags & EV_TESTED);
275 evalcommand(n, flags, (struct backcmd *)NULL);
276 do_etest = !(flags & EV_TESTED);
279 out1fmt("Node type = %d\n", n->type);
288 if (eflag && exitstatus != 0 && do_etest)
289 exitshell(exitstatus);
296 evalloop(union node *n, int flags)
303 evaltree(n->nbinary.ch1, EV_TESTED);
305 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
309 if (evalskip == SKIPBREAK && --skipcount <= 0)
311 if (evalskip == SKIPFUNC || evalskip == SKIPFILE)
315 if (n->type == NWHILE) {
322 evaltree(n->nbinary.ch2, flags);
334 evalfor(union node *n, int flags)
336 struct arglist arglist;
339 struct stackmark smark;
341 setstackmark(&smark);
342 arglist.lastp = &arglist.list;
343 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
344 oexitstatus = exitstatus;
345 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
349 *arglist.lastp = NULL;
353 for (sp = arglist.list ; sp ; sp = sp->next) {
354 setvar(n->nfor.var, sp->text, 0);
355 evaltree(n->nfor.body, flags);
357 if (evalskip == SKIPCONT && --skipcount <= 0) {
361 if (evalskip == SKIPBREAK && --skipcount <= 0)
368 popstackmark(&smark);
374 evalcase(union node *n, int flags)
378 struct arglist arglist;
379 struct stackmark smark;
381 setstackmark(&smark);
382 arglist.lastp = &arglist.list;
383 oexitstatus = exitstatus;
385 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
386 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
387 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
388 if (casematch(patp, arglist.list->text)) {
389 while (cp->nclist.next &&
390 cp->type == NCLISTFALLTHRU) {
393 evaltree(cp->nclist.body,
395 cp = cp->nclist.next;
398 evaltree(cp->nclist.body, flags);
405 popstackmark(&smark);
411 * Kick off a subshell to evaluate a tree.
415 evalsubshell(union node *n, int flags)
418 int backgnd = (n->type == NBACKGND);
420 oexitstatus = exitstatus;
421 expredir(n->nredir.redirect);
422 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
423 forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
426 redirect(n->nredir.redirect, 0);
427 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
428 } else if (! backgnd) {
430 exitstatus = waitforjob(jp, (int *)NULL);
438 * Evaluate a redirected compound command.
442 evalredir(union node *n, int flags)
444 struct jmploc jmploc;
445 struct jmploc *savehandler;
446 volatile int in_redirect = 1;
448 oexitstatus = exitstatus;
449 expredir(n->nredir.redirect);
450 savehandler = handler;
451 if (setjmp(jmploc.loc)) {
454 handler = savehandler;
457 if (e == EXERROR || e == EXEXEC) {
463 longjmp(handler->loc, 1);
467 redirect(n->nredir.redirect, REDIR_PUSH);
470 evaltree(n->nredir.n, flags);
473 handler = savehandler;
480 * Compute the names of the files in a redirection list.
484 expredir(union node *n)
488 for (redir = n ; redir ; redir = redir->nfile.next) {
491 switch (redir->type) {
497 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
498 redir->nfile.expfname = fn.list->text;
502 if (redir->ndup.vname) {
503 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
504 fixredir(redir, fn.list->text, 1);
514 * Evaluate a pipeline. All the processes in the pipeline are children
515 * of the process creating the pipeline. (This differs from some versions
516 * of the shell, which make the last process in a pipeline the parent
521 evalpipe(union node *n)
529 TRACE(("evalpipe(%p) called\n", (void *)n));
531 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
534 jp = makejob(n, pipelen);
536 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
542 error("Pipe call failed: %s", strerror(errno));
545 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
552 if (!(prevfd >= 0 && pip[0] == 0))
559 evaltree(lp->n, EV_EXIT);
568 if (n->npipe.backgnd == 0) {
570 exitstatus = waitforjob(jp, (int *)NULL);
571 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
580 is_valid_fast_cmdsubst(union node *n)
583 return (n->type == NCMD);
587 * Execute a command inside back quotes. If it's a builtin command, we
588 * want to save its output in a block obtained from malloc. Otherwise
589 * we fork off a subprocess and get the output of the command via a pipe.
590 * Should be called with interrupts off.
594 evalbackcmd(union node *n, struct backcmd *result)
598 struct stackmark smark; /* unnecessary */
599 struct jmploc jmploc;
600 struct jmploc *savehandler;
601 struct localvar *savelocalvars;
603 setstackmark(&smark);
612 if (is_valid_fast_cmdsubst(n)) {
613 exitstatus = oexitstatus;
614 savelocalvars = localvars;
617 savehandler = handler;
618 if (setjmp(jmploc.loc)) {
619 if (exception == EXERROR || exception == EXEXEC)
621 else if (exception != 0) {
622 handler = savehandler;
625 localvars = savelocalvars;
626 longjmp(handler->loc, 1);
630 evalcommand(n, EV_BACKCMD, result);
632 handler = savehandler;
635 localvars = savelocalvars;
639 error("Pipe call failed: %s", strerror(errno));
641 if (forkshell(jp, n, FORK_NOJOB) == 0) {
648 evaltree(n, EV_EXIT);
655 popstackmark(&smark);
656 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
657 result->fd, result->buf, result->nleft, result->jp));
661 * Check if a builtin can safely be executed in the same process,
662 * even though it should be in a subshell (command substitution).
663 * Note that jobid, jobs, times and trap can show information not
664 * available in a child process; this is deliberate.
665 * The arguments should already have been expanded.
668 safe_builtin(int idx, int argc, char **argv)
670 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
671 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
672 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
673 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
676 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
678 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
680 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
681 argv[1][0] == '+') && argv[1][1] == 'o' &&
682 argv[1][2] == '\0'));
687 * Execute a simple command.
688 * Note: This may or may not return if (flags & EV_EXIT).
692 evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
694 struct stackmark smark;
696 struct arglist arglist;
697 struct arglist varlist;
705 struct cmdentry cmdentry;
707 struct jmploc jmploc;
708 struct jmploc *savehandler;
710 struct shparam saveparam;
711 struct localvar *savelocalvars;
712 struct parsefile *savetopfile;
716 int do_clearcmdentry;
717 const char *path = pathval();
719 /* First expand the arguments. */
720 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
721 setstackmark(&smark);
722 arglist.lastp = &arglist.list;
723 varlist.lastp = &varlist.list;
726 do_clearcmdentry = 0;
727 oexitstatus = exitstatus;
729 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
730 if (varflag && isassignment(argp->narg.text)) {
731 expandarg(argp, &varlist, EXP_VARTILDE);
734 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
737 *arglist.lastp = NULL;
738 *varlist.lastp = NULL;
739 expredir(cmd->ncmd.redirect);
741 for (sp = arglist.list ; sp ; sp = sp->next)
743 /* Add one slot at the beginning for tryexec(). */
744 argv = stalloc(sizeof (char *) * (argc + 2));
747 for (sp = arglist.list ; sp ; sp = sp->next) {
748 TRACE(("evalcommand arg: %s\n", sp->text));
753 if (iflag && funcnest == 0 && argc > 0)
757 /* Print the command if xflag is set. */
761 ps4 = expandstr(ps4val());
762 out2str(ps4 != NULL ? ps4 : ps4val());
763 for (sp = varlist.list ; sp ; sp = sp->next) {
766 p = strchr(sp->text, '=');
769 outbin(sp->text, p - sp->text, out2);
775 for (sp = arglist.list ; sp ; sp = sp->next) {
778 /* Disambiguate command looking like assignment. */
779 if (sp == arglist.list &&
780 strchr(sp->text, '=') != NULL &&
781 strchr(sp->text, '\'') == NULL) {
793 /* Now locate the command. */
795 /* Variable assignment(s) without command */
796 cmdentry.cmdtype = CMDBUILTIN;
797 cmdentry.u.index = BLTINCMD;
798 cmdentry.special = 0;
800 static const char PATH[] = "PATH=";
801 int cmd_flags = 0, bltinonly = 0;
804 * Modify the command lookup path, if a PATH= assignment
807 for (sp = varlist.list ; sp ; sp = sp->next)
808 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
809 path = sp->text + sizeof(PATH) - 1;
811 * On `PATH=... command`, we need to make
812 * sure that the command isn't using the
813 * non-updated hash table of the outer PATH
814 * setting and we need to make sure that
815 * the hash table isn't filled with items
816 * from the temporary setting.
818 * It would be better to forbit using and
819 * updating the table while this command
820 * runs, by the command finding mechanism
821 * is heavily integrated with hash handling,
822 * so we just delete the hash before and after
823 * the command runs. Partly deleting like
824 * changepatch() does doesn't seem worth the
825 * bookinging effort, since most such runs add
826 * directories in front of the new PATH.
829 do_clearcmdentry = 1;
834 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
835 if (cmdentry.u.index < 0) {
836 cmdentry.u.index = BLTINCMD;
842 find_command(argv[0], &cmdentry, cmd_flags, path);
843 /* implement the bltin and command builtins here */
844 if (cmdentry.cmdtype != CMDBUILTIN)
846 if (cmdentry.u.index == BLTINCMD) {
852 } else if (cmdentry.u.index == COMMANDCMD) {
855 if (!strcmp(argv[1], "-p")) {
858 if (argv[2][0] == '-') {
859 if (strcmp(argv[2], "--"))
869 path = _PATH_STDPATH;
871 do_clearcmdentry = 1;
872 } else if (!strcmp(argv[1], "--")) {
877 } else if (argv[1][0] == '-')
883 cmd_flags |= DO_NOFUNC;
889 * Special builtins lose their special properties when
890 * called via 'command'.
892 if (cmd_flags & DO_NOFUNC)
893 cmdentry.special = 0;
896 /* Fork off a child process if necessary. */
897 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
898 && ((flags & EV_EXIT) == 0 || have_traps()))
899 || ((flags & EV_BACKCMD) != 0
900 && (cmdentry.cmdtype != CMDBUILTIN ||
901 !safe_builtin(cmdentry.u.index, argc, argv)))) {
902 jp = makejob(cmd, 1);
904 if (flags & EV_BACKCMD) {
907 error("Pipe call failed: %s", strerror(errno));
909 if (cmdentry.cmdtype == CMDNORMAL &&
910 cmd->ncmd.redirect == NULL &&
911 varlist.list == NULL &&
912 (mode == FORK_FG || mode == FORK_NOJOB) &&
913 !disvforkset() && !iflag && !mflag) {
914 vforkexecshell(jp, argv, environment(), path,
915 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
918 if (forkshell(jp, cmd, mode) != 0)
919 goto parent; /* at end of routine */
920 if (flags & EV_BACKCMD) {
927 flags &= ~EV_BACKCMD;
932 /* This is the child process if a fork occurred. */
933 /* Execute the command. */
934 if (cmdentry.cmdtype == CMDFUNCTION) {
936 trputs("Shell function: "); trargs(argv);
938 saveparam = shellparam;
939 shellparam.malloc = 0;
940 shellparam.reset = 1;
941 shellparam.nparam = argc - 1;
942 shellparam.p = argv + 1;
943 shellparam.optnext = NULL;
945 savelocalvars = localvars;
947 reffunc(cmdentry.u.func);
948 savehandler = handler;
949 if (setjmp(jmploc.loc)) {
950 freeparam(&shellparam);
951 shellparam = saveparam;
953 unreffunc(cmdentry.u.func);
955 localvars = savelocalvars;
957 handler = savehandler;
958 longjmp(handler->loc, 1);
962 redirect(cmd->ncmd.redirect, REDIR_PUSH);
964 for (sp = varlist.list ; sp ; sp = sp->next)
966 exitstatus = oexitstatus;
967 evaltree(getfuncnode(cmdentry.u.func),
968 flags & (EV_TESTED | EV_EXIT));
970 unreffunc(cmdentry.u.func);
972 localvars = savelocalvars;
973 freeparam(&shellparam);
974 shellparam = saveparam;
975 handler = savehandler;
979 if (evalskip == SKIPFUNC) {
984 exitshell(exitstatus);
985 } else if (cmdentry.cmdtype == CMDBUILTIN) {
987 trputs("builtin command: "); trargs(argv);
989 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
990 if (flags == EV_BACKCMD) {
992 memout.nextc = memout.buf;
996 savecmdname = commandname;
997 savetopfile = getcurrentfile();
998 cmdenviron = varlist.list;
1000 savehandler = handler;
1001 if (setjmp(jmploc.loc)) {
1004 exitstatus = SIGINT+128;
1005 else if (e != EXEXIT)
1010 redirect(cmd->ncmd.redirect, mode);
1012 * If there is no command word, redirection errors should
1013 * not be fatal but assignment errors should.
1016 cmdentry.special = 1;
1017 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1020 commandname = argv[0];
1022 nextopt_optptr = NULL; /* initialize nextopt */
1023 builtin_flags = flags;
1024 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1033 handler = savehandler;
1034 commandname = savecmdname;
1036 exitshell(exitstatus);
1037 if (flags == EV_BACKCMD) {
1038 backcmd->buf = memout.buf;
1039 backcmd->nleft = memout.nextc - memout.buf;
1042 if (cmdentry.u.index != EXECCMD)
1045 if ((e != EXERROR && e != EXEXEC)
1046 || cmdentry.special)
1048 popfilesupto(savetopfile);
1049 if (flags != EV_BACKCMD)
1054 trputs("normal command: "); trargs(argv);
1056 redirect(cmd->ncmd.redirect, 0);
1057 for (sp = varlist.list ; sp ; sp = sp->next)
1058 setvareq(sp->text, VEXPORT|VSTACK);
1059 envp = environment();
1060 shellexec(argv, envp, path, cmdentry.u.index);
1065 parent: /* parent process gets here (if we forked) */
1066 if (mode == FORK_FG) { /* argument to fork */
1068 exitstatus = waitforjob(jp, &realstatus);
1070 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
1071 evalskip = SKIPBREAK;
1072 skipcount = loopnest;
1074 } else if (mode == FORK_NOJOB) {
1075 backcmd->fd = pip[0];
1082 setvar("_", lastarg, 0);
1083 if (do_clearcmdentry)
1085 popstackmark(&smark);
1091 * Search for a command. This is called before we fork so that the
1092 * location of the command will be available in the parent as well as
1093 * the child. The check for "goodname" is an overly conservative
1094 * check that the name will not be subject to expansion.
1098 prehash(union node *n)
1100 struct cmdentry entry;
1102 if (n && n->type == NCMD && n->ncmd.args)
1103 if (goodname(n->ncmd.args->narg.text))
1104 find_command(n->ncmd.args->narg.text, &entry, 0,
1111 * Builtin commands. Builtin commands whose functions are closely
1112 * tied to evaluation are implemented here.
1116 * No command given, a bltin command with no arguments, or a bltin command
1117 * with an invalid name.
1121 bltincmd(int argc, char **argv)
1124 out2fmt_flush("%s: not found\n", argv[1]);
1128 * Preserve exitstatus of a previous possible redirection
1136 * Handle break and continue commands. Break, continue, and return are
1137 * all handled by setting the evalskip flag. The evaluation routines
1138 * above all check this flag, and if it is set they start skipping
1139 * commands rather than executing them. The variable skipcount is
1140 * the number of loops to break/continue, or the number of function
1141 * levels to return. (The latter is always 1.) It should probably
1142 * be an error to break out of more loops than exist, but it isn't
1143 * in the standard shell so we don't make it one here.
1147 breakcmd(int argc, char **argv)
1149 int n = argc > 1 ? number(argv[1]) : 1;
1154 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1161 * The `command' command.
1164 commandcmd(int argc, char **argv)
1170 path = bltinlookup("PATH", 1);
1172 optind = optreset = 1;
1174 while ((ch = getopt(argc, argv, "pvV")) != -1) {
1177 path = _PATH_STDPATH;
1180 cmd = TYPECMD_SMALLV;
1187 error("unknown option: -%c", optopt);
1195 error("wrong number of arguments");
1196 return typecmd_impl(2, argv - 1, cmd, path);
1199 error("commandcmd bad call");
1202 * Do nothing successfully if no command was specified;
1203 * ksh also does this.
1210 * The return command.
1214 returncmd(int argc, char **argv)
1216 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1219 evalskip = SKIPFUNC;
1222 /* skip the rest of the file */
1223 evalskip = SKIPFILE;
1231 falsecmd(int argc __unused, char **argv __unused)
1238 truecmd(int argc __unused, char **argv __unused)
1245 execcmd(int argc, char **argv)
1248 * Because we have historically not supported any options,
1249 * only treat "--" specially.
1251 if (argc > 1 && strcmp(argv[1], "--") == 0)
1256 iflag = 0; /* exit on error */
1259 for (sp = cmdenviron; sp ; sp = sp->next)
1260 setvareq(sp->text, VEXPORT|VSTACK);
1261 shellexec(argv + 1, environment(), pathval(), 0);
1269 timescmd(int argc __unused, char **argv __unused)
1272 long shumins, shsmins, chumins, chsmins;
1273 double shusecs, shssecs, chusecs, chssecs;
1275 if (getrusage(RUSAGE_SELF, &ru) < 0)
1277 shumins = ru.ru_utime.tv_sec / 60;
1278 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1279 shsmins = ru.ru_stime.tv_sec / 60;
1280 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1281 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1283 chumins = ru.ru_utime.tv_sec / 60;
1284 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1285 chsmins = ru.ru_stime.tv_sec / 60;
1286 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1287 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1288 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);