From 0ecb606cb6cf65de1d9fc8a919bceb4be476c602 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Thu, 12 Jul 2007 18:26:36 +0000 Subject: 2.5-18.1 --- sysdeps/unix/sysv/linux/ttyname_r.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'sysdeps/unix/sysv/linux/ttyname_r.c') diff --git a/sysdeps/unix/sysv/linux/ttyname_r.c b/sysdeps/unix/sysv/linux/ttyname_r.c index eee4d862b2..cef8624dc6 100644 --- a/sysdeps/unix/sysv/linux/ttyname_r.c +++ b/sysdeps/unix/sysv/linux/ttyname_r.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991,92,93,1995-2001, 2003 Free Software Foundation, Inc. +/* Copyright (C) 1991,92,93,1995-2001,2003,2006 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -22,18 +22,20 @@ #include #include #include +#include #include #include #include #include +#include static int getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino, int save, int *dostat) internal_function; static int -internal_function +internal_function attribute_compat_text_section getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino, int save, int *dostat) { @@ -99,7 +101,6 @@ __ttyname_r (int fd, char *buf, size_t buflen) struct stat64 st, st1; int dostat = 0; int save = errno; - int ret; /* Test for the absolute minimal size. This makes life easier inside the loop. */ @@ -115,29 +116,34 @@ __ttyname_r (int fd, char *buf, size_t buflen) return ERANGE; } + /* isatty check, tcgetattr is used because it sets the correct + errno (EBADF resp. ENOTTY) on error. */ + struct termios term; + if (__builtin_expect (__tcgetattr (fd, &term) < 0, 0)) + return errno; + /* We try using the /proc filesystem. */ *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0'; - ret = __readlink (procname, buf, buflen - 1); - if (ret == -1 && errno == ENOENT) + ssize_t ret = __readlink (procname, buf, buflen - 1); + if (__builtin_expect (ret == -1 && errno == ENOENT, 0)) { __set_errno (EBADF); return EBADF; } - if (!__isatty (fd)) - { - __set_errno (ENOTTY); - return ENOTTY; - } - - if (ret == -1 && errno == ENAMETOOLONG) + if (__builtin_expect (ret == -1 && errno == ENAMETOOLONG, 0)) { __set_errno (ERANGE); return ERANGE; } - if (ret != -1 && buf[0] != '[') + if (__builtin_expect (ret != -1 +#ifndef __ASSUME_PROC_SELF_FD_SYMLINK + /* This is for Linux 2.0. */ + && buf[0] != '[' +#endif + , 1)) { buf[ret] = '\0'; return 0; -- cgit v1.2.3-70-g09d2