Mercurial > gnulib
diff lib/ptsname_r.c @ 16067:c4b6df81c7b3
ptsname_r: new module
For now, this replacement focuses solely on compilation
compatibility, and assumes that isatty() and ttyname_r() work
on a master side pty; if this assumption fails, or if
thread-safety is also required, then a later patch can follow
the lead of strerror_r.c in wrapping the system ptsname()
with a lock.
* modules/ptsname_r: New module.
* m4/ptsname_r.m4 (gl_FUNC_PTSNAME_R): New file.
* lib/ptsname.c (__ptsname_r): Split...
* lib/ptsname_r.c: ...into new file.
* m4/stdlib_h.m4 (gl_STDLIB_H): Check for decl.
(gl_STDLIB_H_DEFAULTS): Set witness defaults.
* modules/stdlib (Makefile.am): Substitute witnesses.
* lib/stdlib.in.h (ptsname_r): Declare it.
* doc/glibc-functions/ptsname_r.texi (ptsname_r): Document it.
* MODULES.html.sh (Misc): Likewise.
* modules/ptsname (Depends-on): Alter dependency.
* doc/posix-functions/ptsname.texi (ptsname): Mention new module.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Mon, 07 Nov 2011 15:26:08 -0700 |
parents | |
children | 8250f2777afc |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ptsname_r.c Mon Nov 07 15:26:08 2011 -0700 @@ -0,0 +1,85 @@ +/* Determine name of the slave side of a pseudo-terminal. + Copyright (C) 1998, 2002, 2010-2011 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 +# define __ptsname_r ptsname_r + +#endif + + +/* 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. */ +int +__ptsname_r (int fd, char *buf, size_t buflen) +{ + int save_errno = errno; + int err; + 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; + } + + err = __ttyname_r (fd, buf, buflen); + if (err != 0) + { + __set_errno (err); + return errno; + } + + buf[sizeof (_PATH_DEV) - 1] = 't'; + + if (__stat (buf, &st) < 0) + return errno; + + __set_errno (save_errno); + return 0; +}