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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  *
00018  * March-19-2009 @ 02:44: Added sleep command.
00019  * Shao Miller <shao.miller@yrdsb.edu.on.ca>.
00020  */
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <gpxe/command.h>
00027 #include <gpxe/nap.h>
00028 #include <gpxe/timer.h>
00029 
00030 static int time_exec ( int argc, char **argv ) {
00031         unsigned long start;
00032         int rc, secs;
00033 
00034         if ( argc == 1 ||
00035              !strcmp ( argv[1], "--help" ) ||
00036              !strcmp ( argv[1], "-h" ) )
00037         {
00038                 printf ( "Usage:\n"
00039                          "  %s <command>\n"
00040                          "\n"
00041                          "Time a command\n",
00042                          argv[0] );
00043                 return 1;
00044         }
00045 
00046         start = currticks();
00047         rc = execv ( argv[1], argv + 1 );
00048         secs = (currticks() - start) / ticks_per_sec();
00049 
00050         printf ( "%s: %ds\n", argv[0], secs );
00051 
00052         return rc;
00053 }
00054 
00055 struct command time_command __command = {
00056         .name = "time",
00057         .exec = time_exec,
00058 };
00059 
00060 static int sleep_exec ( int argc, char **argv ) {
00061         unsigned long start, delay;
00062 
00063         if ( argc == 1 ||
00064              !strcmp ( argv[1], "--help" ) ||
00065              !strcmp ( argv[1], "-h" ))
00066         {
00067                 printf ( "Usage:\n"
00068                          "  %s <seconds>\n"
00069                          "\n"
00070                          "Sleep for <seconds> seconds\n",
00071                          argv[0] );
00072                 return 1;
00073         }
00074         start = currticks();
00075         delay = strtoul ( argv[1], NULL, 0 ) * ticks_per_sec();
00076         while ( ( currticks() - start ) <= delay )
00077                 cpu_nap();
00078         return 0;
00079 }
00080 
00081 struct command sleep_command __command = {
00082         .name = "sleep",
00083         .exec = sleep_exec,
00084 };

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1