source: rtems/cpukit/libmisc/shell/main_time.c @ 5c141d6a

4.115
Last change on this file since 5c141d6a was 5c141d6a, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 18, 2014 at 9:36:27 AM

shell: Add and use rtems_shell_execute_cmd()

  • Property mode set to 100644
File size: 2.0 KB
Line 
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 "internal.h"
26
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
36static int rtems_shell_main_time(
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)
51    fprintf(stderr, "error: cannot read time\n");
52
53  if (argc) {
54    errorlevel = rtems_shell_execute_cmd(argv[1], argc, &argv[1]);
55  }
56
57  sc = rtems_clock_get_uptime(&end);
58  if (sc != RTEMS_SUCCESSFUL)
59    fprintf(stderr, "error: cannot read time\n");
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  }
68
69  fprintf(stderr, "time: %" PRIdtime_t ":%02" PRIdtime_t ":%02" PRIdtime_t ".%03li\n",
70         period.tv_sec / 3600,
71         period.tv_sec / 60, period.tv_sec % 60,
72         period.tv_nsec / 1000000);
73
74  return errorlevel;
75}
76
77rtems_shell_cmd_t rtems_shell_TIME_Command = {
78  "time",                                     /* name */
79  "time command [arguments...]",              /* usage */
80  "misc",                                     /* topic */
81  rtems_shell_main_time,                      /* command */
82  NULL,                                       /* alias */
83  NULL                                        /* next */
84};
Note: See TracBrowser for help on using the repository browser.