]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/times.def
216abcbea9701b8f28141c9269e67e51456093ba
[thirdparty/bash.git] / builtins / times.def
1 This file is times.def, from which is created times.c.
2 It implements the builtin "times" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 1, or (at your option) any later
11 version.
12
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 $PRODUCES times.c
23
24 $BUILTIN times
25 $FUNCTION times_builtin
26 $SHORT_DOC times
27 Print the accumulated user and system times for processes run from
28 the shell.
29 $END
30
31 #include <config.h>
32
33 #if defined (HAVE_UNISTD_H)
34 # include <unistd.h>
35 #endif
36
37 #include <stdio.h>
38 #include "../bashtypes.h"
39 #include "../shell.h"
40
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
43 # include <time.h>
44 #else
45 # if defined (HAVE_SYS_TIME_H)
46 # include <sys/time.h>
47 # else
48 # include <time.h>
49 # endif
50 #endif
51
52 #if defined (HAVE_SYS_TIMES_H)
53 # include <sys/times.h>
54 #endif /* HAVE_SYS_TIMES_H */
55
56 #if defined (HAVE_SYS_RESOURCE_H)
57 # include <sys/resource.h>
58 #endif
59
60 #include "common.h"
61
62 /* Print the totals for system and user time used. */
63 int
64 times_builtin (list)
65 WORD_LIST *list;
66 {
67 #if defined (HAVE_GETRUSAGE) && defined (HAVE_TIMEVAL) && defined (RUSAGE_SELF)
68 struct rusage self, kids;
69
70 getrusage (RUSAGE_SELF, &self);
71 getrusage (RUSAGE_CHILDREN, &kids); /* terminated child processes */
72
73 print_timeval (stdout, &self.ru_utime);
74 putchar (' ');
75 print_timeval (stdout, &self.ru_stime);
76 putchar ('\n');
77 print_timeval (stdout, &kids.ru_utime);
78 putchar (' ');
79 print_timeval (stdout, &kids.ru_stime);
80 putchar ('\n');
81
82 #else
83 # if defined (HAVE_TIMES)
84 /* As of System V.3, HP-UX 6.5, and other ATT-like systems, this stuff is
85 returned in terms of clock ticks (HZ from sys/param.h). C'mon, guys.
86 This kind of stupid clock-dependent stuff is exactly the reason 4.2BSD
87 introduced the `timeval' struct. */
88 struct tms t;
89
90 times (&t);
91
92 print_time_in_hz (stdout, t.tms_utime);
93 putchar (' ');
94 print_time_in_hz (stdout, t.tms_stime);
95 putchar ('\n');
96 print_time_in_hz (stdout, t.tms_cutime);
97 putchar (' ');
98 print_time_in_hz (stdout, t.tms_cstime);
99 putchar ('\n');
100 # else /* !HAVE_TIMES */
101 printf ("0.00 0.00\n0.00 0.00\n");
102 # endif /* HAVE_TIMES */
103 #endif /* !HAVE_TIMES */
104
105 return (EXECUTION_SUCCESS);
106 }