chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / unix / bsd / times.c
1 /* Copyright (C) 1991,92,93,95,96,97,1998,2001 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <errno.h>
20 #include <stddef.h>
21 #include <sys/resource.h>
22 #include <sys/times.h>
23 #include <sys/time.h>
24 #include <time.h>
25
26
27 /* Time the program started.  */
28 extern time_t _posix_start_time;
29
30 #ifdef __GNUC__
31 __inline
32 #endif
33 static clock_t
34 timeval_to_clock_t (const struct timeval *tv, clock_t clk_tck)
35 {
36   return (clock_t) ((tv->tv_sec * clk_tck) +
37                     (tv->tv_usec * clk_tck / 1000000L));
38 }
39
40 /* Store the CPU time used by this process and all its
41    dead children (and their dead children) in BUFFER.
42    Return the elapsed real time, or (clock_t) -1 for errors.
43    All times are in CLK_TCKths of a second.  */
44 clock_t
45 __times (buffer)
46      struct tms *buffer;
47 {
48   struct rusage usage;
49   clock_t clk_tck;
50
51   if (buffer == NULL)
52     {
53       __set_errno (EINVAL);
54       return (clock_t) -1;
55     }
56
57   clk_tck = __getclktck ();
58   
59   if (__getrusage (RUSAGE_SELF, &usage) < 0)
60     return (clock_t) -1;
61   buffer->tms_utime = (clock_t) timeval_to_clock_t (&usage.ru_utime, clk_tck);
62   buffer->tms_stime = (clock_t) timeval_to_clock_t (&usage.ru_stime, clk_tck);
63
64   if (__getrusage (RUSAGE_CHILDREN, &usage) < 0)
65     return (clock_t) -1;
66   buffer->tms_cutime = (clock_t) timeval_to_clock_t (&usage.ru_utime, clk_tck);
67   buffer->tms_cstime = (clock_t) timeval_to_clock_t (&usage.ru_stime, clk_tck);
68
69   return (time ((time_t *) NULL) - _posix_start_time) * clk_tck;
70 }
71
72 weak_alias (__times, times)