exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / times.c
blobb8bf24c2fa964f606c98dc8d633231e001cfc63f
1 /* Get process times
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
20 #include <config.h>
22 /* Get times prototype. */
23 #include <sys/times.h>
25 /* Get round. */
26 #include <math.h>
28 /* Get GetProcessTimes etc. */
29 #include <windows.h>
31 static clock_t
32 filetime2clock (FILETIME time)
34 float f;
36 /* We have a 64-bit value, in the form of two DWORDS aka unsigned
37 int, counting the number of 100-nanosecond intervals. We need to
38 convert these to clock ticks. Older POSIX uses CLK_TCK to
39 indicate the number of clock ticks per second while modern POSIX
40 uses sysconf(_SC_CLK_TCK). Mingw32 does not appear to have
41 sysconf(_SC_CLK_TCK), but appears to have CLK_TCK = 1000 so we
42 use it. Note that CLOCKS_PER_SEC constant does not apply here,
43 it is for use with the clock function. */
45 f = (unsigned long long) time.dwHighDateTime << 32;
46 f += time.dwLowDateTime;
47 f = f * CLK_TCK / 10000000;
48 return (clock_t) round (f);
51 clock_t
52 times (struct tms * buffer)
54 FILETIME creation_time, exit_time, kernel_time, user_time;
56 if (GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
57 &kernel_time, &user_time) == 0)
58 return (clock_t) -1;
60 buffer->tms_utime = filetime2clock (user_time);
61 buffer->tms_stime = filetime2clock (kernel_time);
62 buffer->tms_cutime = 0;
63 buffer->tms_cstime = 0;
65 return clock ();