xref: /csrg-svn/lib/libc/gen/times.c (revision 45144)
145139Sbostic /*-
245139Sbostic  * Copyright (c) 1990 The Regents of the University of California.
345139Sbostic  * All rights reserved.
445139Sbostic  *
545139Sbostic  * %sccs.include.redist.c%
621334Sdist  */
712843Ssam 
826521Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*45144Sbostic static char sccsid[] = "@(#)times.c	5.4 (Berkeley) 08/27/90";
1045139Sbostic #endif /* LIBC_SCCS and not lint */
1121334Sdist 
12*45144Sbostic #include <sys/param.h>
1312843Ssam #include <sys/time.h>
1445139Sbostic #include <sys/times.h>
1512843Ssam #include <sys/resource.h>
1612843Ssam 
17*45144Sbostic /*
18*45144Sbostic  * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
19*45144Sbostic  * but this would overflow if we switch to nanosec.
20*45144Sbostic  */
21*45144Sbostic #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
22*45144Sbostic 
2345139Sbostic clock_t
24*45144Sbostic times(tp)
25*45144Sbostic 	register struct tms *tp;
2612843Ssam {
2712843Ssam 	struct rusage ru;
28*45144Sbostic 	struct timeval t;
2912843Ssam 
3012843Ssam 	if (getrusage(RUSAGE_SELF, &ru) < 0)
3145139Sbostic 		return ((clock_t)-1);
32*45144Sbostic 	tp->tms_utime = CONVTCK(ru.ru_utime);
33*45144Sbostic 	tp->tms_stime = CONVTCK(ru.ru_stime);
3412843Ssam 	if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
3545139Sbostic 		return ((clock_t)-1);
36*45144Sbostic 	tp->tms_cutime = CONVTCK(ru.ru_utime);
37*45144Sbostic 	tp->tms_cstime = CONVTCK(ru.ru_stime);
38*45144Sbostic 	if (gettimeofday(&t, (struct timezone *)0))
39*45144Sbostic 		return ((clock_t)-1);
40*45144Sbostic 	return ((clock_t)(CONVTCK(t)));
4112843Ssam }
42