blob: 47ddf57724c94ca7792aad226bb9a819a761e0d9 [file] [log] [blame]
Jari Aaltobb706242000-03-17 21:46:59 +00001/* 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
Jari Aalto31859422009-01-12 13:36:28 +00007 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
Jari Aaltobb706242000-03-17 21:46:59 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
Jari Aaltobb706242000-03-17 21:46:59 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aaltobb706242000-03-17 21:46:59 +000020
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
Jari Aaltof73dda02001-11-13 17:56:06 +000033extern long get_clk_tck __P((void));
Jari Aaltobb706242000-03-17 21:46:59 +000034
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);
Jari Aalto28ef6c32001-04-06 19:14:31 +000063 rv = (clock_t)(CONVTCK(tv));
Jari Aaltobb706242000-03-17 21:46:59 +000064#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 */