Patch-ID: bash40-030
[bash.git] / lib / sh / times.c
blob47ddf57724c94ca7792aad226bb9a819a761e0d9
1 /* times.c - times(3) library function */
3 /* Copyright (C) 1999 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 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.
12 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.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #if !defined (HAVE_TIMES)
25 #include <sys/types.h>
26 #include <posixtime.h>
27 #include <systimes.h>
29 #if defined (HAVE_SYS_RESOURCE_H) && defined (HAVE_GETRUSAGE)
30 # include <sys/resource.h>
31 #endif /* HAVE_SYS_RESOURCE_H && HAVE_GETRUSAGE */
33 extern long get_clk_tck __P((void));
35 #define CONVTCK(r) (r.tv_sec * clk_tck + r.tv_usec / (1000000 / clk_tck))
37 clock_t
38 times(tms)
39 struct tms *tms;
41 clock_t rv;
42 static long clk_tck = -1;
44 #if defined (HAVE_GETRUSAGE)
45 struct timeval tv;
46 struct rusage ru;
48 if (clk_tck == -1)
49 clk_tck = get_clk_tck();
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);
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);
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();
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;
72 rv = (clock_t)time((time_t *)0) * clk_tck;
73 # endif /* HAVE_GETRUSAGE */
75 return rv;
77 #endif /* !HAVE_TIMES */