]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/times.def
Bash-4.3 patch 32
[thirdparty/bash.git] / builtins / times.def
CommitLineData
726f6388
JA
1This file is times.def, from which is created times.c.
2It implements the builtin "times" in Bash.
3
3185942a 4Copyright (C) 1987-2009 Free Software Foundation, Inc.
726f6388
JA
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
3185942a
JA
8Bash 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.
726f6388 12
3185942a
JA
13Bash 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.
726f6388 17
3185942a
JA
18You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
726f6388
JA
20
21$PRODUCES times.c
22
23$BUILTIN times
24$FUNCTION times_builtin
25$SHORT_DOC times
3185942a
JA
26Display process times.
27
28Prints the accumulated user and system times for the shell and all of its
29child processes.
30
31Exit Status:
32Always succeeds.
726f6388
JA
33$END
34
ccc6cda3 35#include <config.h>
726f6388 36
ccc6cda3 37#if defined (HAVE_UNISTD_H)
cce855bc
JA
38# ifdef _MINIX
39# include <sys/types.h>
40# endif
ccc6cda3
JA
41# include <unistd.h>
42#endif
726f6388 43
ccc6cda3
JA
44#include <stdio.h>
45#include "../bashtypes.h"
46#include "../shell.h"
47
bb70624e 48#include <posixtime.h>
ccc6cda3
JA
49
50#if defined (HAVE_SYS_TIMES_H)
51# include <sys/times.h>
52#endif /* HAVE_SYS_TIMES_H */
53
cce855bc 54#if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
726f6388 55# include <sys/resource.h>
ccc6cda3
JA
56#endif
57
58#include "common.h"
726f6388 59
ccc6cda3
JA
60/* Print the totals for system and user time used. */
61int
726f6388
JA
62times_builtin (list)
63 WORD_LIST *list;
64{
ccc6cda3 65#if defined (HAVE_GETRUSAGE) && defined (HAVE_TIMEVAL) && defined (RUSAGE_SELF)
726f6388
JA
66 struct rusage self, kids;
67
7117c2d2
JA
68 USE_VAR(list);
69
70 if (no_options (list))
71 return (EX_USAGE);
72
726f6388
JA
73 getrusage (RUSAGE_SELF, &self);
74 getrusage (RUSAGE_CHILDREN, &kids); /* terminated child processes */
75
ccc6cda3 76 print_timeval (stdout, &self.ru_utime);
726f6388 77 putchar (' ');
ccc6cda3 78 print_timeval (stdout, &self.ru_stime);
726f6388 79 putchar ('\n');
ccc6cda3 80 print_timeval (stdout, &kids.ru_utime);
726f6388 81 putchar (' ');
ccc6cda3 82 print_timeval (stdout, &kids.ru_stime);
726f6388
JA
83 putchar ('\n');
84
ccc6cda3
JA
85#else
86# if defined (HAVE_TIMES)
bb70624e
JA
87 /* This uses the POSIX.1/XPG5 times(2) interface, which fills in a
88 `struct tms' with values of type clock_t. */
ccc6cda3
JA
89 struct tms t;
90
7117c2d2
JA
91 USE_VAR(list);
92
93 if (no_options (list))
94 return (EX_USAGE);
95
ccc6cda3 96 times (&t);
726f6388 97
bb70624e 98 print_clock_t (stdout, t.tms_utime);
726f6388 99 putchar (' ');
bb70624e 100 print_clock_t (stdout, t.tms_stime);
726f6388 101 putchar ('\n');
bb70624e 102 print_clock_t (stdout, t.tms_cutime);
726f6388 103 putchar (' ');
bb70624e 104 print_clock_t (stdout, t.tms_cstime);
726f6388 105 putchar ('\n');
7117c2d2 106
ccc6cda3 107# else /* !HAVE_TIMES */
7117c2d2
JA
108
109 USE_VAR(list);
110
111 if (no_options (list))
112 return (EX_USAGE);
ccc6cda3 113 printf ("0.00 0.00\n0.00 0.00\n");
7117c2d2 114
ccc6cda3
JA
115# endif /* HAVE_TIMES */
116#endif /* !HAVE_TIMES */
726f6388 117
3185942a 118 return (sh_chkwrite (EXECUTION_SUCCESS));
726f6388 119}