scriptreplay.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * scriptreplay - play back typescripts, using timing information
  4. *
  5. * pascal.bellard@ads-lu.com
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. *
  9. */
  10. //config:config SCRIPTREPLAY
  11. //config: bool "scriptreplay"
  12. //config: default y
  13. //config: help
  14. //config: This program replays a typescript, using timing information
  15. //config: given by script -t.
  16. //applet:IF_SCRIPTREPLAY(APPLET(scriptreplay, BB_DIR_BIN, BB_SUID_DROP))
  17. //kbuild:lib-$(CONFIG_SCRIPTREPLAY) += scriptreplay.o
  18. //usage:#define scriptreplay_trivial_usage
  19. //usage: "timingfile [typescript [divisor]]"
  20. //usage:#define scriptreplay_full_usage "\n\n"
  21. //usage: "Play back typescripts, using timing information"
  22. #include "libbb.h"
  23. int scriptreplay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  24. int scriptreplay_main(int argc UNUSED_PARAM, char **argv)
  25. {
  26. const char *script = "typescript";
  27. double delay, factor = 1000000.0;
  28. int fd;
  29. unsigned long count;
  30. FILE *tfp;
  31. if (!argv[1])
  32. bb_show_usage();
  33. if (argv[2]) {
  34. script = argv[2];
  35. if (argv[3])
  36. factor /= atof(argv[3]);
  37. }
  38. tfp = xfopen_for_read(argv[1]);
  39. fd = xopen(script, O_RDONLY);
  40. while (fscanf(tfp, "%lf %lu\n", &delay, &count) == 2) {
  41. usleep(delay * factor);
  42. bb_copyfd_exact_size(fd, STDOUT_FILENO, count);
  43. }
  44. if (ENABLE_FEATURE_CLEAN_UP) {
  45. close(fd);
  46. fclose(tfp);
  47. }
  48. return EXIT_SUCCESS;
  49. }