Back to home page

LXR

 
 

    


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

0001 /**
0002  * @file
0003  * 
0004  * @brief DATE Shell Command Implmentation
0005  */
0006 
0007 /*
0008  * Copyright (c) 2001 Fernando Ruiz Casas <fruizcasas@gmail.com>
0009  * Copyright (c) 2008 Joel Sherrill <joel.sherrill@oarcorp.com>.
0010  *
0011  *  The license and distribution terms for this file may be
0012  *  found in the file LICENSE in this distribution or at
0013  *  http://www.rtems.org/license/LICENSE.
0014  */
0015 
0016 #define _XOPEN_SOURCE
0017 
0018 #ifdef HAVE_CONFIG_H
0019 #include "config.h"
0020 #endif
0021 
0022 #include <stdio.h>
0023 #include <unistd.h>
0024 #include <string.h>
0025 #include <errno.h>
0026 #include <time.h>
0027 
0028 #include <rtems.h>
0029 #include <rtems/shell.h>
0030 #include "internal.h"
0031 
0032 static int rtems_shell_main_date(
0033   int   argc,
0034   char *argv[]
0035 )
0036 {
0037   /*
0038    *  Print the current date and time in default format.
0039    */
0040   if ( argc == 1 ) {
0041     time_t t;
0042 
0043     time(&t);
0044     printf("%s", ctime(&t));
0045     return 0;
0046   }
0047 
0048   /*
0049    *  Set the current date and time
0050    */
0051   if ( argc == 3 ) {
0052     char buf[128];
0053     struct tm TOD;
0054     struct timespec timesp;
0055     char *result;
0056 
0057     snprintf( buf, sizeof(buf), "%s %s", argv[1], argv[2] );
0058     result = strptime(
0059       buf,
0060       "%Y-%m-%d %T",
0061       &TOD
0062     );
0063     if ( result && !*result ) {
0064       timesp.tv_sec = mktime( &TOD );
0065       timesp.tv_nsec = 0;
0066       clock_settime( CLOCK_REALTIME, &timesp );
0067       return 0;
0068     }
0069   }
0070 
0071   fprintf( stderr, "%s: Usage: [YYYY-MM-DD HH:MM:SS]\n", argv[0] );
0072   return -1;
0073 }
0074 
0075 rtems_shell_cmd_t rtems_shell_DATE_Command = {
0076   "date",                        /* name */
0077   "date [YYYY-MM-DD HH:MM:SS]",  /* usage */
0078   "misc",                        /* topic */
0079   rtems_shell_main_date,         /* command */
0080   NULL,                          /* alias */
0081   NULL                           /* next */
0082 };