1 | /* |
---|
2 | * Time Shell Command Implmentation |
---|
3 | * |
---|
4 | * Author: Chris Johns <chrisj@rtems.org> |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * http://www.rtems.org/license/LICENSE. |
---|
9 | */ |
---|
10 | |
---|
11 | #ifdef HAVE_CONFIG_H |
---|
12 | #include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | #include <unistd.h> |
---|
17 | #include <string.h> |
---|
18 | #include <inttypes.h> |
---|
19 | #include <errno.h> |
---|
20 | #include <sys/types.h> |
---|
21 | #include <sys/stat.h> |
---|
22 | |
---|
23 | #include <rtems.h> |
---|
24 | #include <rtems/shell.h> |
---|
25 | #include <rtems/inttypes.h> |
---|
26 | #include "internal.h" |
---|
27 | |
---|
28 | static int rtems_shell_main_time( |
---|
29 | int argc, |
---|
30 | char *argv[] |
---|
31 | ) |
---|
32 | { |
---|
33 | int errorlevel = 0; |
---|
34 | struct timespec start; |
---|
35 | struct timespec end; |
---|
36 | struct timespec period; |
---|
37 | rtems_status_code sc; |
---|
38 | |
---|
39 | argc--; |
---|
40 | |
---|
41 | sc = rtems_clock_get_uptime(&start); |
---|
42 | if (sc != RTEMS_SUCCESSFUL) |
---|
43 | fprintf(stderr, "error: cannot read time\n"); |
---|
44 | |
---|
45 | if (argc) { |
---|
46 | errorlevel = rtems_shell_execute_cmd(argv[1], argc, &argv[1]); |
---|
47 | } |
---|
48 | |
---|
49 | sc = rtems_clock_get_uptime(&end); |
---|
50 | if (sc != RTEMS_SUCCESSFUL) |
---|
51 | fprintf(stderr, "error: cannot read time\n"); |
---|
52 | |
---|
53 | period.tv_sec = end.tv_sec - start.tv_sec; |
---|
54 | period.tv_nsec = end.tv_nsec - start.tv_nsec; |
---|
55 | if (period.tv_nsec < 0) |
---|
56 | { |
---|
57 | --period.tv_sec; |
---|
58 | period.tv_nsec += 1000000000UL; |
---|
59 | } |
---|
60 | |
---|
61 | fprintf(stderr, "time: %" PRIdtime_t ":%02" PRIdtime_t ":%02" PRIdtime_t ".%03li\n", |
---|
62 | period.tv_sec / 3600, |
---|
63 | period.tv_sec / 60, period.tv_sec % 60, |
---|
64 | period.tv_nsec / 1000000); |
---|
65 | |
---|
66 | return errorlevel; |
---|
67 | } |
---|
68 | |
---|
69 | rtems_shell_cmd_t rtems_shell_TIME_Command = { |
---|
70 | .name = "time", |
---|
71 | .usage = "time command [arguments...]", |
---|
72 | .topic = "misc", |
---|
73 | .command = rtems_shell_main_time, |
---|
74 | .mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH |
---|
75 | }; |
---|