]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/times.c
1 /* times.c - times(3) library function */
3 /* Copyright (C) 1999 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #if !defined (HAVE_TIMES)
25 #include <sys/types.h>
26 #include <posixtime.h>
29 #if defined (HAVE_SYS_RESOURCE_H) && defined (HAVE_GETRUSAGE)
30 # include <sys/resource.h>
31 #endif /* HAVE_SYS_RESOURCE_H && HAVE_GETRUSAGE */
33 extern long get_clk_tck();
35 #define CONVTCK(r) (r.tv_sec * clk_tck + r.tv_usec / (1000000 / clk_tck))
42 static long clk_tck
= -1;
44 #if defined (HAVE_GETRUSAGE)
49 clk_tck
= get_clk_tck();
51 if (getrusage(RUSAGE_SELF
, &ru
) < 0)
53 tms
->tms_utime
= CONVTCK(ru
.ru_utime
);
54 tms
->tms_stime
= CONVTCK(ru
.ru_stime
);
56 if (getrusage(RUSAGE_CHILDREN
, &ru
) < 0)
58 tms
->tms_cutime
= CONVTCK(ru
.ru_utime
);
59 tms
->tms_cstime
= CONVTCK(ru
.ru_stime
);
61 if (gettimeofday(&tv
, (struct timezone
*) 0) < 0)
63 rv
= (clock_t)(CONVTCK(tv
));
64 #else /* !HAVE_GETRUSAGE */
66 clk_tck
= get_clk_tck();
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;
72 rv
= (clock_t)time((time_t *)0) * clk_tck
;
73 # endif /* HAVE_GETRUSAGE */
77 #endif /* !HAVE_TIMES */