xref: /csrg-svn/lib/libc/gen/times.c (revision 61111)
145139Sbostic /*-
2*61111Sbostic  * Copyright (c) 1990, 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
445139Sbostic  *
545139Sbostic  * %sccs.include.redist.c%
621334Sdist  */
712843Ssam 
826521Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61111Sbostic static char sccsid[] = "@(#)times.c	8.1 (Berkeley) 06/04/93";
1045139Sbostic #endif /* LIBC_SCCS and not lint */
1121334Sdist 
1245144Sbostic #include <sys/param.h>
1312843Ssam #include <sys/time.h>
1445139Sbostic #include <sys/times.h>
1512843Ssam #include <sys/resource.h>
1612843Ssam 
1745144Sbostic /*
1845144Sbostic  * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
1945144Sbostic  * but this would overflow if we switch to nanosec.
2045144Sbostic  */
2145144Sbostic #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
2245144Sbostic 
2345139Sbostic clock_t
times(tp)2445144Sbostic times(tp)
2545144Sbostic 	register struct tms *tp;
2612843Ssam {
2712843Ssam 	struct rusage ru;
2845144Sbostic 	struct timeval t;
2912843Ssam 
3012843Ssam 	if (getrusage(RUSAGE_SELF, &ru) < 0)
3145139Sbostic 		return ((clock_t)-1);
3245144Sbostic 	tp->tms_utime = CONVTCK(ru.ru_utime);
3345144Sbostic 	tp->tms_stime = CONVTCK(ru.ru_stime);
3412843Ssam 	if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
3545139Sbostic 		return ((clock_t)-1);
3645144Sbostic 	tp->tms_cutime = CONVTCK(ru.ru_utime);
3745144Sbostic 	tp->tms_cstime = CONVTCK(ru.ru_stime);
3845144Sbostic 	if (gettimeofday(&t, (struct timezone *)0))
3945144Sbostic 		return ((clock_t)-1);
4045144Sbostic 	return ((clock_t)(CONVTCK(t)));
4112843Ssam }
42