summaryrefslogtreecommitdiff
path: root/lib/ptsname_r.c
diff options
context:
space:
mode:
authorEric Blake <eblake@redhat.com>2011-11-07 15:26:08 -0700
committerEric Blake <eblake@redhat.com>2011-11-09 15:53:07 -0700
commit78232883c8c01a22307bb38793928f2b9a5a6159 (patch)
treeeb53e0effab97294bfc73767a34cc5270f5b99c0 /lib/ptsname_r.c
parentc025e2c9516ad949e0b8c97aacc2dd811909d877 (diff)
downloadgnulib-78232883c8c01a22307bb38793928f2b9a5a6159.tar.gz
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>
Diffstat (limited to 'lib/ptsname_r.c')
-rw-r--r--lib/ptsname_r.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/lib/ptsname_r.c b/lib/ptsname_r.c
new file mode 100644
index 0000000000..e7f2d5b9ef
--- /dev/null
+++ b/lib/ptsname_r.c
@@ -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;
+}