baudcat.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright (C) 2022 Matt Arnold
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  8. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
  9. DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  10. RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  11. CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  12. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdbool.h>
  19. #include <errno.h>
  20. int main(int argc, char **argv)
  21. {
  22. printf("%s", "\033[2J"); /* Clears the screen */
  23. printf("%s", "\033[H");
  24. long rate = 37;
  25. long slpt = 1;
  26. int opt;
  27. FILE *fp;
  28. bool havefile = false;
  29. char *fname;
  30. while ((opt = getopt(argc, argv, "r:t:f:")) != -1) {
  31. switch (opt) {
  32. case 'r':
  33. errno = 0;
  34. rate = strtol(optarg, NULL, 10);
  35. if (errno) {
  36. fprintf(stderr, "%s\n", strerror(errno));
  37. exit(1);
  38. }
  39. else {
  40. rate /= 8;
  41. }
  42. break;
  43. case 't':
  44. errno = 0;
  45. slpt = strtol(optarg, NULL, 10);
  46. if (errno) {
  47. fprintf(stderr, "%s\n", strerror(errno));
  48. exit(1);
  49. }
  50. break;
  51. case 'f':
  52. asprintf(&fname,"%s", optarg);
  53. havefile = true;
  54. break;
  55. }
  56. }
  57. int count = 0;
  58. char ch;
  59. if (havefile) {
  60. fp = fopen(fname, "r");
  61. }
  62. else {
  63. fp = stdin;
  64. }
  65. while (!feof(fp)) {
  66. if ( count == rate) {
  67. int isleep = (int) slpt;
  68. sleep(isleep);
  69. count = 0;
  70. }
  71. ch = getc(fp);
  72. putchar(ch);
  73. fflush(stdout);
  74. count++;
  75. }
  76. return 0;
  77. }