diff --git a/examples/server.c b/examples/server.c index 7c4986e..d8d2381 100644 --- a/examples/server.c +++ b/examples/server.c @@ -55,7 +55,6 @@ int main(int argc, char **argv) { if (isatty(info.fd) && grantpt(info.fd) == 0 && unlockpt(info.fd) == 0) { char name[256]; - struct termios t; if (ptsname_r(info.fd, name, sizeof(name)) < 0) { fprintf(stderr, "%s: %s: %s: %s\n", @@ -64,22 +63,6 @@ int main(int argc, char **argv) { goto error_open; } - if (tcgetattr(info.fd, &t) < 0) { - fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], argv[1], "tcgetattr()", strerror(errno)); - - goto error_open; - } - - cfmakeraw(&t); - - if (tcsetattr(info.fd, TCSANOW, &t) < 0) { - fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], argv[1], "tcsetattr()", strerror(errno)); - - goto error_open; - } - fprintf(stderr, "pts %s\n", name); } diff --git a/src/kiss.c b/src/kiss.c index d7047c3..61cf3cf 100644 --- a/src/kiss.c +++ b/src/kiss.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,9 @@ enum tnc_opts { }; struct _patty_kiss_tnc { + struct termios attrs, + attrs_old; + int fd, opts; @@ -48,6 +52,18 @@ patty_kiss_tnc *patty_kiss_tnc_new_fd(int fd) { goto error_malloc_buf; } + if (tcgetattr(fd, &tnc->attrs) < 0) { + goto error_tcgetattr; + } + + memcpy(&tnc->attrs_old, &tnc->attrs, sizeof(tnc->attrs_old)); + + cfmakeraw(&tnc->attrs); + + if (tcsetattr(fd, TCSANOW, &tnc->attrs) < 0) { + goto error_tcsetattr; + } + tnc->fd = fd; tnc->opts = TNC_NONE; tnc->bufsz = PATTY_KISS_BUFSZ; @@ -57,6 +73,8 @@ patty_kiss_tnc *patty_kiss_tnc_new_fd(int fd) { return tnc; +error_tcsetattr: +error_tcgetattr: error_malloc_buf: free(tnc); @@ -123,6 +141,8 @@ int patty_kiss_tnc_fd(patty_kiss_tnc *tnc) { } void patty_kiss_tnc_destroy(patty_kiss_tnc *tnc) { + (void)tcsetattr(tnc->fd, TCSANOW, &tnc->attrs_old); + if (tnc->opts & TNC_CLOSE_ON_DESTROY) { close(tnc->fd); }