xref: /original-bsd/lib/libc/gen/times.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)times.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/times.h>
15 #include <sys/resource.h>
16 
17 /*
18  * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
19  * but this would overflow if we switch to nanosec.
20  */
21 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
22 
23 clock_t
24 times(tp)
25 	register struct tms *tp;
26 {
27 	struct rusage ru;
28 	struct timeval t;
29 
30 	if (getrusage(RUSAGE_SELF, &ru) < 0)
31 		return ((clock_t)-1);
32 	tp->tms_utime = CONVTCK(ru.ru_utime);
33 	tp->tms_stime = CONVTCK(ru.ru_stime);
34 	if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
35 		return ((clock_t)-1);
36 	tp->tms_cutime = CONVTCK(ru.ru_utime);
37 	tp->tms_cstime = CONVTCK(ru.ru_stime);
38 	if (gettimeofday(&t, (struct timezone *)0))
39 		return ((clock_t)-1);
40 	return ((clock_t)(CONVTCK(t)));
41 }
42