You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time_cmd.c 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * March-19-2009 @ 02:44: Added sleep command.
  19. * Shao Miller <shao.miller@yrdsb.edu.on.ca>.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <gpxe/command.h>
  26. #include <gpxe/nap.h>
  27. #include <gpxe/timer.h>
  28. static int time_exec ( int argc, char **argv ) {
  29. unsigned long start;
  30. int rc, secs;
  31. if ( argc == 1 ||
  32. !strcmp ( argv[1], "--help" ) ||
  33. !strcmp ( argv[1], "-h" ) )
  34. {
  35. printf ( "Usage:\n"
  36. " %s <command>\n"
  37. "\n"
  38. "Time a command\n",
  39. argv[0] );
  40. return 1;
  41. }
  42. start = currticks();
  43. rc = execv ( argv[1], argv + 1 );
  44. secs = (currticks() - start) / ticks_per_sec();
  45. printf ( "%s: %ds\n", argv[0], secs );
  46. return rc;
  47. }
  48. struct command time_command __command = {
  49. .name = "time",
  50. .exec = time_exec,
  51. };
  52. static int sleep_exec ( int argc, char **argv ) {
  53. unsigned long start, delay;
  54. if ( argc == 1 ||
  55. !strcmp ( argv[1], "--help" ) ||
  56. !strcmp ( argv[1], "-h" ))
  57. {
  58. printf ( "Usage:\n"
  59. " %s <seconds>\n"
  60. "\n"
  61. "Sleep for <seconds> seconds\n",
  62. argv[0] );
  63. return 1;
  64. }
  65. start = currticks();
  66. delay = strtoul ( argv[1], NULL, 0 ) * ticks_per_sec();
  67. while ( ( currticks() - start ) <= delay )
  68. cpu_nap();
  69. return 0;
  70. }
  71. struct command sleep_command __command = {
  72. .name = "sleep",
  73. .exec = sleep_exec,
  74. };