9a91e3847dec6571794d21b33489844e626bc8da
[platform/upstream/bash.git] / lib / sh / clock.c
1 /* clock.c - operations on struct tms and clock_t's */
2
3 /* Copyright (C) 1999 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
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
10    version.
11
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
15    for more details.
16
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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
20
21 #include <config.h>
22
23 #if defined (HAVE_TIMES)
24
25 #include <sys/types.h>
26 #include <posixtime.h>
27
28 #if defined (HAVE_SYS_TIMES_H)
29 #  include <sys/times.h>
30 #endif
31
32 #include <stdio.h>
33
34 void
35 clock_t_to_secs (t, sp, sfp)
36      clock_t t;
37      long *sp;
38      int *sfp;
39 {
40   static long clk_tck = -1;
41
42   if (clk_tck == -1)
43     clk_tck = get_clk_tck ();
44
45   *sfp = t % clk_tck;
46   *sfp = (*sfp * 1000) / clk_tck;
47
48   *sp = t / clk_tck;
49
50   /* Sanity check */
51   if (*sfp >= 1000)
52     {
53       *sp += 1;
54       *sfp -= 1000;
55     }
56 }
57
58 /* Print the time defined by a clock_t (returned by the `times' and `time'
59    system calls) in a standard way to stdio stream FP.  This is scaled in
60    terms of the value of CLK_TCK, which is what is returned by the
61    `times' call. */
62 void
63 print_clock_t (fp, t)
64      FILE *fp;
65      clock_t t;
66 {
67   int minutes, seconds_fraction;
68   long seconds;
69
70   clock_t_to_secs (t, &seconds, &seconds_fraction);
71
72   minutes = seconds / 60;
73   seconds %= 60;
74
75   fprintf (fp, "%0dm%0ld.%03ds",  minutes, seconds, seconds_fraction);
76 }
77 #endif /* HAVE_TIMES */
78