Patch-ID: bash32-017
[bash.git] / builtins / times.def
blob22304fc2f92c00c4a25fcf3469dad27063640410
1 This file is times.def, from which is created times.c.
2 It implements the builtin "times" in Bash.
4 Copyright (C) 1987-2002 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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.
22 $PRODUCES times.c
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
31 #include <config.h>
33 #if defined (HAVE_UNISTD_H)
34 # ifdef _MINIX
35 # include <sys/types.h>
36 # endif
37 # include <unistd.h>
38 #endif
40 #include <stdio.h>
41 #include "../bashtypes.h"
42 #include "../shell.h"
44 #include <posixtime.h>
46 #if defined (HAVE_SYS_TIMES_H)
47 # include <sys/times.h>
48 #endif /* HAVE_SYS_TIMES_H */
50 #if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
51 # include <sys/resource.h>
52 #endif
54 #include "common.h"
56 /* Print the totals for system and user time used. */
57 int
58 times_builtin (list)
59 WORD_LIST *list;
61 #if defined (HAVE_GETRUSAGE) && defined (HAVE_TIMEVAL) && defined (RUSAGE_SELF)
62 struct rusage self, kids;
64 USE_VAR(list);
66 if (no_options (list))
67 return (EX_USAGE);
69 getrusage (RUSAGE_SELF, &self);
70 getrusage (RUSAGE_CHILDREN, &kids); /* terminated child processes */
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');
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;
87 USE_VAR(list);
89 if (no_options (list))
90 return (EX_USAGE);
92 times (&t);
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');
103 # else /* !HAVE_TIMES */
105 USE_VAR(list);
107 if (no_options (list))
108 return (EX_USAGE);
109 printf ("0.00 0.00\n0.00 0.00\n");
111 # endif /* HAVE_TIMES */
112 #endif /* !HAVE_TIMES */
114 return (EXECUTION_SUCCESS);