blob: f31f43331e96a8cb5a5961d75c4696ef0920e229 [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001This file is times.def, from which is created times.c.
2It implements the builtin "times" in Bash.
3
Jari Aalto31859422009-01-12 13:36:28 +00004Copyright (C) 1987-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00005
6This file is part of GNU Bash, the Bourne Again SHell.
7
Jari Aalto31859422009-01-12 13:36:28 +00008Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
Jari Aalto726f6381996-08-26 18:22:31 +000020
21$PRODUCES times.c
22
23$BUILTIN times
24$FUNCTION times_builtin
25$SHORT_DOC times
Jari Aalto31859422009-01-12 13:36:28 +000026Display process times.
27
28Prints the accumulated user and system times for the shell and all of its
29child processes.
30
31Exit Status:
32Always succeeds.
Jari Aalto726f6381996-08-26 18:22:31 +000033$END
34
Jari Aaltoccc6cda1996-12-23 17:02:34 +000035#include <config.h>
36
37#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000038# ifdef _MINIX
39# include <sys/types.h>
40# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000041# include <unistd.h>
42#endif
43
44#include <stdio.h>
45#include "../bashtypes.h"
Jari Aalto726f6381996-08-26 18:22:31 +000046#include "../shell.h"
Jari Aalto726f6381996-08-26 18:22:31 +000047
Jari Aaltobb706242000-03-17 21:46:59 +000048#include <posixtime.h>
Jari Aalto726f6381996-08-26 18:22:31 +000049
Jari Aaltoccc6cda1996-12-23 17:02:34 +000050#if defined (HAVE_SYS_TIMES_H)
51# include <sys/times.h>
52#endif /* HAVE_SYS_TIMES_H */
53
Jari Aaltocce855b1998-04-17 19:52:44 +000054#if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000055# include <sys/resource.h>
56#endif
57
58#include "common.h"
59
60/* Print the totals for system and user time used. */
61int
Jari Aalto726f6381996-08-26 18:22:31 +000062times_builtin (list)
63 WORD_LIST *list;
64{
Jari Aaltoccc6cda1996-12-23 17:02:34 +000065#if defined (HAVE_GETRUSAGE) && defined (HAVE_TIMEVAL) && defined (RUSAGE_SELF)
Jari Aalto726f6381996-08-26 18:22:31 +000066 struct rusage self, kids;
67
Jari Aalto7117c2d2002-07-17 14:10:11 +000068 USE_VAR(list);
69
70 if (no_options (list))
71 return (EX_USAGE);
72
Jari Aalto726f6381996-08-26 18:22:31 +000073 getrusage (RUSAGE_SELF, &self);
74 getrusage (RUSAGE_CHILDREN, &kids); /* terminated child processes */
75
Jari Aaltoccc6cda1996-12-23 17:02:34 +000076 print_timeval (stdout, &self.ru_utime);
Jari Aalto726f6381996-08-26 18:22:31 +000077 putchar (' ');
Jari Aaltoccc6cda1996-12-23 17:02:34 +000078 print_timeval (stdout, &self.ru_stime);
Jari Aalto726f6381996-08-26 18:22:31 +000079 putchar ('\n');
Jari Aaltoccc6cda1996-12-23 17:02:34 +000080 print_timeval (stdout, &kids.ru_utime);
Jari Aalto726f6381996-08-26 18:22:31 +000081 putchar (' ');
Jari Aaltoccc6cda1996-12-23 17:02:34 +000082 print_timeval (stdout, &kids.ru_stime);
Jari Aalto726f6381996-08-26 18:22:31 +000083 putchar ('\n');
84
Jari Aaltoccc6cda1996-12-23 17:02:34 +000085#else
86# if defined (HAVE_TIMES)
Jari Aaltobb706242000-03-17 21:46:59 +000087 /* This uses the POSIX.1/XPG5 times(2) interface, which fills in a
88 `struct tms' with values of type clock_t. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000089 struct tms t;
Jari Aalto726f6381996-08-26 18:22:31 +000090
Jari Aalto7117c2d2002-07-17 14:10:11 +000091 USE_VAR(list);
92
93 if (no_options (list))
94 return (EX_USAGE);
95
Jari Aaltoccc6cda1996-12-23 17:02:34 +000096 times (&t);
97
Jari Aaltobb706242000-03-17 21:46:59 +000098 print_clock_t (stdout, t.tms_utime);
Jari Aalto726f6381996-08-26 18:22:31 +000099 putchar (' ');
Jari Aaltobb706242000-03-17 21:46:59 +0000100 print_clock_t (stdout, t.tms_stime);
Jari Aalto726f6381996-08-26 18:22:31 +0000101 putchar ('\n');
Jari Aaltobb706242000-03-17 21:46:59 +0000102 print_clock_t (stdout, t.tms_cutime);
Jari Aalto726f6381996-08-26 18:22:31 +0000103 putchar (' ');
Jari Aaltobb706242000-03-17 21:46:59 +0000104 print_clock_t (stdout, t.tms_cstime);
Jari Aalto726f6381996-08-26 18:22:31 +0000105 putchar ('\n');
Jari Aalto7117c2d2002-07-17 14:10:11 +0000106
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000107# else /* !HAVE_TIMES */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000108
109 USE_VAR(list);
110
111 if (no_options (list))
112 return (EX_USAGE);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000113 printf ("0.00 0.00\n0.00 0.00\n");
Jari Aalto7117c2d2002-07-17 14:10:11 +0000114
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000115# endif /* HAVE_TIMES */
116#endif /* !HAVE_TIMES */
Jari Aalto726f6381996-08-26 18:22:31 +0000117
Jari Aalto31859422009-01-12 13:36:28 +0000118 return (sh_chkwrite (EXECUTION_SUCCESS));
Jari Aalto726f6381996-08-26 18:22:31 +0000119}