[b311cbc9] | 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 |
---|
[c499856] | 8 | * http://www.rtems.org/license/LICENSE. |
---|
[b311cbc9] | 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> |
---|
[94107fa8] | 18 | #include <inttypes.h> |
---|
[b311cbc9] | 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 "internal.h" |
---|
| 26 | |
---|
[94107fa8] | 27 | /* Helper macro to print "time_t" */ |
---|
| 28 | #if SIZEOF_TIME_T == 8 |
---|
| 29 | #define PRIdtime_t PRId64 |
---|
| 30 | #elif SIZEOF_TIME_T == 4 |
---|
| 31 | #define PRIdtime_t PRId32 |
---|
| 32 | #else |
---|
| 33 | #error "PRIdtime_t: unsupported size of time_t" |
---|
| 34 | #endif |
---|
| 35 | |
---|
[3421e520] | 36 | static int rtems_shell_main_time( |
---|
[b311cbc9] | 37 | int argc, |
---|
| 38 | char *argv[] |
---|
| 39 | ) |
---|
| 40 | { |
---|
| 41 | int errorlevel = 0; |
---|
| 42 | struct timespec start; |
---|
| 43 | struct timespec end; |
---|
| 44 | struct timespec period; |
---|
| 45 | rtems_status_code sc; |
---|
| 46 | |
---|
| 47 | argc--; |
---|
| 48 | |
---|
| 49 | sc = rtems_clock_get_uptime(&start); |
---|
| 50 | if (sc != RTEMS_SUCCESSFUL) |
---|
[66da4844] | 51 | fprintf(stderr, "error: cannot read time\n"); |
---|
[0893220] | 52 | |
---|
[b311cbc9] | 53 | if (argc) { |
---|
[5c141d6a] | 54 | errorlevel = rtems_shell_execute_cmd(argv[1], argc, &argv[1]); |
---|
[b311cbc9] | 55 | } |
---|
| 56 | |
---|
| 57 | sc = rtems_clock_get_uptime(&end); |
---|
| 58 | if (sc != RTEMS_SUCCESSFUL) |
---|
[66da4844] | 59 | fprintf(stderr, "error: cannot read time\n"); |
---|
[b311cbc9] | 60 | |
---|
| 61 | period.tv_sec = end.tv_sec - start.tv_sec; |
---|
| 62 | period.tv_nsec = end.tv_nsec - start.tv_nsec; |
---|
| 63 | if (period.tv_nsec < 0) |
---|
| 64 | { |
---|
| 65 | --period.tv_sec; |
---|
| 66 | period.tv_nsec += 1000000000UL; |
---|
| 67 | } |
---|
[0893220] | 68 | |
---|
[66da4844] | 69 | fprintf(stderr, "time: %" PRIdtime_t ":%02" PRIdtime_t ":%02" PRIdtime_t ".%03li\n", |
---|
[b311cbc9] | 70 | period.tv_sec / 3600, |
---|
| 71 | period.tv_sec / 60, period.tv_sec % 60, |
---|
| 72 | period.tv_nsec / 1000000); |
---|
[0893220] | 73 | |
---|
[b311cbc9] | 74 | return errorlevel; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | rtems_shell_cmd_t rtems_shell_TIME_Command = { |
---|
[7eada71] | 78 | .name = "time", |
---|
| 79 | .usage = "time command [arguments...]", |
---|
| 80 | .topic = "misc", |
---|
| 81 | .command = rtems_shell_main_time, |
---|
| 82 | .mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH |
---|
[b311cbc9] | 83 | }; |
---|