sh - Sync to FreeBSD d038ee76 part 1/2
[dragonfly.git] / bin / sh / eval.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
02d0b1ce 16 * 4. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
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
30 * SUCH DAMAGE.
31 */
32
02d0b1ce
MD
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD$");
f75716ef 40
984263bc
MD
41#include <paths.h>
42#include <signal.h>
f75716ef 43#include <stdlib.h>
984263bc 44#include <unistd.h>
02d0b1ce
MD
45#include <sys/resource.h>
46#include <sys/wait.h> /* For WIFSIGNALED(status) */
47#include <errno.h>
984263bc
MD
48
49/*
50 * Evaluate a command.
51 */
52
53#include "shell.h"
54#include "nodes.h"
55#include "syntax.h"
56#include "expand.h"
57#include "parser.h"
58#include "jobs.h"
59#include "eval.h"
60#include "builtins.h"
61#include "options.h"
62#include "exec.h"
63#include "redir.h"
64#include "input.h"
65#include "output.h"
66#include "trap.h"
67#include "var.h"
68#include "memalloc.h"
69#include "error.h"
70#include "show.h"
71#include "mystring.h"
72#ifndef NO_HISTORY
73#include "myhistedit.h"
74#endif
75
76
934863d7 77int evalskip; /* set if we are skipping commands */
a1cf61ab 78int skipcount; /* number of levels to skip */
b1abb130 79static int loopnest; /* current loop nesting level */
984263bc 80int funcnest; /* depth of function calls */
99512ac4 81static int builtin_flags; /* evalcommand flags for builtins */
984263bc
MD
82
83
02d0b1ce 84char *commandname;
984263bc
MD
85struct strlist *cmdenviron;
86int exitstatus; /* exit status of last command */
87int oexitstatus; /* saved exit status */
88
89
99512ac4
PA
90static void evalloop(union node *, int);
91static void evalfor(union node *, int);
b88d2faf 92static union node *evalcase(union node *);
99512ac4
PA
93static void evalsubshell(union node *, int);
94static void evalredir(union node *, int);
f00eae14 95static void exphere(union node *, struct arglist *);
99512ac4
PA
96static void expredir(union node *);
97static void evalpipe(union node *);
98static int is_valid_fast_cmdsubst(union node *n);
99static void evalcommand(union node *, int, struct backcmd *);
100static void prehash(union node *);
984263bc
MD
101
102
103/*
104 * Called to reset things after an exception.
105 */
106
b1abb130
PA
107void
108reseteval(void)
109{
984263bc
MD
110 evalskip = 0;
111 loopnest = 0;
984263bc 112}
984263bc
MD
113
114
115/*
116 * The eval command.
117 */
118
119int
120evalcmd(int argc, char **argv)
121{
122 char *p;
123 char *concat;
124 char **ap;
125
126 if (argc > 1) {
127 p = argv[1];
128 if (argc > 2) {
129 STARTSTACKSTR(concat);
130 ap = argv + 2;
131 for (;;) {
99512ac4 132 STPUTS(p, concat);
984263bc
MD
133 if ((p = *ap++) == NULL)
134 break;
135 STPUTC(' ', concat);
136 }
137 STPUTC('\0', concat);
138 p = grabstackstr(concat);
139 }
f15f3668 140 evalstring(p, builtin_flags);
99512ac4
PA
141 } else
142 exitstatus = 0;
984263bc
MD
143 return exitstatus;
144}
145
146
147/*
148 * Execute a command or commands contained in a string.
149 */
150
151void
02d0b1ce 152evalstring(const char *s, int flags)
984263bc
MD
153{
154 union node *n;
155 struct stackmark smark;
99512ac4
PA
156 int flags_exit;
157 int any;
984263bc 158
99512ac4
PA
159 flags_exit = flags & EV_EXIT;
160 flags &= ~EV_EXIT;
161 any = 0;
984263bc
MD
162 setstackmark(&smark);
163 setinputstring(s, 1);
164 while ((n = parsecmd(0)) != NEOF) {
276d61f5 165 if (n != NULL && !nflag) {
99512ac4
PA
166 if (flags_exit && preadateof())
167 evaltree(n, flags | EV_EXIT);
168 else
169 evaltree(n, flags);
170 any = 1;
02d0b1ce
MD
171 if (evalskip)
172 break;
99512ac4 173 }
984263bc 174 popstackmark(&smark);
f00eae14 175 setstackmark(&smark);
984263bc
MD
176 }
177 popfile();
178 popstackmark(&smark);
99512ac4
PA
179 if (!any)
180 exitstatus = 0;
181 if (flags_exit)
2b23d27f 182 exraise(EXEXIT);
984263bc
MD
183}
184
185
984263bc
MD
186/*
187 * Evaluate a parse tree. The value is left in the global variable
188 * exitstatus.
189 */
190
191void
192evaltree(union node *n, int flags)
193{
6a3f0d3d 194 int do_etest;
99512ac4 195 union node *next;
f00eae14 196 struct stackmark smark;
6a3f0d3d 197
f00eae14 198 setstackmark(&smark);
6a3f0d3d 199 do_etest = 0;
984263bc
MD
200 if (n == NULL) {
201 TRACE(("evaltree(NULL) called\n"));
202 exitstatus = 0;
203 goto out;
204 }
99512ac4
PA
205 do {
206 next = NULL;
984263bc 207#ifndef NO_HISTORY
99512ac4 208 displayhist = 1; /* show history substitutions done with fc */
984263bc 209#endif
99512ac4
PA
210 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
211 switch (n->type) {
212 case NSEMI:
213 evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
214 if (evalskip)
215 goto out;
216 next = n->nbinary.ch2;
217 break;
218 case NAND:
219 evaltree(n->nbinary.ch1, EV_TESTED);
220 if (evalskip || exitstatus != 0) {
221 goto out;
222 }
223 next = n->nbinary.ch2;
224 break;
225 case NOR:
226 evaltree(n->nbinary.ch1, EV_TESTED);
227 if (evalskip || exitstatus == 0)
228 goto out;
229 next = n->nbinary.ch2;
230 break;
231 case NREDIR:
232 evalredir(n, flags);
233 break;
234 case NSUBSHELL:
235 evalsubshell(n, flags);
236 do_etest = !(flags & EV_TESTED);
237 break;
238 case NBACKGND:
239 evalsubshell(n, flags);
240 break;
241 case NIF: {
242 evaltree(n->nif.test, EV_TESTED);
243 if (evalskip)
244 goto out;
245 if (exitstatus == 0)
246 next = n->nif.ifpart;
247 else if (n->nif.elsepart)
248 next = n->nif.elsepart;
249 else
250 exitstatus = 0;
251 break;
984263bc 252 }
99512ac4
PA
253 case NWHILE:
254 case NUNTIL:
255 evalloop(n, flags & ~EV_EXIT);
256 break;
257 case NFOR:
258 evalfor(n, flags & ~EV_EXIT);
259 break;
260 case NCASE:
b88d2faf
PA
261 next = evalcase(n);
262 break;
263 case NCLIST:
264 next = n->nclist.body;
265 break;
266 case NCLISTFALLTHRU:
267 if (n->nclist.body) {
268 evaltree(n->nclist.body, flags & ~EV_EXIT);
269 if (evalskip)
270 goto out;
271 }
272 next = n->nclist.next;
99512ac4
PA
273 break;
274 case NDEFUN:
275 defun(n->narg.text, n->narg.next);
984263bc 276 exitstatus = 0;
99512ac4
PA
277 break;
278 case NNOT:
279 evaltree(n->nnot.com, EV_TESTED);
b1abb130
PA
280 if (evalskip)
281 goto out;
99512ac4
PA
282 exitstatus = !exitstatus;
283 break;
284
285 case NPIPE:
286 evalpipe(n);
287 do_etest = !(flags & EV_TESTED);
288 break;
289 case NCMD:
02d0b1ce 290 evalcommand(n, flags, (struct backcmd *)NULL);
99512ac4
PA
291 do_etest = !(flags & EV_TESTED);
292 break;
293 default:
294 out1fmt("Node type = %d\n", n->type);
295 flushout(&output);
296 break;
297 }
298 n = next;
f00eae14
PA
299 popstackmark(&smark);
300 setstackmark(&smark);
99512ac4 301 } while (n != NULL);
984263bc 302out:
f00eae14
PA
303 popstackmark(&smark);
304 if (pendingsig)
984263bc 305 dotrap();
2b23d27f 306 if (eflag && exitstatus != 0 && do_etest)
984263bc 307 exitshell(exitstatus);
2b23d27f
PA
308 if (flags & EV_EXIT)
309 exraise(EXEXIT);
984263bc
MD
310}
311
312
99512ac4 313static void
6a3f0d3d 314evalloop(union node *n, int flags)
984263bc
MD
315{
316 int status;
317
318 loopnest++;
319 status = 0;
320 for (;;) {
02d0b1ce 321 if (!evalskip)
984263bc
MD
322 evaltree(n->nbinary.ch1, EV_TESTED);
323 if (evalskip) {
02d0b1ce 324 if (evalskip == SKIPCONT && --skipcount <= 0) {
984263bc
MD
325 evalskip = 0;
326 continue;
327 }
328 if (evalskip == SKIPBREAK && --skipcount <= 0)
329 evalskip = 0;
02d0b1ce 330 if (evalskip == SKIPRETURN)
99512ac4 331 status = exitstatus;
984263bc
MD
332 break;
333 }
334 if (n->type == NWHILE) {
335 if (exitstatus != 0)
336 break;
337 } else {
338 if (exitstatus == 0)
339 break;
340 }
6a3f0d3d 341 evaltree(n->nbinary.ch2, flags);
984263bc 342 status = exitstatus;
984263bc
MD
343 }
344 loopnest--;
345 exitstatus = status;
346}
347
348
349
99512ac4 350static void
6a3f0d3d 351evalfor(union node *n, int flags)
984263bc
MD
352{
353 struct arglist arglist;
354 union node *argp;
355 struct strlist *sp;
723ecb59 356 int status;
984263bc 357
984263bc
MD
358 arglist.lastp = &arglist.list;
359 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
360 oexitstatus = exitstatus;
361 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
984263bc
MD
362 }
363 *arglist.lastp = NULL;
364
984263bc 365 loopnest++;
723ecb59 366 status = 0;
984263bc
MD
367 for (sp = arglist.list ; sp ; sp = sp->next) {
368 setvar(n->nfor.var, sp->text, 0);
6a3f0d3d 369 evaltree(n->nfor.body, flags);
723ecb59 370 status = exitstatus;
984263bc
MD
371 if (evalskip) {
372 if (evalskip == SKIPCONT && --skipcount <= 0) {
373 evalskip = 0;
374 continue;
375 }
376 if (evalskip == SKIPBREAK && --skipcount <= 0)
377 evalskip = 0;
378 break;
379 }
380 }
381 loopnest--;
723ecb59 382 exitstatus = status;
984263bc
MD
383}
384
385
b88d2faf
PA
386/*
387 * Evaluate a case statement, returning the selected tree.
388 *
389 * The exit status needs care to get right.
390 */
984263bc 391
b3caee47 392static union node *
b88d2faf 393evalcase(union node *n)
984263bc
MD
394{
395 union node *cp;
396 union node *patp;
397 struct arglist arglist;
984263bc 398
984263bc
MD
399 arglist.lastp = &arglist.list;
400 oexitstatus = exitstatus;
401 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
b3caee47 402 for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
984263bc
MD
403 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
404 if (casematch(patp, arglist.list->text)) {
21f23b60 405 while (cp->nclist.next &&
b88d2faf
PA
406 cp->type == NCLISTFALLTHRU &&
407 cp->nclist.body == NULL)
21f23b60 408 cp = cp->nclist.next;
b88d2faf
PA
409 if (cp->nclist.next &&
410 cp->type == NCLISTFALLTHRU)
411 return (cp);
c79cd3b3
PA
412 if (cp->nclist.body == NULL)
413 exitstatus = 0;
b3caee47 414 return (cp->nclist.body);
984263bc
MD
415 }
416 }
417 }
c79cd3b3 418 exitstatus = 0;
b3caee47 419 return (NULL);
984263bc
MD
420}
421
422
423
424/*
425 * Kick off a subshell to evaluate a tree.
426 */
427
99512ac4 428static void
984263bc
MD
429evalsubshell(union node *n, int flags)
430{
431 struct job *jp;
432 int backgnd = (n->type == NBACKGND);
433
87838cc9 434 oexitstatus = exitstatus;
984263bc 435 expredir(n->nredir.redirect);
99512ac4
PA
436 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
437 forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
984263bc
MD
438 if (backgnd)
439 flags &=~ EV_TESTED;
440 redirect(n->nredir.redirect, 0);
441 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
99512ac4 442 } else if (!backgnd) {
984263bc 443 INTOFF;
02d0b1ce 444 exitstatus = waitforjob(jp, (int *)NULL);
984263bc 445 INTON;
5d4731c8
PA
446 } else
447 exitstatus = 0;
984263bc
MD
448}
449
450
99512ac4
PA
451/*
452 * Evaluate a redirected compound command.
453 */
454
455static void
456evalredir(union node *n, int flags)
457{
458 struct jmploc jmploc;
459 struct jmploc *savehandler;
460 volatile int in_redirect = 1;
461
87838cc9 462 oexitstatus = exitstatus;
99512ac4
PA
463 expredir(n->nredir.redirect);
464 savehandler = handler;
465 if (setjmp(jmploc.loc)) {
466 int e;
467
468 handler = savehandler;
469 e = exception;
2b23d27f 470 popredir();
99512ac4 471 if (e == EXERROR || e == EXEXEC) {
99512ac4
PA
472 if (in_redirect) {
473 exitstatus = 2;
474 return;
475 }
476 }
477 longjmp(handler->loc, 1);
478 } else {
479 INTOFF;
480 handler = &jmploc;
481 redirect(n->nredir.redirect, REDIR_PUSH);
482 in_redirect = 0;
483 INTON;
484 evaltree(n->nredir.n, flags);
485 }
486 INTOFF;
487 handler = savehandler;
488 popredir();
489 INTON;
490}
491
984263bc 492
f00eae14
PA
493static void
494exphere(union node *redir, struct arglist *fn)
495{
496 struct jmploc jmploc;
497 struct jmploc *savehandler;
498 struct localvar *savelocalvars;
499 int need_longjmp = 0;
500
02d0b1ce 501 redir->nhere.expdoc = "";
f00eae14
PA
502 savelocalvars = localvars;
503 localvars = NULL;
504 forcelocal++;
505 savehandler = handler;
506 if (setjmp(jmploc.loc))
507 need_longjmp = exception != EXERROR && exception != EXEXEC;
508 else {
509 handler = &jmploc;
510 expandarg(redir->nhere.doc, fn, 0);
511 redir->nhere.expdoc = fn->list->text;
512 INTOFF;
513 }
514 handler = savehandler;
515 forcelocal--;
516 poplocalvars();
517 localvars = savelocalvars;
518 if (need_longjmp)
519 longjmp(handler->loc, 1);
520 INTON;
521}
522
523
984263bc
MD
524/*
525 * Compute the names of the files in a redirection list.
526 */
527
99512ac4 528static void
984263bc
MD
529expredir(union node *n)
530{
531 union node *redir;
532
533 for (redir = n ; redir ; redir = redir->nfile.next) {
534 struct arglist fn;
535 fn.lastp = &fn.list;
984263bc
MD
536 switch (redir->type) {
537 case NFROM:
538 case NTO:
539 case NFROMTO:
540 case NAPPEND:
541 case NCLOBBER:
02d0b1ce 542 expandarg(redir->nfile.fname, &fn, EXP_TILDE);
984263bc
MD
543 redir->nfile.expfname = fn.list->text;
544 break;
545 case NFROMFD:
546 case NTOFD:
547 if (redir->ndup.vname) {
02d0b1ce 548 expandarg(redir->ndup.vname, &fn, EXP_TILDE);
984263bc
MD
549 fixredir(redir, fn.list->text, 1);
550 }
551 break;
f00eae14
PA
552 case NXHERE:
553 exphere(redir, &fn);
554 break;
984263bc
MD
555 }
556 }
557}
558
559
560
561/*
562 * Evaluate a pipeline. All the processes in the pipeline are children
563 * of the process creating the pipeline. (This differs from some versions
564 * of the shell, which make the last process in a pipeline the parent
565 * of all the rest.)
566 */
567
99512ac4 568static void
984263bc
MD
569evalpipe(union node *n)
570{
571 struct job *jp;
572 struct nodelist *lp;
573 int pipelen;
574 int prevfd;
575 int pip[2];
576
0d5aaed6 577 TRACE(("evalpipe(%p) called\n", (void *)n));
984263bc
MD
578 pipelen = 0;
579 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
580 pipelen++;
581 INTOFF;
582 jp = makejob(n, pipelen);
583 prevfd = -1;
584 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
585 prehash(lp->n);
586 pip[1] = -1;
587 if (lp->next) {
588 if (pipe(pip) < 0) {
42ccfa71
SS
589 if (prevfd >= 0)
590 close(prevfd);
984263bc
MD
591 error("Pipe call failed: %s", strerror(errno));
592 }
593 }
594 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
595 INTON;
596 if (prevfd > 0) {
0d5aaed6 597 dup2(prevfd, 0);
984263bc
MD
598 close(prevfd);
599 }
600 if (pip[1] >= 0) {
601 if (!(prevfd >= 0 && pip[0] == 0))
602 close(pip[0]);
603 if (pip[1] != 1) {
0d5aaed6 604 dup2(pip[1], 1);
984263bc
MD
605 close(pip[1]);
606 }
607 }
608 evaltree(lp->n, EV_EXIT);
609 }
610 if (prevfd >= 0)
611 close(prevfd);
612 prevfd = pip[0];
d23796a8
PA
613 if (pip[1] != -1)
614 close(pip[1]);
984263bc
MD
615 }
616 INTON;
617 if (n->npipe.backgnd == 0) {
618 INTOFF;
02d0b1ce 619 exitstatus = waitforjob(jp, (int *)NULL);
984263bc
MD
620 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
621 INTON;
5d4731c8
PA
622 } else
623 exitstatus = 0;
984263bc
MD
624}
625
626
627
99512ac4
PA
628static int
629is_valid_fast_cmdsubst(union node *n)
630{
99512ac4 631
32931063 632 return (n->type == NCMD);
99512ac4
PA
633}
634
984263bc
MD
635/*
636 * Execute a command inside back quotes. If it's a builtin command, we
637 * want to save its output in a block obtained from malloc. Otherwise
638 * we fork off a subprocess and get the output of the command via a pipe.
639 * Should be called with interrupts off.
640 */
641
642void
643evalbackcmd(union node *n, struct backcmd *result)
644{
645 int pip[2];
646 struct job *jp;
f00eae14 647 struct stackmark smark;
99512ac4
PA
648 struct jmploc jmploc;
649 struct jmploc *savehandler;
32931063 650 struct localvar *savelocalvars;
984263bc 651
984263bc
MD
652 result->fd = -1;
653 result->buf = NULL;
654 result->nleft = 0;
655 result->jp = NULL;
656 if (n == NULL) {
657 exitstatus = 0;
02d0b1ce 658 return;
984263bc 659 }
02d0b1ce 660 setstackmark(&smark);
f00eae14 661 exitstatus = oexitstatus;
99512ac4 662 if (is_valid_fast_cmdsubst(n)) {
32931063
PA
663 savelocalvars = localvars;
664 localvars = NULL;
665 forcelocal++;
99512ac4
PA
666 savehandler = handler;
667 if (setjmp(jmploc.loc)) {
668 if (exception == EXERROR || exception == EXEXEC)
669 exitstatus = 2;
670 else if (exception != 0) {
671 handler = savehandler;
32931063
PA
672 forcelocal--;
673 poplocalvars();
674 localvars = savelocalvars;
99512ac4
PA
675 longjmp(handler->loc, 1);
676 }
677 } else {
678 handler = &jmploc;
679 evalcommand(n, EV_BACKCMD, result);
680 }
681 handler = savehandler;
32931063
PA
682 forcelocal--;
683 poplocalvars();
684 localvars = savelocalvars;
984263bc 685 } else {
984263bc
MD
686 if (pipe(pip) < 0)
687 error("Pipe call failed: %s", strerror(errno));
688 jp = makejob(n, 1);
689 if (forkshell(jp, n, FORK_NOJOB) == 0) {
690 FORCEINTON;
691 close(pip[0]);
692 if (pip[1] != 1) {
0d5aaed6 693 dup2(pip[1], 1);
984263bc
MD
694 close(pip[1]);
695 }
696 evaltree(n, EV_EXIT);
697 }
698 close(pip[1]);
699 result->fd = pip[0];
700 result->jp = jp;
701 }
984263bc 702 popstackmark(&smark);
0d5aaed6 703 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
984263bc
MD
704 result->fd, result->buf, result->nleft, result->jp));
705}
706
f00eae14
PA
707static int
708mustexpandto(const char *argtext, const char *mask)
709{
710 for (;;) {
711 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
712 argtext++;
713 continue;
714 }
715 if (*argtext == CTLESC)
716 argtext++;
717 else if (BASESYNTAX[(int)*argtext] == CCTL)
718 return (0);
719 if (*argtext != *mask)
720 return (0);
721 if (*argtext == '\0')
722 return (1);
723 argtext++;
724 mask++;
725 }
726}
727
728static int
729isdeclarationcmd(struct narg *arg)
730{
731 int have_command = 0;
732
733 if (arg == NULL)
734 return (0);
735 while (mustexpandto(arg->text, "command")) {
736 have_command = 1;
737 arg = &arg->next->narg;
738 if (arg == NULL)
739 return (0);
740 /*
741 * To also allow "command -p" and "command --" as part of
742 * a declaration command, add code here.
743 * We do not do this, as ksh does not do it either and it
744 * is not required by POSIX.
745 */
746 }
747 return (mustexpandto(arg->text, "export") ||
748 mustexpandto(arg->text, "readonly") ||
749 (mustexpandto(arg->text, "local") &&
750 (have_command || !isfunc("local"))));
751}
752
02d0b1ce
MD
753static void
754xtracecommand(struct arglist *varlist, struct arglist *arglist)
755{
756 struct strlist *sp;
757 char sep = 0;
758 const char *p, *ps4;
759
760 ps4 = expandstr(ps4val());
761 out2str(ps4 != NULL ? ps4 : ps4val());
762 for (sp = varlist->list ; sp ; sp = sp->next) {
763 if (sep != 0)
764 out2c(' ');
765 p = strchr(sp->text, '=');
766 if (p != NULL) {
767 p++;
768 outbin(sp->text, p - sp->text, out2);
769 out2qstr(p);
770 } else
771 out2qstr(sp->text);
772 sep = ' ';
773 }
774 for (sp = arglist->list ; sp ; sp = sp->next) {
775 if (sep != 0)
776 out2c(' ');
777 out2qstr(sp->text);
778 sep = ' ';
779 }
780 out2c('\n');
781 flushout(&errout);
782}
783
99512ac4
PA
784/*
785 * Check if a builtin can safely be executed in the same process,
786 * even though it should be in a subshell (command substitution).
787 * Note that jobid, jobs, times and trap can show information not
788 * available in a child process; this is deliberate.
789 * The arguments should already have been expanded.
790 */
791static int
792safe_builtin(int idx, int argc, char **argv)
793{
794 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
795 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
796 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
797 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
798 idx == TYPECMD)
799 return (1);
800 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
801 idx == UMASKCMD)
802 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
803 if (idx == SETCMD)
804 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
805 argv[1][0] == '+') && argv[1][1] == 'o' &&
806 argv[1][2] == '\0'));
807 return (0);
808}
984263bc
MD
809
810/*
811 * Execute a simple command.
99512ac4 812 * Note: This may or may not return if (flags & EV_EXIT).
984263bc
MD
813 */
814
99512ac4 815static void
02d0b1ce 816evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
984263bc 817{
984263bc
MD
818 union node *argp;
819 struct arglist arglist;
820 struct arglist varlist;
02d0b1ce
MD
821 char **argv;
822 int argc;
984263bc
MD
823 char **envp;
824 int varflag;
825 struct strlist *sp;
826 int mode;
827 int pip[2];
828 struct cmdentry cmdentry;
02d0b1ce 829 struct job *jp;
984263bc 830 struct jmploc jmploc;
99512ac4 831 struct jmploc *savehandler;
02d0b1ce 832 char *savecmdname;
99512ac4
PA
833 struct shparam saveparam;
834 struct localvar *savelocalvars;
835 struct parsefile *savetopfile;
984263bc 836 volatile int e;
02d0b1ce 837 char *lastarg;
984263bc 838 int realstatus;
02d0b1ce 839 int do_clearcmdentry;
99512ac4 840 const char *path = pathval();
984263bc
MD
841
842 /* First expand the arguments. */
0d5aaed6 843 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
984263bc
MD
844 arglist.lastp = &arglist.list;
845 varlist.lastp = &varlist.list;
846 varflag = 1;
99512ac4 847 jp = NULL;
984263bc
MD
848 do_clearcmdentry = 0;
849 oexitstatus = exitstatus;
850 exitstatus = 0;
851 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
088f2cdc 852 if (varflag && isassignment(argp->narg.text)) {
f00eae14
PA
853 expandarg(argp, varflag == 1 ? &varlist : &arglist,
854 EXP_VARTILDE);
088f2cdc 855 continue;
f00eae14
PA
856 } else if (varflag == 1)
857 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
984263bc 858 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
984263bc
MD
859 }
860 *arglist.lastp = NULL;
861 *varlist.lastp = NULL;
862 expredir(cmd->ncmd.redirect);
863 argc = 0;
864 for (sp = arglist.list ; sp ; sp = sp->next)
865 argc++;
99512ac4
PA
866 /* Add one slot at the beginning for tryexec(). */
867 argv = stalloc(sizeof (char *) * (argc + 2));
868 argv++;
984263bc
MD
869
870 for (sp = arglist.list ; sp ; sp = sp->next) {
871 TRACE(("evalcommand arg: %s\n", sp->text));
872 *argv++ = sp->text;
873 }
874 *argv = NULL;
875 lastarg = NULL;
876 if (iflag && funcnest == 0 && argc > 0)
877 lastarg = argv[-1];
878 argv -= argc;
879
880 /* Print the command if xflag is set. */
02d0b1ce
MD
881 if (xflag)
882 xtracecommand(&varlist, &arglist);
984263bc
MD
883
884 /* Now locate the command. */
885 if (argc == 0) {
0d5aaed6 886 /* Variable assignment(s) without command */
984263bc
MD
887 cmdentry.cmdtype = CMDBUILTIN;
888 cmdentry.u.index = BLTINCMD;
99512ac4 889 cmdentry.special = 0;
984263bc
MD
890 } else {
891 static const char PATH[] = "PATH=";
99512ac4 892 int cmd_flags = 0, bltinonly = 0;
984263bc
MD
893
894 /*
895 * Modify the command lookup path, if a PATH= assignment
896 * is present
897 */
898 for (sp = varlist.list ; sp ; sp = sp->next)
899 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
900 path = sp->text + sizeof(PATH) - 1;
0d5aaed6 901 /*
984263bc
MD
902 * On `PATH=... command`, we need to make
903 * sure that the command isn't using the
904 * non-updated hash table of the outer PATH
0d5aaed6 905 * setting and we need to make sure that
984263bc
MD
906 * the hash table isn't filled with items
907 * from the temporary setting.
908 *
0d5aaed6 909 * It would be better to forbit using and
984263bc
MD
910 * updating the table while this command
911 * runs, by the command finding mechanism
912 * is heavily integrated with hash handling,
913 * so we just delete the hash before and after
914 * the command runs. Partly deleting like
915 * changepatch() does doesn't seem worth the
916 * bookinging effort, since most such runs add
0d5aaed6 917 * directories in front of the new PATH.
984263bc 918 */
99512ac4 919 clearcmdentry();
984263bc
MD
920 do_clearcmdentry = 1;
921 }
922
99512ac4
PA
923 for (;;) {
924 if (bltinonly) {
925 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
926 if (cmdentry.u.index < 0) {
927 cmdentry.u.index = BLTINCMD;
928 argv--;
929 argc++;
984263bc 930 break;
984263bc 931 }
99512ac4
PA
932 } else
933 find_command(argv[0], &cmdentry, cmd_flags, path);
934 /* implement the bltin and command builtins here */
935 if (cmdentry.cmdtype != CMDBUILTIN)
936 break;
937 if (cmdentry.u.index == BLTINCMD) {
938 if (argc == 1)
984263bc 939 break;
99512ac4
PA
940 argv++;
941 argc--;
942 bltinonly = 1;
943 } else if (cmdentry.u.index == COMMANDCMD) {
944 if (argc == 1)
945 break;
946 if (!strcmp(argv[1], "-p")) {
947 if (argc == 2)
948 break;
949 if (argv[2][0] == '-') {
950 if (strcmp(argv[2], "--"))
951 break;
952 if (argc == 3)
953 break;
954 argv += 3;
955 argc -= 3;
956 } else {
957 argv += 2;
958 argc -= 2;
959 }
960 path = _PATH_STDPATH;
961 clearcmdentry();
962 do_clearcmdentry = 1;
963 } else if (!strcmp(argv[1], "--")) {
964 if (argc == 2)
965 break;
966 argv += 2;
967 argc -= 2;
968 } else if (argv[1][0] == '-')
969 break;
970 else {
971 argv++;
972 argc--;
973 }
974 cmd_flags |= DO_NOFUNC;
975 bltinonly = 0;
976 } else
977 break;
984263bc 978 }
99512ac4
PA
979 /*
980 * Special builtins lose their special properties when
981 * called via 'command'.
982 */
983 if (cmd_flags & DO_NOFUNC)
984 cmdentry.special = 0;
984263bc
MD
985 }
986
987 /* Fork off a child process if necessary. */
df6b9adb 988 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
99512ac4 989 && ((flags & EV_EXIT) == 0 || have_traps()))
984263bc 990 || ((flags & EV_BACKCMD) != 0
99512ac4
PA
991 && (cmdentry.cmdtype != CMDBUILTIN ||
992 !safe_builtin(cmdentry.u.index, argc, argv)))) {
984263bc 993 jp = makejob(cmd, 1);
df6b9adb 994 mode = FORK_FG;
984263bc
MD
995 if (flags & EV_BACKCMD) {
996 mode = FORK_NOJOB;
997 if (pipe(pip) < 0)
998 error("Pipe call failed: %s", strerror(errno));
999 }
3fb5edd5
PA
1000 if (cmdentry.cmdtype == CMDNORMAL &&
1001 cmd->ncmd.redirect == NULL &&
1002 varlist.list == NULL &&
1003 (mode == FORK_FG || mode == FORK_NOJOB) &&
1004 !disvforkset() && !iflag && !mflag) {
1005 vforkexecshell(jp, argv, environment(), path,
1006 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
1007 goto parent;
1008 }
984263bc
MD
1009 if (forkshell(jp, cmd, mode) != 0)
1010 goto parent; /* at end of routine */
1011 if (flags & EV_BACKCMD) {
1012 FORCEINTON;
1013 close(pip[0]);
1014 if (pip[1] != 1) {
0d5aaed6 1015 dup2(pip[1], 1);
984263bc
MD
1016 close(pip[1]);
1017 }
f15f3668 1018 flags &= ~EV_BACKCMD;
984263bc
MD
1019 }
1020 flags |= EV_EXIT;
1021 }
1022
1023 /* This is the child process if a fork occurred. */
1024 /* Execute the command. */
1025 if (cmdentry.cmdtype == CMDFUNCTION) {
1026#ifdef DEBUG
1027 trputs("Shell function: "); trargs(argv);
1028#endif
984263bc
MD
1029 saveparam = shellparam;
1030 shellparam.malloc = 0;
1031 shellparam.reset = 1;
1032 shellparam.nparam = argc - 1;
1033 shellparam.p = argv + 1;
02d0b1ce 1034 shellparam.optp = NULL;
984263bc
MD
1035 shellparam.optnext = NULL;
1036 INTOFF;
1037 savelocalvars = localvars;
1038 localvars = NULL;
99512ac4
PA
1039 reffunc(cmdentry.u.func);
1040 savehandler = handler;
984263bc 1041 if (setjmp(jmploc.loc)) {
99512ac4
PA
1042 freeparam(&shellparam);
1043 shellparam = saveparam;
2b23d27f 1044 popredir();
99512ac4 1045 unreffunc(cmdentry.u.func);
984263bc
MD
1046 poplocalvars();
1047 localvars = savelocalvars;
99512ac4 1048 funcnest--;
984263bc
MD
1049 handler = savehandler;
1050 longjmp(handler->loc, 1);
1051 }
984263bc 1052 handler = &jmploc;
99512ac4
PA
1053 funcnest++;
1054 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1055 INTON;
984263bc
MD
1056 for (sp = varlist.list ; sp ; sp = sp->next)
1057 mklocal(sp->text);
99512ac4 1058 exitstatus = oexitstatus;
2b23d27f
PA
1059 evaltree(getfuncnode(cmdentry.u.func),
1060 flags & (EV_TESTED | EV_EXIT));
984263bc 1061 INTOFF;
99512ac4 1062 unreffunc(cmdentry.u.func);
984263bc
MD
1063 poplocalvars();
1064 localvars = savelocalvars;
1065 freeparam(&shellparam);
1066 shellparam = saveparam;
1067 handler = savehandler;
99512ac4 1068 funcnest--;
984263bc
MD
1069 popredir();
1070 INTON;
02d0b1ce 1071 if (evalskip == SKIPRETURN) {
984263bc
MD
1072 evalskip = 0;
1073 skipcount = 0;
1074 }
99512ac4 1075 if (jp)
984263bc
MD
1076 exitshell(exitstatus);
1077 } else if (cmdentry.cmdtype == CMDBUILTIN) {
1078#ifdef DEBUG
1079 trputs("builtin command: "); trargs(argv);
1080#endif
1081 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
1082 if (flags == EV_BACKCMD) {
1083 memout.nleft = 0;
1084 memout.nextc = memout.buf;
1085 memout.bufsize = 64;
1086 mode |= REDIR_BACKQ;
1087 }
984263bc 1088 savecmdname = commandname;
99512ac4 1089 savetopfile = getcurrentfile();
984263bc
MD
1090 cmdenviron = varlist.list;
1091 e = -1;
99512ac4 1092 savehandler = handler;
984263bc
MD
1093 if (setjmp(jmploc.loc)) {
1094 e = exception;
2b23d27f
PA
1095 if (e == EXINT)
1096 exitstatus = SIGINT+128;
1097 else if (e != EXEXIT)
1098 exitstatus = 2;
984263bc
MD
1099 goto cmddone;
1100 }
984263bc 1101 handler = &jmploc;
0d5aaed6 1102 redirect(cmd->ncmd.redirect, mode);
f00eae14 1103 outclearerror(out1);
99512ac4
PA
1104 /*
1105 * If there is no command word, redirection errors should
1106 * not be fatal but assignment errors should.
1107 */
d303b442 1108 if (argc == 0)
99512ac4
PA
1109 cmdentry.special = 1;
1110 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1111 if (argc > 0)
1112 bltinsetlocale();
984263bc
MD
1113 commandname = argv[0];
1114 argptr = argv + 1;
99512ac4
PA
1115 nextopt_optptr = NULL; /* initialize nextopt */
1116 builtin_flags = flags;
984263bc
MD
1117 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1118 flushall();
f00eae14
PA
1119 if (outiserror(out1)) {
1120 warning("write error on stdout");
1121 if (exitstatus == 0 || exitstatus == 1)
1122 exitstatus = 2;
1123 }
984263bc 1124cmddone:
99512ac4
PA
1125 if (argc > 0)
1126 bltinunsetlocale();
984263bc
MD
1127 cmdenviron = NULL;
1128 out1 = &output;
1129 out2 = &errout;
1130 freestdout();
984263bc 1131 handler = savehandler;
99512ac4
PA
1132 commandname = savecmdname;
1133 if (jp)
1134 exitshell(exitstatus);
984263bc
MD
1135 if (flags == EV_BACKCMD) {
1136 backcmd->buf = memout.buf;
1137 backcmd->nleft = memout.nextc - memout.buf;
1138 memout.buf = NULL;
1139 }
2b23d27f 1140 if (cmdentry.u.index != EXECCMD)
99512ac4
PA
1141 popredir();
1142 if (e != -1) {
1143 if ((e != EXERROR && e != EXEXEC)
1144 || cmdentry.special)
1145 exraise(e);
1146 popfilesupto(savetopfile);
1147 if (flags != EV_BACKCMD)
1148 FORCEINTON;
1149 }
984263bc
MD
1150 } else {
1151#ifdef DEBUG
1152 trputs("normal command: "); trargs(argv);
1153#endif
984263bc
MD
1154 redirect(cmd->ncmd.redirect, 0);
1155 for (sp = varlist.list ; sp ; sp = sp->next)
1156 setvareq(sp->text, VEXPORT|VSTACK);
1157 envp = environment();
99512ac4 1158 shellexec(argv, envp, path, cmdentry.u.index);
984263bc
MD
1159 /*NOTREACHED*/
1160 }
1161 goto out;
1162
1163parent: /* parent process gets here (if we forked) */
99512ac4 1164 if (mode == FORK_FG) { /* argument to fork */
984263bc
MD
1165 INTOFF;
1166 exitstatus = waitforjob(jp, &realstatus);
1167 INTON;
1168 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
1169 evalskip = SKIPBREAK;
1170 skipcount = loopnest;
1171 }
99512ac4 1172 } else if (mode == FORK_NOJOB) {
984263bc
MD
1173 backcmd->fd = pip[0];
1174 close(pip[1]);
1175 backcmd->jp = jp;
df6b9adb 1176 }
984263bc
MD
1177
1178out:
1179 if (lastarg)
1180 setvar("_", lastarg, 0);
1181 if (do_clearcmdentry)
99512ac4 1182 clearcmdentry();
984263bc
MD
1183}
1184
1185
1186
1187/*
1188 * Search for a command. This is called before we fork so that the
1189 * location of the command will be available in the parent as well as
1190 * the child. The check for "goodname" is an overly conservative
1191 * check that the name will not be subject to expansion.
1192 */
1193
99512ac4 1194static void
984263bc
MD
1195prehash(union node *n)
1196{
1197 struct cmdentry entry;
1198
0d5aaed6 1199 if (n && n->type == NCMD && n->ncmd.args)
984263bc
MD
1200 if (goodname(n->ncmd.args->narg.text))
1201 find_command(n->ncmd.args->narg.text, &entry, 0,
1202 pathval());
1203}
1204
1205
1206
1207/*
1208 * Builtin commands. Builtin commands whose functions are closely
1209 * tied to evaluation are implemented here.
1210 */
1211
1212/*
99512ac4
PA
1213 * No command given, a bltin command with no arguments, or a bltin command
1214 * with an invalid name.
984263bc
MD
1215 */
1216
1217int
99512ac4 1218bltincmd(int argc, char **argv)
984263bc 1219{
99512ac4
PA
1220 if (argc > 1) {
1221 out2fmt_flush("%s: not found\n", argv[1]);
1222 return 127;
1223 }
984263bc
MD
1224 /*
1225 * Preserve exitstatus of a previous possible redirection
1226 * as POSIX mandates
1227 */
1228 return exitstatus;
1229}
1230
1231
1232/*
1233 * Handle break and continue commands. Break, continue, and return are
1234 * all handled by setting the evalskip flag. The evaluation routines
1235 * above all check this flag, and if it is set they start skipping
1236 * commands rather than executing them. The variable skipcount is
1237 * the number of loops to break/continue, or the number of function
1238 * levels to return. (The latter is always 1.) It should probably
1239 * be an error to break out of more loops than exist, but it isn't
1240 * in the standard shell so we don't make it one here.
1241 */
1242
1243int
1244breakcmd(int argc, char **argv)
1245{
02d0b1ce
MD
1246 long n;
1247 char *end;
984263bc 1248
02d0b1ce
MD
1249 if (argc > 1) {
1250 /* Allow arbitrarily large numbers. */
1251 n = strtol(argv[1], &end, 10);
1252 if (!is_digit(argv[1][0]) || *end != '\0')
1253 error("Illegal number: %s", argv[1]);
1254 } else
1255 n = 1;
984263bc
MD
1256 if (n > loopnest)
1257 n = loopnest;
1258 if (n > 0) {
1259 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1260 skipcount = n;
1261 }
1262 return 0;
1263}
1264
1265/*
1266 * The `command' command.
1267 */
1268int
f00eae14 1269commandcmd(int argc __unused, char **argv __unused)
984263bc 1270{
99512ac4 1271 const char *path;
984263bc 1272 int ch;
0d5aaed6 1273 int cmd = -1;
984263bc 1274
99512ac4 1275 path = bltinlookup("PATH", 1);
984263bc 1276
f00eae14 1277 while ((ch = nextopt("pvV")) != '\0') {
984263bc
MD
1278 switch (ch) {
1279 case 'p':
99512ac4 1280 path = _PATH_STDPATH;
984263bc 1281 break;
0d5aaed6
PA
1282 case 'v':
1283 cmd = TYPECMD_SMALLV;
1284 break;
1285 case 'V':
1286 cmd = TYPECMD_BIGV;
1287 break;
984263bc
MD
1288 }
1289 }
984263bc 1290
0d5aaed6 1291 if (cmd != -1) {
f00eae14 1292 if (*argptr == NULL || argptr[1] != NULL)
0d5aaed6 1293 error("wrong number of arguments");
f00eae14 1294 return typecmd_impl(2, argptr - 1, cmd, path);
984263bc 1295 }
f00eae14 1296 if (*argptr != NULL)
99512ac4 1297 error("commandcmd bad call");
984263bc
MD
1298
1299 /*
1300 * Do nothing successfully if no command was specified;
1301 * ksh also does this.
1302 */
02d0b1ce 1303 return 0;
984263bc
MD
1304}
1305
1306
1307/*
1308 * The return command.
1309 */
1310
1311int
1312returncmd(int argc, char **argv)
1313{
1314 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1315
02d0b1ce 1316 evalskip = SKIPRETURN;
984263bc 1317 skipcount = 1;
984263bc
MD
1318 return ret;
1319}
1320
1321
1322int
1323falsecmd(int argc __unused, char **argv __unused)
1324{
1325 return 1;
1326}
1327
1328
1329int
1330truecmd(int argc __unused, char **argv __unused)
1331{
1332 return 0;
1333}
1334
1335
1336int
1337execcmd(int argc, char **argv)
1338{
99512ac4
PA
1339 /*
1340 * Because we have historically not supported any options,
1341 * only treat "--" specially.
1342 */
1343 if (argc > 1 && strcmp(argv[1], "--") == 0)
1344 argc--, argv++;
984263bc
MD
1345 if (argc > 1) {
1346 struct strlist *sp;
1347
1348 iflag = 0; /* exit on error */
1349 mflag = 0;
1350 optschanged();
1351 for (sp = cmdenviron; sp ; sp = sp->next)
1352 setvareq(sp->text, VEXPORT|VSTACK);
1353 shellexec(argv + 1, environment(), pathval(), 0);
1354
1355 }
1356 return 0;
1357}
0d5aaed6
PA
1358
1359
1360int
1361timescmd(int argc __unused, char **argv __unused)
1362{
1363 struct rusage ru;
1364 long shumins, shsmins, chumins, chsmins;
1365 double shusecs, shssecs, chusecs, chssecs;
1366
1367 if (getrusage(RUSAGE_SELF, &ru) < 0)
1368 return 1;
1369 shumins = ru.ru_utime.tv_sec / 60;
1370 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1371 shsmins = ru.ru_stime.tv_sec / 60;
1372 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1373 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1374 return 1;
1375 chumins = ru.ru_utime.tv_sec / 60;
1376 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1377 chsmins = ru.ru_stime.tv_sec / 60;
1378 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1379 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1380 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);
1381 return 0;
1382}