1/* times.c - times(3) library function */
2
3/* Copyright (C) 1999 Free Software Foundation, Inc.
4
5   This file is part of GNU Bash, the Bourne Again SHell.
6
7   Bash is free software; you can redistribute it and/or modify it under
8   the terms of the GNU General Public License as published by the Free
9   Software Foundation; either version 2, or (at your option) any later
10   version.
11
12   Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13   WARRANTY; without even the implied warranty of MERCHANTABILITY or
14   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15   for more details.
16
17   You should have received a copy of the GNU General Public License along
18   with Bash; see the file COPYING.  If not, write to the Free Software
19   Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21#include <config.h>
22
23#if !defined (HAVE_TIMES)
24
25#include <sys/types.h>
26#include <posixtime.h>
27#include <systimes.h>
28
29#if defined (HAVE_SYS_RESOURCE_H) && defined (HAVE_GETRUSAGE)
30#  include <sys/resource.h>
31#endif /* HAVE_SYS_RESOURCE_H && HAVE_GETRUSAGE */
32
33extern long	get_clk_tck __P((void));
34
35#define CONVTCK(r)      (r.tv_sec * clk_tck + r.tv_usec / (1000000 / clk_tck))
36
37clock_t
38times(tms)
39	struct tms *tms;
40{
41	clock_t rv;
42	static long clk_tck = -1;
43
44#if defined (HAVE_GETRUSAGE)
45	struct timeval tv;
46	struct rusage ru;
47
48	if (clk_tck == -1)
49		clk_tck = get_clk_tck();
50
51	if (getrusage(RUSAGE_SELF, &ru) < 0)
52		return ((clock_t)-1);
53	tms->tms_utime = CONVTCK(ru.ru_utime);
54	tms->tms_stime = CONVTCK(ru.ru_stime);
55
56	if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
57		return ((clock_t)-1);
58	tms->tms_cutime = CONVTCK(ru.ru_utime);
59	tms->tms_cstime = CONVTCK(ru.ru_stime);
60
61	if (gettimeofday(&tv, (struct timezone *) 0) < 0)
62		return ((clock_t)-1);
63	rv = (clock_t)(CONVTCK(tv));
64#else /* !HAVE_GETRUSAGE */
65	if (clk_tck == -1)
66		clk_tck = get_clk_tck();
67
68	/* We can't do anything. */
69	tms->tms_utime = tms->tms_stime = (clock_t)0;
70	tms->tms_cutime = tms->tms_cstime = (clock_t)0;
71
72	rv = (clock_t)time((time_t *)0) * clk_tck;
73# endif /* HAVE_GETRUSAGE */
74
75	return rv;
76}
77#endif /* !HAVE_TIMES */
78