From: Philip Guenther Subject: Re: Implement tcgetwinsize and tcsetwinsize To: enh Cc: finxx , tech@openbsd.org Date: Sun, 23 Feb 2025 15:38:46 -0800 On Sun, Feb 23, 2025 at 3:24 PM enh wrote: > > On Sun, Feb 23, 2025 at 2:14 PM finxx wrote: > > > > tcgetwinsize(3) and tcsetwinsize(3) have existed in POSIX for years, > > well, since 2024's issue 8 anyway. > > > and > > been supported on the other BSDs for just as long. This patch implements > > them in libc. > > > > diff 11d817a3382201927643a2ae0a36dcc4048bfb0b HEAD > > commit - 11d817a3382201927643a2ae0a36dcc4048bfb0b > > commit + c077581a317e8f39c250c631d6d29b01a97e7f8a > > blob - 6eb2cca93e5f1c834a57a03af4a4b3fe27c64ce1 > > blob + 18cf144e7a06b88f2514407c57cda12d192d7709 > > --- lib/libc/Symbols.list > > +++ lib/libc/Symbols.list > > @@ -1700,9 +1700,11 @@ tcflush > > tcgetattr > > tcgetpgrp > > tcgetsid > > +tcgetwinsize > > tcsendbreak > > tcsetattr > > tcsetpgrp > > +tcsetwinsize > > > > /* thread */ > > _rthread_debug > > blob - 85c1d52273c9cb24478c2c7e67a79bbdd86dae51 > > blob + a1aff88ae34741f62149e88c69c93f6b5b7326c6 > > --- lib/libc/termios/Makefile.inc > > +++ lib/libc/termios/Makefile.inc > > @@ -4,6 +4,7 @@ > > > > SRCS+= cfgetispeed.c cfgetospeed.c cfmakeraw.c cfsetispeed.c cfsetospeed.c \ > > cfsetspeed.c tcdrain.c tcflow.c tcflush.c tcgetattr.c tcgetpgrp.c \ > > - tcsendbreak.c tcsetattr.c tcsetpgrp.c tcgetsid.c > > + tcsendbreak.c tcsetattr.c tcsetpgrp.c tcgetsid.c tcgetwinsize.c \ > > + tcsetwinsize.c > > > > MAN+= tcgetpgrp.3 tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 tcgetsid.3 > > blob - /dev/null > > blob + c41b49129a7c60230f20f727ce90f7e4b089cd30 (mode 644) > > --- /dev/null > > +++ lib/libc/termios/tcgetwinsize.c > > @@ -0,0 +1,24 @@ > > +/* > > + * Copyright (c) 2025 Finxx > > + * > > + * Permission to use, copy, modify, and distribute this software for any > > + * purpose with or without fee is hereby granted, provided that the above > > + * copyright notice and this permission notice appear in all copies. > > + * > > + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES > > + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF > > + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR > > + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES > > + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN > > + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF > > + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. > > + */ > > + > > +#include > > +#include > > + > > +int > > +tcgetwinsize(int fd, struct winsize *winsize_p) > > +{ > > + return (ioctl(fd, TIOCGWINSZ, winsize_p)); > > +} > > blob - /dev/null > > blob + 7e652d5f8457bb752b724c14cbfbdd9dc15f0564 (mode 644) > > --- /dev/null > > +++ lib/libc/termios/tcsetwinsize.c > > @@ -0,0 +1,24 @@ > > +/* > > + * Copyright (c) 2025 Finxx > > + * > > + * Permission to use, copy, modify, and distribute this software for any > > + * purpose with or without fee is hereby granted, provided that the above > > + * copyright notice and this permission notice appear in all copies. > > + * > > + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES > > + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF > > + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR > > + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES > > + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN > > + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF > > + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. > > + */ > > + > > +#include > > +#include > > + > > +int > > +tcsetwinsize(int fd, const struct winsize *winsize_p) > > +{ > > + return (ioctl(fd, TIOCSWINSZ, winsize_p)); > > +} > > blob - 681c29489138850507516a8ef6bc346bed04e4e9 > > blob + 62a2d7a676481ae3fd947c15e237d4a5b018c1ca > > --- sys/sys/termios.h > > +++ sys/sys/termios.h > > @@ -272,6 +272,8 @@ int tcdrain(int); > > int tcflow(int, int); > > int tcflush(int, int); > > int tcsendbreak(int, int); > > +int tcgetwinsize(int, struct winsize *); > > +int tcsetwinsize(int, const struct winsize *); > > > > #if __XPG_VISIBLE >= 420 || __POSIX_VISIBLE >= 200809 > > this makes me wonder whether tcgetwinsize()/tcsetwinsize() should be > guarded by __POSIX_VISIBLE too? Yes. There are other, visibility bits missing in libc, struct winsize needs to be moved to the specified header (also with proper guards), a manpage needs to be written, and then there's the hard part: verify the window size is reset on first open for all the terminal drivers. Philip Guenther