From d6f9917389ea73a7839960fa3174a5bb8dc6cf36 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 2 Jan 2023 04:16:50 +0000 Subject: [PATCH] use ttyname_r if ptsname_r is not available --- bin/te.c | 11 ++++++++--- lib/pty.c | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/bin/te.c b/bin/te.c index d33406d..73f990f 100644 --- a/bin/te.c +++ b/bin/te.c @@ -180,9 +180,9 @@ int x11_setup(struct X11 *x11) x11->buf_x = 0; x11->buf_y = 0; x11->buf = malloc(x11->buf_w * x11->buf_h); - if (x11->buf == NULL) + if (!x11->buf ) { - perror("calloc"); + fprintf(stderr, "Could not allocate terminal buffer\n"); return -1; } memset (x11->buf, ' ', x11->buf_w * x11->buf_h); @@ -363,7 +363,7 @@ int main() hio = hio_open(HIO_NULL, 0, HIO_NULL, HIO_FEATURE_ALL, 512, HIO_NULL); if (!hio) { - printf ("Cannot open hio\n"); + fprintf (stderr, "Error: Unable to open hio\n"); return -1; } @@ -382,6 +382,11 @@ int main() pi.on_read = pty_on_read; pi.on_close = pty_on_close; pty = hio_dev_pty_make(hio, HIO_SIZEOF(*pair), &pi); + if (!pty) + { + fprintf (stderr, "Error: Unable to create pty - %s\n", hio_geterrbmsg(hio)); + return -1; + } pair = hio_dev_pty_getxtn(pty); pair->x11 = &x11; diff --git a/lib/pty.c b/lib/pty.c index ad27719..c0022fd 100644 --- a/lib/pty.c +++ b/lib/pty.c @@ -38,6 +38,13 @@ #if defined(HAVE_PTY_H) # include #endif +#if defined(HAVE_PATHS_H) +# include +#endif + +#if !defined(_PATH_DEV) +# define _PATH_DEV "/dev/" +#endif /* ========================================================================= */ @@ -215,7 +222,11 @@ static int dev_pty_make (hio_dev_t* dev, void* ctx) hio_oow_t capa = HIO_COUNTOF(pts_name_buf); /* open a pty slave device name */ +#if defined(HAVE_PTSNAME_R) if (ptsname_r(pfds[0], ptr, capa) != 0) +#else + if (ttyname_r(pfds[0], ptr, capa) != 0) +#endif { if (errno == ERANGE) { @@ -233,6 +244,11 @@ static int dev_pty_make (hio_dev_t* dev, void* ctx) goto oops; } +#if !defined(HAVE_PTSNAME_R) + /* ttyname_r() gives something like /dev/ptyp0. change the leading p to t */ + ptr[HIO_SIZEOF(_PATH_DEV) - 1] = 't'; +#endif + /* open a pty slave */ pfds[1] = open(ptr, O_RDWR | O_NOCTTY); if (pfds[1] == -1)