Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature-vt/sys/src/ape/cmd/pdksh/c_sh.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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