Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*
0002  *  Time Shell Command Implmentation
0003  *
0004  *  Author: Chris Johns <chrisj@rtems.org>
0005  *
0006  *  The license and distribution terms for this file may be
0007  *  found in the file LICENSE in this distribution or at
0008  *  http://www.rtems.org/license/LICENSE.
0009  */
0010 
0011 #ifdef HAVE_CONFIG_H
0012 #include "config.h"
0013 #endif
0014 
0015 #include <stdio.h>
0016 #include <unistd.h>
0017 #include <string.h>
0018 #include <inttypes.h>
0019 #include <errno.h>
0020 #include <sys/types.h>
0021 #include <sys/stat.h>
0022 
0023 #include <rtems.h>
0024 #include <rtems/shell.h>
0025 #include <rtems/inttypes.h>
0026 #include "internal.h"
0027 
0028 static int rtems_shell_main_time(
0029   int   argc,
0030   char *argv[]
0031 )
0032 {
0033   int                errorlevel = 0;
0034   struct timespec    start;
0035   struct timespec    end;
0036   struct timespec    period;
0037   rtems_status_code  sc;
0038 
0039   argc--;
0040 
0041   sc = rtems_clock_get_uptime(&start);
0042   if (sc != RTEMS_SUCCESSFUL)
0043     fprintf(stderr, "error: cannot read time\n");
0044 
0045   if (argc) {
0046     errorlevel = rtems_shell_execute_cmd(argv[1], argc, &argv[1]);
0047   }
0048 
0049   sc = rtems_clock_get_uptime(&end);
0050   if (sc != RTEMS_SUCCESSFUL)
0051     fprintf(stderr, "error: cannot read time\n");
0052 
0053   period.tv_sec = end.tv_sec - start.tv_sec;
0054   period.tv_nsec = end.tv_nsec - start.tv_nsec;
0055   if (period.tv_nsec < 0)
0056   {
0057     --period.tv_sec;
0058     period.tv_nsec += 1000000000UL;
0059   }
0060 
0061   fprintf(stderr, "time: %" PRIdtime_t ":%02" PRIdtime_t ":%02" PRIdtime_t ".%03li\n",
0062          period.tv_sec / 3600,
0063          period.tv_sec / 60, period.tv_sec % 60,
0064          period.tv_nsec / 1000000);
0065 
0066   return errorlevel;
0067 }
0068 
0069 rtems_shell_cmd_t rtems_shell_TIME_Command = {
0070   .name = "time",
0071   .usage = "time command [arguments...]",
0072   .topic = "misc",
0073   .command = rtems_shell_main_time,
0074   .mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
0075 };