VirtualBox

source: kBuild/trunk/src/kash/eval.c@ 3505

Last change on this file since 3505 was 3477, checked in by bird, 4 years ago

kash: Use kHlpAssert instead of assert.h (debugger stops on the assertion rather than at exit process code).

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette