From 8e7bc4d9d435c2d18be77134896aa823733816a0 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 18 Mar 2010 16:34:11 -0600 Subject: [PATCH] forkpty, openpty: prefer glibc's const-safe prototype This silences a compiler warning for test-forkpty.c. * lib/forkpty.c (rpl_forkpty): New file. * lib/openpty.c (rpl_openpty): Likewise. * modules/forkpty (Files): Distribute it. * modules/openpty (Files): Likewise. * m4/pty_h.m4 (gl_PTY_H_DEFAULTS): Add new witnesses. Move decl check... * m4/pty.m4 (gl_FORKPTY, gl_OPENPTY): ...here. Request replacement for for non-const BSD signature. * modules/pty (Makefile.am): Substitute witnesses. * lib/pty.in.h (forkpty, openpty): Declare replacements. * tests/test-forkpty.c: Update signature check. * tests/test-openpty.c: Likewise. * doc/glibc-functions/forkpty.texi (forkpty): Document the fix. * doc/glibc-functions/openpty.texi (openpty): Likewise. Reported by Bruno Haible. Signed-off-by: Eric Blake --- ChangeLog | 16 ++++++++ doc/glibc-functions/forkpty.texi | 4 ++ doc/glibc-functions/openpty.texi | 4 ++ lib/forkpty.c | 35 +++++++++++++++++ lib/openpty.c | 35 +++++++++++++++++ lib/pty.in.h | 34 +++++++++++++++++ m4/pty.m4 | 82 +++++++++++++++++++++++++++++++++++++++- m4/pty_h.m4 | 16 ++------ modules/forkpty | 1 + modules/openpty | 1 + modules/pty | 2 + tests/test-openpty.c | 4 +- 12 files changed, 218 insertions(+), 16 deletions(-) create mode 100644 lib/forkpty.c create mode 100644 lib/openpty.c diff --git a/ChangeLog b/ChangeLog index 8c1820aa8..b42d244c7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,21 @@ 2010-03-19 Eric Blake + forkpty, openpty: prefer glibc's const-safe prototype + * lib/forkpty.c (rpl_forkpty): New file. + * lib/openpty.c (rpl_openpty): Likewise. + * modules/forkpty (Files): Distribute it. + * modules/openpty (Files): Likewise. + * m4/pty_h.m4 (gl_PTY_H_DEFAULTS): Add new witnesses. Move decl + check... + * m4/pty.m4 (gl_FORKPTY, gl_OPENPTY): ...here. Request + replacement for for non-const BSD signature. + * modules/pty (Makefile.am): Substitute witnesses. + * lib/pty.in.h (forkpty, openpty): Declare replacements. + * tests/test-forkpty.c: Update signature check. + * tests/test-openpty.c: Likewise. + * doc/glibc-functions/forkpty.texi (forkpty): Document the fix. + * doc/glibc-functions/openpty.texi (openpty): Likewise. + forkpty, openpty: split functions into new modules * modules/pty (Makefile.am): Substitute new witnesses. (Libraries): Move library detection... diff --git a/doc/glibc-functions/forkpty.texi b/doc/glibc-functions/forkpty.texi index 8a8651158..aecc2329e 100644 --- a/doc/glibc-functions/forkpty.texi +++ b/doc/glibc-functions/forkpty.texi @@ -16,6 +16,10 @@ required. The function is declared in pty.h on Cygwin, Interix, OSF/1 4 and 5, and glibc. It is declared in util.h on Mac OS X, OpenBSD and NetBSD. It is declared in libutil.h on FreeBSD. +@item +Some platforms declare the function without marking the last two +parameters @code{const}. +FreeBSD, Cygwin 1.7.1. @end itemize Portability problems not fixed by Gnulib: diff --git a/doc/glibc-functions/openpty.texi b/doc/glibc-functions/openpty.texi index 7eb528efd..5eb57c353 100644 --- a/doc/glibc-functions/openpty.texi +++ b/doc/glibc-functions/openpty.texi @@ -16,6 +16,10 @@ required. The function is declared in pty.h on Cygwin, Interix, OSF/1 4 and 5, and glibc. It is declared in util.h on Mac OS X, OpenBSD and NetBSD. It is declared in libutil.h on FreeBSD. +@item +Some platforms declare the function without marking the last two +parameters @code{const}. +FreeBSD, Cygwin 1.7.1. @end itemize Portability problems not fixed by Gnulib: diff --git a/lib/forkpty.c b/lib/forkpty.c new file mode 100644 index 000000000..adbc3d551 --- /dev/null +++ b/lib/forkpty.c @@ -0,0 +1,35 @@ +/* Fork a child attached to a pseudo-terminal descriptor. + Copyright (C) 2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Specification. */ +#include + +#if HAVE_DECL_FORKPTY +# undef forkpty +int +rpl_forkpty (int *amaster, char *name, struct termios const *termp, + struct winsize const *winp) +{ + /* Cast away const, for implementations with weaker prototypes. */ + return forkpty (amaster, name, (struct termios *) termp, + (struct winsize *) winp); +} +#else +# error forkpty has not been ported to your system; \ + report this to bug-gnulib@gnu.org for help +#endif diff --git a/lib/openpty.c b/lib/openpty.c new file mode 100644 index 000000000..e7eb46d46 --- /dev/null +++ b/lib/openpty.c @@ -0,0 +1,35 @@ +/* Open a pseudo-terminal descriptor. + Copyright (C) 2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* Specification. */ +#include + +#if HAVE_DECL_OPENPTY +# undef openpty +int +rpl_openpty (int *amaster, int *aslave, char *name, struct termios const *termp, + struct winsize const *winp) +{ + /* Cast away const, for implementations with weaker prototypes. */ + return openpty (amaster, aslave, name, (struct termios *) termp, + (struct winsize *) winp); +} +#else +# error openpty has not been ported to your system; \ + report this to bug-gnulib@gnu.org for help +#endif diff --git a/lib/pty.in.h b/lib/pty.in.h index 1b7bf8205..2780d6141 100644 --- a/lib/pty.in.h +++ b/lib/pty.in.h @@ -45,6 +45,23 @@ /* Declare overridden functions. */ #if @GNULIB_FORKPTY@ +# if @REPLACE_FORKPTY@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef forkpty +# define forkpty rpl_forkpty +# endif +_GL_FUNCDECL_RPL (forkpty, int, + (int *, char *, struct termios const *, + struct winsize const *)); +_GL_CXXALIAS_RPL (forkpty, int, + (int *, char *, struct termios const *, + struct winsize const *)); +# else +_GL_CXXALIAS_SYS (forkpty, int, + (int *, char *, struct termios const *, + struct winsize const *)); +# endif +_GL_CXXALIASWARN (forkpty); #elif defined GNULIB_POSIXCHECK # undef forkpty # if HAVE_RAW_DECL_FORKPTY @@ -54,6 +71,23 @@ _GL_WARN_ON_USE (forkpty, "forkpty is not declared consistently - " #endif #if @GNULIB_OPENPTY@ +# if @REPLACE_OPENPTY@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef openpty +# define openpty rpl_openpty +# endif +_GL_FUNCDECL_RPL (openpty, int, + (int *, int *, char *, struct termios const *, + struct winsize const *)); +_GL_CXXALIAS_RPL (openpty, int, + (int *, int *, char *, struct termios const *, + struct winsize const *)); +# else +_GL_CXXALIAS_SYS (openpty, int, + (int *, int *, char *, struct termios const *, + struct winsize const *)); +# endif +_GL_CXXALIASWARN (openpty); #elif defined GNULIB_POSIXCHECK # undef openpty # if HAVE_RAW_DECL_OPENPTY diff --git a/m4/pty.m4 b/m4/pty.m4 index f2a5664f8..d2f8110f1 100644 --- a/m4/pty.m4 +++ b/m4/pty.m4 @@ -1,4 +1,4 @@ -# pty.m4 serial 1 +# pty.m4 serial 2 dnl Copyright (C) 2010 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -23,9 +23,89 @@ AC_DEFUN([gl_PTY_LIB], AC_DEFUN([gl_FORKPTY], [ AC_REQUIRE([gl_PTY_LIB]) + AC_REQUIRE([gl_PTY]) + + AC_CHECK_DECLS([forkpty],,, [[ +#if HAVE_PTY_H +# include +#endif +#if HAVE_UTIL_H +# include +#endif +#if HAVE_LIBUTIL_H +# include +#endif +]]) + if test $ac_cv_have_decl_forkpty = no; then + AC_MSG_WARN([[Cannot find forkpty, build will likely fail]]) + fi + + dnl Prefer glibc's const-safe prototype, if available. + AC_CACHE_CHECK([for const-safe forkpty signature], + [gl_cv_func_forkpty_const], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[ +#if HAVE_PTY_H +# include +#endif +#if HAVE_UTIL_H +# include +#endif +#if HAVE_LIBUTIL_H +# include +#endif + ]], [[ + int forkpty (int *, char *, struct termios const *, + struct winsize const *); + ]])], + [gl_cv_func_forkpty_const=yes], [gl_cv_func_forkpty_const=no])]) + if test $gl_cv_func_forkpty_const != yes; then + REPLACE_FORKPTY=1 + AC_LIBOBJ([forkpty]) + fi ]) AC_DEFUN([gl_OPENPTY], [ AC_REQUIRE([gl_PTY_LIB]) + AC_REQUIRE([gl_PTY]) + + AC_CHECK_DECLS([openpty],,, [[ +#if HAVE_PTY_H +# include +#endif +#if HAVE_UTIL_H +# include +#endif +#if HAVE_LIBUTIL_H +# include +#endif +]]) + if test $ac_cv_have_decl_openpty = no; then + AC_MSG_WARN([[Cannot find openpty, build will likely fail]]) + fi + + dnl Prefer glibc's const-safe prototype, if available. + AC_CACHE_CHECK([for const-safe openpty signature], + [gl_cv_func_openpty_const], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[ +#if HAVE_PTY_H +# include +#endif +#if HAVE_UTIL_H +# include +#endif +#if HAVE_LIBUTIL_H +# include +#endif + ]], [[ + int openpty (int *, int *, char *, struct termios const *, + struct winsize const *); + ]])], + [gl_cv_func_openpty_const=yes], [gl_cv_func_openpty_const=no])]) + if test $gl_cv_func_openpty_const != yes; then + REPLACE_OPENPTY=1 + AC_LIBOBJ([openpty]) + fi ]) diff --git a/m4/pty_h.m4 b/m4/pty_h.m4 index 05ac3575c..cf41eab81 100644 --- a/m4/pty_h.m4 +++ b/m4/pty_h.m4 @@ -1,4 +1,4 @@ -# pty_h.m4 serial 3 +# pty_h.m4 serial 4 dnl Copyright (C) 2009, 2010 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -22,18 +22,6 @@ AC_DEFUN_ONCE([gl_PTY], if test $ac_cv_header_libutil_h = yes; then HAVE_LIBUTIL_H=1 fi - dnl FIXME - move this into forkpty module, when replacement is provided - AC_CHECK_DECLS([forkpty],,, [[ -#if HAVE_UTIL_H -# include -#endif -#if HAVE_LIBUTIL_H -# include -#endif -]]) - if test $ac_cv_have_decl_forkpty = no; then - AC_MSG_WARN([[Cannot find forkpty, build will likely fail]]) - fi else # Have , assume forkpty is declared there. HAVE_PTY_H=1 fi @@ -70,4 +58,6 @@ AC_DEFUN([gl_PTY_H_DEFAULTS], dnl Assume proper GNU behavior unless another module says otherwise. HAVE_UTIL_H=0; AC_SUBST([HAVE_UTIL_H]) HAVE_LIBUTIL_H=0; AC_SUBST([HAVE_LIBUTIL_H]) + REPLACE_FORKPTY=0; AC_SUBST([REPLACE_FORKPTY]) + REPLACE_OPENPTY=0; AC_SUBST([REPLACE_OPENPTY]) ]) diff --git a/modules/forkpty b/modules/forkpty index 5bfe0a5de..8fcb38219 100644 --- a/modules/forkpty +++ b/modules/forkpty @@ -2,6 +2,7 @@ Description: Provide the forkpty() function. Files: +lib/forkpty.c m4/pty.m4 Depends-on: diff --git a/modules/openpty b/modules/openpty index 68e00db4a..1608a8da4 100644 --- a/modules/openpty +++ b/modules/openpty @@ -2,6 +2,7 @@ Description: Provide the openpty() function. Files: +lib/openpty.c m4/pty.m4 Depends-on: diff --git a/modules/pty b/modules/pty index 8ea7ed3e3..9020a1a28 100644 --- a/modules/pty +++ b/modules/pty @@ -29,6 +29,8 @@ pty.h: pty.in.h $(CXXDEFS_H) $(WARN_ON_USE_H) -e 's|@''GNULIB_OPENPTY''@|$(GNULIB_OPENPTY)|g' \ -e 's|@''HAVE_UTIL_H''@|$(HAVE_UTIL_H)|g' \ -e 's|@''HAVE_LIBUTIL_H''@|$(HAVE_LIBUTIL_H)|g' \ + -e 's|@''REPLACE_FORKPTY''@|$(REPLACE_FORKPTY)|g' \ + -e 's|@''REPLACE_OPENPTY''@|$(REPLACE_OPENPTY)|g' \ -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ < $(srcdir)/pty.in.h; \ diff --git a/tests/test-openpty.c b/tests/test-openpty.c index 255aeb0dd..2af25b49c 100644 --- a/tests/test-openpty.c +++ b/tests/test-openpty.c @@ -21,8 +21,8 @@ #include #include "signature.h" -SIGNATURE_CHECK (openpty, int, (int *, int *, char *, struct termios *, - struct winsize *)); +SIGNATURE_CHECK (openpty, int, (int *, int *, char *, struct termios const *, + struct winsize const *)); #include -- 2.11.0