blob: 4e8b49002a132757789f1972e3d276dbedaaff0a [file] [log] [blame]
Dan Pasanenc6e37862014-10-02 14:08:59 -05001/* times.c, created from times.def. */
2#line 22 "./times.def"
3
4#line 34 "./times.def"
5
6#include <config.h>
7
8#if defined (HAVE_UNISTD_H)
9# ifdef _MINIX
10# include <sys/types.h>
11# endif
12# include <unistd.h>
13#endif
14
15#include <stdio.h>
16#include "../bashtypes.h"
17#include "../shell.h"
18
19#include <posixtime.h>
20
21#if defined (HAVE_SYS_TIMES_H)
22# include <sys/times.h>
23#endif /* HAVE_SYS_TIMES_H */
24
25#if defined (HAVE_SYS_RESOURCE_H) && !defined (RLIMTYPE)
26# include <sys/resource.h>
27#endif
28
29#include "common.h"
30
31/* Print the totals for system and user time used. */
32int
33times_builtin (list)
34 WORD_LIST *list;
35{
36#if defined (HAVE_GETRUSAGE) && defined (HAVE_TIMEVAL) && defined (RUSAGE_SELF)
37 struct rusage self, kids;
38
39 USE_VAR(list);
40
41 if (no_options (list))
42 return (EX_USAGE);
43
44 getrusage (RUSAGE_SELF, &self);
45 getrusage (RUSAGE_CHILDREN, &kids); /* terminated child processes */
46
47 print_timeval (stdout, &self.ru_utime);
48 putchar (' ');
49 print_timeval (stdout, &self.ru_stime);
50 putchar ('\n');
51 print_timeval (stdout, &kids.ru_utime);
52 putchar (' ');
53 print_timeval (stdout, &kids.ru_stime);
54 putchar ('\n');
55
56#else
57# if defined (HAVE_TIMES)
58 /* This uses the POSIX.1/XPG5 times(2) interface, which fills in a
59 `struct tms' with values of type clock_t. */
60 struct tms t;
61
62 USE_VAR(list);
63
64 if (no_options (list))
65 return (EX_USAGE);
66
67 times (&t);
68
69 print_clock_t (stdout, t.tms_utime);
70 putchar (' ');
71 print_clock_t (stdout, t.tms_stime);
72 putchar ('\n');
73 print_clock_t (stdout, t.tms_cutime);
74 putchar (' ');
75 print_clock_t (stdout, t.tms_cstime);
76 putchar ('\n');
77
78# else /* !HAVE_TIMES */
79
80 USE_VAR(list);
81
82 if (no_options (list))
83 return (EX_USAGE);
84 printf ("0.00 0.00\n0.00 0.00\n");
85
86# endif /* HAVE_TIMES */
87#endif /* !HAVE_TIMES */
88
89 return (sh_chkwrite (EXECUTION_SUCCESS));
90}