Update.
[glibc.git] / sysdeps / unix / sysv / linux / clock.c
blobe9d408d91c327bcf14b27ec3494be872d1ac8c1d
1 /* Copyright (C) 1991, 1992, 1996, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 #include <sys/times.h>
20 #include <time.h>
21 #include <unistd.h>
23 /* Return the time used by the program so far (user time + system time). */
24 clock_t
25 clock (void)
27 struct tms buf;
28 long clk_tck = __sysconf (_SC_CLK_TCK);
30 /* We don't check for errors here. The only error the kernel
31 returns is EFAULT if the value cannot be written to the struct we
32 pass a pointer to. Otherwise the kernel returns an `unsigned
33 long' value which is the number of jiffies since system start.
34 But this number can be negative (when read as `long') when the
35 system is up for some time. Ignoring errors should therefore
36 have no negative impacts but solve the problem. */
37 __times (&buf);
39 return
40 (clk_tck <= CLOCKS_PER_SEC)
41 ? ((unsigned long) buf.tms_utime + buf.tms_stime) * (CLOCKS_PER_SEC
42 / clk_tck)
43 : ((unsigned long) buf.tms_utime + buf.tms_stime) / (clk_tck
44 / CLOCKS_PER_SEC);