bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new module 'ptsname'


From: Bruno Haible
Subject: new module 'ptsname'
Date: Sun, 21 Mar 2010 16:16:59 +0100
User-agent: KMail/1.9.9

The next pty related function that gnulib can provide is 'ptsname'. It's only
lacking on MacOS X 10.3 and OpenBSD. The glibc has an implementation for this
situation.


2010-03-21  Bruno Haible  <address@hidden>

        New module 'ptsname'.
        * lib/ptsname.c: New file, from glibc with modifications.
        * m4/ptsname.m4: New file.
        * modules/ptsname: New file.
        * lib/stdlib.in.h (ptsname): New declaration.
        * m4/stdlib_h.m4 (gl_STDLIB_H): Check whether ptsname is declared.
        (gl_STDLIB_H_DEFAULTS): Initialize GNULIB_PTSNAME, HAVE_PTSNAME.
        * modules/stdlib (Makefile.am): Substitute GNULIB_PTSNAME,
        HAVE_PTSNAME.
        * doc/posix-functions/ptsname.texi: Mention the new module.
        * tests/test-stdlib-c++.cc: Check GNULIB_NAMESPACE::ptsname.
        * config/srclist.txt: Add ptsname.c (commented).

================================ lib/ptsname.c ================================
/* Determine name of the slave side of a pseudo-terminal.
   Copyright (C) 1998, 2002, 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 <http://www.gnu.org/licenses/>.  */

#include <config.h>

#include <stdlib.h>

#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#ifdef _LIBC
# include <paths.h>
#else
# ifndef _PATH_TTY
#  define _PATH_TTY "/dev/tty"
# endif
# ifndef _PATH_DEV
#  define _PATH_DEV "/dev/"
# endif

# define __set_errno(e) errno = (e)
# define __isatty isatty
# define __stat stat
# define __ttyname_r ttyname_r

static int __ptsname_r (int fd, char *buf, size_t buflen);
#endif


/* Static buffer for `ptsname'.  */
static char buffer[sizeof (_PATH_TTY) + 2];


/* Return the pathname of the pseudo terminal slave associated with
   the master FD is open on, or NULL on errors.
   The returned storage is good until the next call to this function.  */
char *
ptsname (int fd)
{
  return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
}


/* Store at most BUFLEN characters of the pathname of the slave pseudo
   terminal associated with the master FD is open on in BUF.
   Return 0 on success, otherwise an error number.  */
static int
__ptsname_r (int fd, char *buf, size_t buflen)
{
  int save_errno = errno;
  struct stat st;

  if (buf == NULL)
    {
      __set_errno (EINVAL);
      return EINVAL;
    }

  if (!__isatty (fd))
    /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY).  */
    return errno;

  if (buflen < strlen (_PATH_TTY) + 3)
    {
      __set_errno (ERANGE);
      return ERANGE;
    }

  if (__ttyname_r (fd, buf, buflen) != 0)
    return errno;

  buf[sizeof (_PATH_DEV) - 1] = 't';

  if (__stat (buf, &st) < 0)
    return errno;

  __set_errno (save_errno);
  return 0;
}
================================ m4/ptsname.m4 ================================
# ptsname.m4 serial 1
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,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_PTSNAME],
[
  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])

  dnl Persuade glibc <stdlib.h> to declare ptsname().
  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])

  AC_CHECK_FUNCS([ptsname])
  if test $ac_cv_func_ptsname = no; then
    HAVE_PTSNAME=0
    AC_LIBOBJ([ptsname])
    gl_PREREQ_PTSNAME
  fi
])

# Prerequisites of lib/ptsname.c.
AC_DEFUN([gl_PREREQ_PTSNAME], [
  :
])
=============================== modules/ptsname ===============================
Description:
ptsname() function: Determine name of the slave side of a pseudo-terminal.

Files:
lib/ptsname.c
m4/ptsname.m4

Depends-on:
stdlib
extensions
ttyname_r

configure.ac:
gl_FUNC_PTSNAME
gl_STDLIB_MODULE_INDICATOR([ptsname])

Makefile.am:

Include:
<stdlib.h>

License:
LGPL

Maintainer:
Bruno Haible
===============================================================================
--- lib/stdlib.in.h.orig        Sun Mar 21 16:10:15 2010
+++ lib/stdlib.in.h     Sun Mar 21 14:51:11 2010
@@ -349,6 +349,22 @@
 # endif
 #endif
 
+#if @GNULIB_PTSNAME@
+/* Return the pathname of the pseudo-terminal slave associated with
+   the master FD is open on, or NULL on errors.  */
+# if address@hidden@
+_GL_FUNCDECL_SYS (ptsname, char *, (int fd));
+# endif
+_GL_CXXALIAS_SYS (ptsname, char *, (int fd));
+_GL_CXXALIASWARN (ptsname);
+#elif defined GNULIB_POSIXCHECK
+# undef ptsname
+# if HAVE_RAW_DECL_PTSNAME
+_GL_WARN_ON_USE (ptsname, "ptsname is not portable - "
+                 "use gnulib module ptsname for portability");
+# endif
+#endif
+
 #if @GNULIB_PUTENV@
 # if @REPLACE_PUTENV@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
--- m4/stdlib_h.m4.orig Sun Mar 21 16:10:15 2010
+++ m4/stdlib_h.m4      Sun Mar 21 14:53:22 2010
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 23
+# stdlib_h.m4 serial 24
 dnl Copyright (C) 2007-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,
@@ -34,7 +34,7 @@
 # include <random.h>
 #endif
     ]], [atoll canonicalize_file_name getloadavg getsubopt mkdtemp
-    mkostemp mkostemps mkstemp mkstemps random_r initstat_r srandom_r
+    mkostemp mkostemps mkstemp mkstemps ptsname random_r initstat_r srandom_r
     setstate_r realpath rpmatch setenv strtod strtoll strtoull unsetenv])
 ])
 
@@ -60,6 +60,7 @@
   GNULIB_MKOSTEMPS=0;     AC_SUBST([GNULIB_MKOSTEMPS])
   GNULIB_MKSTEMP=0;       AC_SUBST([GNULIB_MKSTEMP])
   GNULIB_MKSTEMPS=0;      AC_SUBST([GNULIB_MKSTEMPS])
+  GNULIB_PTSNAME=0;       AC_SUBST([GNULIB_PTSNAME])
   GNULIB_PUTENV=0;        AC_SUBST([GNULIB_PUTENV])
   GNULIB_RANDOM_R=0;      AC_SUBST([GNULIB_RANDOM_R])
   GNULIB_REALLOC_POSIX=0; AC_SUBST([GNULIB_REALLOC_POSIX])
@@ -81,6 +82,7 @@
   HAVE_MKOSTEMP=1;           AC_SUBST([HAVE_MKOSTEMP])
   HAVE_MKOSTEMPS=1;          AC_SUBST([HAVE_MKOSTEMPS])
   HAVE_MKSTEMPS=1;           AC_SUBST([HAVE_MKSTEMPS])
+  HAVE_PTSNAME=1;            AC_SUBST([HAVE_PTSNAME])
   HAVE_RANDOM_R=1;           AC_SUBST([HAVE_RANDOM_R])
   HAVE_REALLOC_POSIX=1;      AC_SUBST([HAVE_REALLOC_POSIX])
   HAVE_REALPATH=1;           AC_SUBST([HAVE_REALPATH])
--- modules/stdlib.orig Sun Mar 21 16:10:15 2010
+++ modules/stdlib      Sun Mar 21 14:54:51 2010
@@ -39,6 +39,7 @@
              -e 's|@''GNULIB_MKOSTEMPS''@|$(GNULIB_MKOSTEMPS)|g' \
              -e 's|@''GNULIB_MKSTEMP''@|$(GNULIB_MKSTEMP)|g' \
              -e 's|@''GNULIB_MKSTEMPS''@|$(GNULIB_MKSTEMPS)|g' \
+             -e 's|@''GNULIB_PTSNAME''@|$(GNULIB_PTSNAME)|g' \
              -e 's|@''GNULIB_PUTENV''@|$(GNULIB_PUTENV)|g' \
              -e 's|@''GNULIB_RANDOM_R''@|$(GNULIB_RANDOM_R)|g' \
              -e 's|@''GNULIB_REALLOC_POSIX''@|$(GNULIB_REALLOC_POSIX)|g' \
@@ -59,6 +60,7 @@
              -e 's|@''HAVE_MKOSTEMP''@|$(HAVE_MKOSTEMP)|g' \
              -e 's|@''HAVE_MKOSTEMPS''@|$(HAVE_MKOSTEMPS)|g' \
              -e 's|@''HAVE_MKSTEMPS''@|$(HAVE_MKSTEMPS)|g' \
+             -e 's|@''HAVE_PTSNAME''@|$(HAVE_PTSNAME)|g' \
              -e 's|@''HAVE_RANDOM_H''@|$(HAVE_RANDOM_H)|g' \
              -e 's|@''HAVE_RANDOM_R''@|$(HAVE_RANDOM_R)|g' \
              -e 's|@''HAVE_REALLOC_POSIX''@|$(HAVE_REALLOC_POSIX)|g' \
--- doc/posix-functions/ptsname.texi.orig       Sun Mar 21 16:10:15 2010
+++ doc/posix-functions/ptsname.texi    Sun Mar 21 14:46:06 2010
@@ -4,15 +4,15 @@
 
 POSIX specification: 
@url{http://www.opengroup.org/onlinepubs/9699919799/functions/ptsname.html}
 
-Gnulib module: ---
+Gnulib module: ptsname
 
 Portability problems fixed by Gnulib:
 @itemize
address@hidden
+This function is missing on some platforms:
+MacOS X 10.3, OpenBSD 3.8, mingw, BeOS.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
address@hidden
-This function is missing on some platforms:
-MacOS X 10.3, OpenBSD 3.8, mingw, BeOS.
 @end itemize
--- tests/test-stdlib-c++.cc.orig       Sun Mar 21 16:10:15 2010
+++ tests/test-stdlib-c++.cc    Sun Mar 21 14:55:34 2010
@@ -72,6 +72,10 @@
 SIGNATURE_CHECK (GNULIB_NAMESPACE::mkstemps, int, (char *, int));
 #endif
 
+#if GNULIB_PTSNAME
+SIGNATURE_CHECK (GNULIB_NAMESPACE::ptsname, char *, (int));
+#endif
+
 #if GNULIB_PUTENV
 SIGNATURE_CHECK (GNULIB_NAMESPACE::putenv, int, (char *));
 #endif
--- config/srclist.txt.orig     Sun Mar 21 16:10:15 2010
+++ config/srclist.txt  Sun Mar 21 15:07:06 2010
@@ -213,6 +213,7 @@
 #$LIBCSRC/sysdeps/posix/euidaccess.c   lib gpl
 #$LIBCSRC/sysdeps/posix/tempname.c     lib gpl
 #$LIBCSRC/sysdeps/unix/bsd/poll.c      lib gpl
+#$LIBCSRC/sysdeps/unix/bsd/ptsname.c   lib gpl
 #$LIBCSRC/sysdeps/unix/dirfd.c         lib gpl
 #$LIBCSRC/sysdeps/unix/rmdir.c         lib gpl
 #$LIBCSRC/time/strftime.c              lib gpl




reply via email to

[Prev in Thread] Current Thread [Next in Thread]