Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 16 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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/>. |
| 19 | */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 33 | extern long get_clk_tck __P((void)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 34 | |
| 35 | #define CONVTCK(r) (r.tv_sec * clk_tck + r.tv_usec / (1000000 / clk_tck)) |
| 36 | |
| 37 | clock_t |
| 38 | times(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 Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 63 | rv = (clock_t)(CONVTCK(tv)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 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 */ |