22304fc2f92c00c4a25fcf3469dad27063640410
[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-2002 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 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 #  ifdef _MINIX
35 #    include <sys/types.h>
36 #  endif
37 #  include <unistd.h>
38 #endif
39
40 #include <stdio.h>
41 #include "../bashtypes.h"
42 #include "../shell.h"
43
44 #include <posixtime.h>
45
46 #if defined (HAVE_SYS_TIMES_H)
47 #  include <sys/times.h>
48 #endif /* HAVE_SYS_TIMES_H */
49
50 #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
51 #  include <sys/resource.h>
52 #endif
53
54 #include "common.h"
55
56 /* Print the totals for system and user time used. */
57 int
58 times_builtin (list)
59      WORD_LIST *list;
60 {
61 #if defined (HAVE_GETRUSAGE) && defined (HAVE_TIMEVAL) && defined (RUSAGE_SELF)
62   struct rusage self, kids;
63
64   USE_VAR(list);
65
66   if (no_options (list))
67     return (EX_USAGE);
68
69   getrusage (RUSAGE_SELF, &self);
70   getrusage (RUSAGE_CHILDREN, &kids);   /* terminated child processes */
71
72   print_timeval (stdout, &self.ru_utime);
73   putchar (' ');
74   print_timeval (stdout, &self.ru_stime);
75   putchar ('\n');
76   print_timeval (stdout, &kids.ru_utime);
77   putchar (' ');
78   print_timeval (stdout, &kids.ru_stime);
79   putchar ('\n');
80
81 #else
82 #  if defined (HAVE_TIMES)
83   /* This uses the POSIX.1/XPG5 times(2) interface, which fills in a 
84      `struct tms' with values of type clock_t. */
85   struct tms t;
86
87   USE_VAR(list);
88
89   if (no_options (list))
90     return (EX_USAGE);
91
92   times (&t);
93
94   print_clock_t (stdout, t.tms_utime);
95   putchar (' ');
96   print_clock_t (stdout, t.tms_stime);
97   putchar ('\n');
98   print_clock_t (stdout, t.tms_cutime);
99   putchar (' ');
100   print_clock_t (stdout, t.tms_cstime);
101   putchar ('\n');
102
103 #  else /* !HAVE_TIMES */
104
105   USE_VAR(list);
106
107   if (no_options (list))
108     return (EX_USAGE);
109   printf ("0.00 0.00\n0.00 0.00\n");
110
111 #  endif /* HAVE_TIMES */
112 #endif /* !HAVE_TIMES */
113
114   return (EXECUTION_SUCCESS);
115 }