scriptreplay.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. //config:config SCRIPTREPLAY
  10. //config: bool "scriptreplay (2.6 kb)"
  11. //config: default y
  12. //config: help
  13. //config: This program replays a typescript, using timing information
  14. //config: given by script -t.
  15. //applet:IF_SCRIPTREPLAY(APPLET(scriptreplay, BB_DIR_BIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_SCRIPTREPLAY) += scriptreplay.o
  17. //usage:#define scriptreplay_trivial_usage
  18. //usage: "TIMINGFILE [TYPESCRIPT [DIVISOR]]"
  19. //usage:#define scriptreplay_full_usage "\n\n"
  20. //usage: "Play back typescripts, using timing information"
  21. #include "libbb.h"
  22. int scriptreplay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  23. int scriptreplay_main(int argc UNUSED_PARAM, char **argv)
  24. {
  25. const char *script = "typescript";
  26. double delay, factor = 1000000.0;
  27. int fd;
  28. unsigned long count;
  29. FILE *tfp;
  30. if (!argv[1])
  31. bb_show_usage();
  32. if (argv[2]) {
  33. script = argv[2];
  34. if (argv[3])
  35. factor /= atof(argv[3]);
  36. }
  37. tfp = xfopen_for_read(argv[1]);
  38. fd = xopen(script, O_RDONLY);
  39. while (fscanf(tfp, "%lf %lu\n", &delay, &count) == 2) {
  40. usleep(delay * factor);
  41. bb_copyfd_exact_size(fd, STDOUT_FILENO, count);
  42. }
  43. if (ENABLE_FEATURE_CLEAN_UP) {
  44. close(fd);
  45. fclose(tfp);
  46. }
  47. return EXIT_SUCCESS;
  48. }