iPXE
time_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * March-19-2009 @ 02:44: Added sleep command.
00020  * Shao Miller <shao.miller@yrdsb.edu.on.ca>.
00021  */
00022 
00023 FILE_LICENCE ( GPL2_OR_LATER );
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <unistd.h>
00029 #include <ipxe/command.h>
00030 #include <ipxe/parseopt.h>
00031 #include <ipxe/timer.h>
00032 
00033 /** @file
00034  *
00035  * Time commands
00036  *
00037  */
00038 
00039 /** "time" options */
00040 struct time_options {};
00041 
00042 /** "time" option list */
00043 static struct option_descriptor time_opts[] = {};
00044 
00045 /** "time" command descriptor */
00046 static struct command_descriptor time_cmd =
00047         COMMAND_DESC ( struct time_options, time_opts, 1, MAX_ARGUMENTS,
00048                        "<command>" );
00049 
00050 /**
00051  * "time" command
00052  *
00053  * @v argc              Argument count
00054  * @v argv              Argument list
00055  * @ret rc              Return status code
00056  */
00057 static int time_exec ( int argc, char **argv ) {
00058         struct time_options opts;
00059         unsigned long start;
00060         unsigned long elapsed;
00061         int decisecs;
00062         int rc;
00063 
00064         /* Parse options */
00065         if ( ( rc = parse_options ( argc, argv, &time_cmd, &opts ) ) != 0 )
00066                 return rc;
00067 
00068         start = currticks();
00069         rc = execv ( argv[1], argv + 1 );
00070         elapsed = ( currticks() - start );
00071         decisecs = ( 10 * elapsed / TICKS_PER_SEC );
00072 
00073         printf ( "%s: %d.%ds\n", argv[0],
00074                  ( decisecs / 10 ), ( decisecs % 10 ) );
00075 
00076         return rc;
00077 }
00078 
00079 /** "time" command */
00080 struct command time_command __command = {
00081         .name = "time",
00082         .exec = time_exec,
00083 };