Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

time_cmd.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <ipxe/command.h>
  26. #include <ipxe/parseopt.h>
  27. #include <ipxe/nap.h>
  28. #include <ipxe/timer.h>
  29. /** @file
  30. *
  31. * Time commands
  32. *
  33. */
  34. /** "time" options */
  35. struct time_options {};
  36. /** "time" option list */
  37. static struct option_descriptor time_opts[] = {};
  38. /** "time" command descriptor */
  39. static struct command_descriptor time_cmd =
  40. COMMAND_DESC ( struct time_options, time_opts, 1, MAX_ARGUMENTS,
  41. "<command>", "Time a command" );
  42. /**
  43. * "time" command
  44. *
  45. * @v argc Argument count
  46. * @v argv Argument list
  47. * @ret rc Return status code
  48. */
  49. static int time_exec ( int argc, char **argv ) {
  50. struct time_options opts;
  51. unsigned long start;
  52. int secs;
  53. int rc;
  54. /* Parse options */
  55. if ( ( rc = parse_options ( argc, argv, &time_cmd, &opts ) ) != 0 )
  56. return rc;
  57. start = currticks();
  58. rc = execv ( argv[1], argv + 1 );
  59. secs = (currticks() - start) / ticks_per_sec();
  60. printf ( "%s: %ds\n", argv[0], secs );
  61. return rc;
  62. }
  63. /** "time" command */
  64. struct command time_command __command = {
  65. .name = "time",
  66. .exec = time_exec,
  67. };
  68. /** "sleep" options */
  69. struct sleep_options {};
  70. /** "sleep" option list */
  71. static struct option_descriptor sleep_opts[] = {};
  72. /** "sleep" command descriptor */
  73. static struct command_descriptor sleep_cmd =
  74. COMMAND_DESC ( struct sleep_options, sleep_opts, 1, 1,
  75. "<seconds>", "Sleep for <seconds> 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. };