Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

time_cmd.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. FILE_LICENCE ( GPL2_OR_LATER );
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <ipxe/command.h>
  27. #include <ipxe/parseopt.h>
  28. #include <ipxe/nap.h>
  29. #include <ipxe/timer.h>
  30. /** @file
  31. *
  32. * Time commands
  33. *
  34. */
  35. /** "time" options */
  36. struct time_options {};
  37. /** "time" option list */
  38. static struct option_descriptor time_opts[] = {};
  39. /** "time" command descriptor */
  40. static struct command_descriptor time_cmd =
  41. COMMAND_DESC ( struct time_options, time_opts, 1, MAX_ARGUMENTS,
  42. "<command>" );
  43. /**
  44. * "time" command
  45. *
  46. * @v argc Argument count
  47. * @v argv Argument list
  48. * @ret rc Return status code
  49. */
  50. static int time_exec ( int argc, char **argv ) {
  51. struct time_options opts;
  52. unsigned long start;
  53. int secs;
  54. int rc;
  55. /* Parse options */
  56. if ( ( rc = parse_options ( argc, argv, &time_cmd, &opts ) ) != 0 )
  57. return rc;
  58. start = currticks();
  59. rc = execv ( argv[1], argv + 1 );
  60. secs = (currticks() - start) / ticks_per_sec();
  61. printf ( "%s: %ds\n", argv[0], secs );
  62. return rc;
  63. }
  64. /** "time" command */
  65. struct command time_command __command = {
  66. .name = "time",
  67. .exec = time_exec,
  68. };
  69. /** "sleep" options */
  70. struct sleep_options {};
  71. /** "sleep" option list */
  72. static struct option_descriptor sleep_opts[] = {};
  73. /** "sleep" command descriptor */
  74. static struct command_descriptor sleep_cmd =
  75. COMMAND_DESC ( struct sleep_options, sleep_opts, 1, 1, "<seconds>" );
  76. /**
  77. * "sleep" command
  78. *
  79. * @v argc Argument count
  80. * @v argv Argument list
  81. * @ret rc Return status code
  82. */
  83. static int sleep_exec ( int argc, char **argv ) {
  84. struct sleep_options opts;
  85. unsigned long start, delay;
  86. int rc;
  87. /* Parse options */
  88. if ( ( rc = parse_options ( argc, argv, &sleep_cmd, &opts ) ) != 0 )
  89. return rc;
  90. start = currticks();
  91. delay = strtoul ( argv[1], NULL, 0 ) * ticks_per_sec();
  92. while ( ( currticks() - start ) <= delay )
  93. cpu_nap();
  94. return 0;
  95. }
  96. /** "sleep" command */
  97. struct command sleep_command __command = {
  98. .name = "sleep",
  99. .exec = sleep_exec,
  100. };