binlog.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * This file is Copyright 2010 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. */
  5. #include <assert.h>
  6. #include <err.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <termios.h>
  15. #include <time.h> /* For nanosleep() */
  16. #include <unistd.h>
  17. void spinner(int );
  18. int main(int argc, char **argv) {
  19. int speed, n, ifd, ofd;
  20. struct termios term;
  21. char buf[BUFSIZ];
  22. struct timespec delay;
  23. if (argc != 4){
  24. fprintf(stderr, "usage: binlog <speed> <port> <logfile>\n");
  25. return 1;
  26. }
  27. speed = atoi(argv[1]);
  28. switch (speed) {
  29. case 230400:
  30. case 115200:
  31. case 57600:
  32. case 38400:
  33. case 28800:
  34. case 19200:
  35. case 14400:
  36. case 9600:
  37. case 4800:
  38. break;
  39. default:
  40. fprintf(stderr, "invalid speed\n");
  41. return 1;
  42. }
  43. if ((ifd = open(argv[2], O_RDWR | O_NONBLOCK | O_NOCTTY, 0644)) == -1)
  44. err(1, "open");
  45. if ((ofd = open(argv[3], O_RDWR | O_CREAT | O_APPEND, 0644)) == -1)
  46. err(1, "open");
  47. tcgetattr(ifd, &term);
  48. cfmakeraw(&term);
  49. cfsetospeed(&term, speed);
  50. cfsetispeed(&term, speed);
  51. if (tcsetattr(ifd, TCSANOW | TCSAFLUSH, &term) == -1)
  52. err(1, "tcsetattr");
  53. tcflush(ifd, TCIOFLUSH);
  54. n = 0;
  55. while (1){
  56. int l = read(ifd, buf, BUFSIZ);
  57. if (l > 0)
  58. assert(write(ofd, buf, l) > 0);
  59. /* wait 1,000 uSec */
  60. delay.tv_sec = 0;
  61. delay.tv_nsec = 1000000L;
  62. nanosleep(&delay, NULL);
  63. memset(buf, 0, BUFSIZ);
  64. spinner( n++ );
  65. }
  66. /* NOTREACHED */
  67. close(ifd);
  68. close(ofd);
  69. return 0;
  70. }
  71. void spinner(int n){
  72. char *s = "|/-\\";
  73. if (n % 4)
  74. return;
  75. n /= 4;
  76. fprintf(stderr, "\010\010\010\010\010\010\010\010\010\010\010\010\010");
  77. fprintf(stderr, "%c %d", s[n%4], n);
  78. fflush(stderr);
  79. }