sendbrk.c 767 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <termios.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. const char *_progname;
  9. static int send_break(const char *port)
  10. {
  11. int err;
  12. int fd = open(port, O_RDWR|O_NOCTTY|O_NONBLOCK);
  13. if (fd < 0)
  14. goto fail;
  15. tcdrain(fd);
  16. do {
  17. err = tcsendbreak(fd, 0);
  18. } while (err && errno == EAGAIN);
  19. if (err)
  20. goto fail;
  21. close(fd);
  22. return 0;
  23. fail:
  24. fprintf(stderr, "%s: %s: %s\n", _progname, port, strerror(errno));
  25. if (fd >= 0)
  26. close(fd);
  27. return -1;
  28. }
  29. int main(int argc, char *argv[])
  30. {
  31. _progname = argv[0];
  32. if (argc != 2) {
  33. fprintf(stderr, "Usage: %s port\n", _progname);
  34. exit(1);
  35. }
  36. send_break(argv[1]);
  37. return 0;
  38. }