maint: update copyright
[gnulib.git] / lib / times.c
1 /* Get process times
2
3    Copyright (C) 2008-2014 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program 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 General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Written by Simon Josefsson <simon@josefsson.org>, 2008.  */
19
20 #include <config.h>
21
22 /* Get times prototype. */
23 #include <sys/times.h>
24
25 /* Get round. */
26 #include <math.h>
27
28 /* Get GetProcessTimes etc. */
29 #include <windows.h>
30
31 static clock_t
32 filetime2clock (FILETIME time)
33 {
34   float f;
35
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.  */
44
45   f = (unsigned long long) time.dwHighDateTime << 32;
46   f += time.dwLowDateTime;
47   f = f * CLK_TCK / 10000000;
48   return (clock_t) round (f);
49 }
50
51 clock_t
52 times (struct tms * buffer)
53 {
54   FILETIME creation_time, exit_time, kernel_time, user_time;
55
56   if (GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
57                        &kernel_time, &user_time) == 0)
58     return (clock_t) -1;
59
60   buffer->tms_utime = filetime2clock (user_time);
61   buffer->tms_stime = filetime2clock (kernel_time);
62   buffer->tms_cutime = 0;
63   buffer->tms_cstime = 0;
64
65   return filetime2clock (creation_time);
66 }