No specific user configuration
[platform/upstream/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-2009 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
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bash.  If not, see <http://www.gnu.org/licenses/>.
20
21 $PRODUCES times.c
22
23 $BUILTIN times
24 $FUNCTION times_builtin
25 $SHORT_DOC times
26 Display process times.
27
28 Prints the accumulated user and system times for the shell and all of its
29 child processes.
30
31 Exit Status:
32 Always succeeds.
33 $END
34
35 #include <config.h>
36
37 #if defined (HAVE_UNISTD_H)
38 #  ifdef _MINIX
39 #    include <sys/types.h>
40 #  endif
41 #  include <unistd.h>
42 #endif
43
44 #include <stdio.h>
45 #include "../bashtypes.h"
46 #include "../shell.h"
47
48 #include <posixtime.h>
49
50 #if defined (HAVE_SYS_TIMES_H)
51 #  include <sys/times.h>
52 #endif /* HAVE_SYS_TIMES_H */
53
54 #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
55 #  include <sys/resource.h>
56 #endif
57
58 #include "common.h"
59
60 /* Print the totals for system and user time used. */
61 int
62 times_builtin (list)
63      WORD_LIST *list;
64 {
65 #if defined (HAVE_GETRUSAGE) && defined (HAVE_TIMEVAL) && defined (RUSAGE_SELF)
66   struct rusage self, kids;
67
68   USE_VAR(list);
69
70   if (no_options (list))
71     return (EX_USAGE);
72
73   getrusage (RUSAGE_SELF, &self);
74   getrusage (RUSAGE_CHILDREN, &kids);   /* terminated child processes */
75
76   print_timeval (stdout, &self.ru_utime);
77   putchar (' ');
78   print_timeval (stdout, &self.ru_stime);
79   putchar ('\n');
80   print_timeval (stdout, &kids.ru_utime);
81   putchar (' ');
82   print_timeval (stdout, &kids.ru_stime);
83   putchar ('\n');
84
85 #else
86 #  if defined (HAVE_TIMES)
87   /* This uses the POSIX.1/XPG5 times(2) interface, which fills in a 
88      `struct tms' with values of type clock_t. */
89   struct tms t;
90
91   USE_VAR(list);
92
93   if (no_options (list))
94     return (EX_USAGE);
95
96   times (&t);
97
98   print_clock_t (stdout, t.tms_utime);
99   putchar (' ');
100   print_clock_t (stdout, t.tms_stime);
101   putchar ('\n');
102   print_clock_t (stdout, t.tms_cutime);
103   putchar (' ');
104   print_clock_t (stdout, t.tms_cstime);
105   putchar ('\n');
106
107 #  else /* !HAVE_TIMES */
108
109   USE_VAR(list);
110
111   if (no_options (list))
112     return (EX_USAGE);
113   printf ("0.00 0.00\n0.00 0.00\n");
114
115 #  endif /* HAVE_TIMES */
116 #endif /* !HAVE_TIMES */
117
118   return (sh_chkwrite (EXECUTION_SUCCESS));
119 }