This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
[patch] cleanup ttyname and make it devfs friendly
- To: GNU C Library <libc-alpha at sourceware dot cygnus dot com>
- Subject: [patch] cleanup ttyname and make it devfs friendly
- From: Ben Collins <bcollins at debian dot org>
- Date: Mon, 29 Oct 2001 20:47:33 -0500
This is due to popular demand since ttyname doesn't work with sgid apps
with devfs only systems. Since I had to add /dev/vc/ to the list of
directories to search, I figured I'd clean things up a bit to make it
easier to read. Tested with current CVS.
2001-10-29 Ben Collins <bcollins@debian.org>
* sysdeps/unix/sysv/linux/ttyname.c(getttyname): Take a single
struct stat64* for an arg, so we can do all the work out the
_STATBUF_ST_RDEV in one place.
(ttyname): Update usage of getttyname(). Also cleanup the checks
for /dev/<subdir> searches and add /dev/vc/ to the search for
devfs only systems.
* sysdeps/unix/sysv/linux/ttyname_r.c: Likewise.
--
.----------=======-=-======-=========-----------=====------------=-=-----.
/ Ben Collins -- Debian GNU/Linux \
` bcollins@debian.org -- bcollins@openldap.org -- bcollins@linux.com '
`---=========------=======-------------=-=-----=-===-======-------=--=---'
--- sysdeps/unix/sysv/linux/ttyname_r.c~ 2001/07/06 04:56:13 1.14
+++ sysdeps/unix/sysv/linux/ttyname_r.c 2001/10/29 19:25:38
@@ -28,21 +28,24 @@
#include <stdio-common/_itoa.h>
-static int getttyname_r (char *buf, size_t buflen,
- dev_t mydev, ino64_t myino, int save,
+static int getttyname_r (const char *prefix, char *buf, size_t buflen,
+ struct stat64 *mystat, int save,
int *dostat) internal_function;
static int
internal_function
-getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino,
+getttyname_r (const char *prefix, char *buf, size_t buflen, struct stat64 *mystat,
int save, int *dostat)
{
struct stat64 st;
DIR *dirstream;
struct dirent64 *d;
- size_t devlen = strlen (buf);
+ size_t devlen = strlen (prefix);
- dirstream = __opendir (buf);
+ memcpy (buf, prefix, strlen (prefix));
+ buflen -= strlen (prefix) - 1;
+
+ dirstream = __opendir (prefix);
if (dirstream == NULL)
{
*dostat = -1;
@@ -50,7 +53,7 @@
}
while ((d = __readdir64 (dirstream)) != NULL)
- if ((d->d_fileno == myino || *dostat)
+ if ((d->d_fileno == mystat->st_ino || *dostat)
&& strcmp (d->d_name, "stdin")
&& strcmp (d->d_name, "stdout")
&& strcmp (d->d_name, "stderr"))
@@ -71,9 +74,9 @@
if (__xstat64 (_STAT_VER, buf, &st) == 0
#ifdef _STATBUF_ST_RDEV
- && S_ISCHR (st.st_mode) && st.st_rdev == mydev
+ && S_ISCHR (st.st_mode) && st.st_rdev == mystat->st_rdev
#else
- && d->d_fileno == myino && st.st_dev == mydev
+ && d->d_fileno == mystat->st_ino && st.st_dev == mystat->st_dev
#endif
)
{
@@ -99,7 +102,8 @@
struct stat64 st, st1;
int dostat = 0;
int save = errno;
- int ret;
+ int ret, i;
+ const char *dirs[] = { "/dev/pts/", "/dev/vc/", "/dev/tts/", 0 };
/* Test for the absolute minimal size. This makes life easier inside
the loop. */
@@ -139,50 +143,24 @@
if (__fxstat64 (_STAT_VER, fd, &st) < 0)
return errno;
- /* Prepare the result buffer. */
- memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
- buflen -= sizeof ("/dev/pts/") - 1;
+ ret = ENOTTY;
- if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode))
- {
-#ifdef _STATBUF_ST_RDEV
- ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
- &dostat);
-#else
- ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
- &dostat);
-#endif
- }
- else
+ for (i = 0; ret && dirs[i]; i++)
{
- __set_errno (save);
- ret = ENOENT;
+ if (__xstat64 (_STAT_VER, dirs[i], &st1) == 0 && S_ISDIR (st1.st_mode))
+ ret = getttyname_r (dirs[i], buf, buflen, &st, save, &dostat);
+ else
+ __set_errno (save);
}
+
if (ret && dostat != -1)
- {
- buf[sizeof ("/dev/") - 1] = '\0';
- buflen += sizeof ("pts/") - 1;
-#ifdef _STATBUF_ST_RDEV
- ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
- &dostat);
-#else
- ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
- &dostat);
-#endif
- }
+ ret = getttyname_r ("/dev/", buf, buflen, &st, save, &dostat);
if (ret && dostat != -1)
{
- buf[sizeof ("/dev/") - 1] = '\0';
dostat = 1;
-#ifdef _STATBUF_ST_RDEV
- ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino,
- save, &dostat);
-#else
- ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino,
- save, &dostat);
-#endif
+ ret = getttyname_r ("/dev/", buf, buflen, &st, save, &dostat);
}
return ret;
--- sysdeps/unix/sysv/linux/ttyname.c~ 2001/07/06 04:56:13 1.16
+++ sysdeps/unix/sysv/linux/ttyname.c 2001/10/29 19:25:38
@@ -30,8 +30,8 @@
char *__ttyname;
-static char *getttyname (const char *dev, dev_t mydev,
- ino64_t myino, int save, int *dostat)
+static char *getttyname (const char *dev, struct stat64 *mystat,
+ int save, int *dostat)
internal_function;
@@ -39,7 +39,7 @@
static char *
internal_function
-getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat)
+getttyname (const char *dev, struct stat64 *mystat, int save, int *dostat)
{
static size_t namelen;
struct stat64 st;
@@ -55,7 +55,7 @@
}
while ((d = __readdir64 (dirstream)) != NULL)
- if ((d->d_fileno == myino || *dostat)
+ if ((d->d_fileno == mystat->st_ino || *dostat)
&& strcmp (d->d_name, "stdin")
&& strcmp (d->d_name, "stdout")
&& strcmp (d->d_name, "stderr"))
@@ -78,9 +78,9 @@
memcpy (&getttyname_name[devlen], d->d_name, dlen);
if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
#ifdef _STATBUF_ST_RDEV
- && S_ISCHR (st.st_mode) && st.st_rdev == mydev
+ && S_ISCHR (st.st_mode) && st.st_rdev == mystat->st_rdev
#else
- && d->d_fileno == myino && st.st_dev == mydev
+ && d->d_fileno == mystat->st_ino && st.st_dev == mystat->st_dev
#endif
)
{
@@ -110,9 +110,10 @@
char procname[30];
struct stat64 st, st1;
int dostat = 0;
- char *name;
+ char *name = NULL;
int save = errno;
- int len;
+ int len, i;
+ const char *dirs[] = { "/dev/pts", "/dev/vc", "/dev/tts", 0 };
if (!__isatty (fd))
return NULL;
@@ -146,37 +147,21 @@
if (__fxstat64 (_STAT_VER, fd, &st) < 0)
return NULL;
- if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
+ for (i = 0; !name && dirs[i]; i++)
{
-#ifdef _STATBUF_ST_RDEV
- name = getttyname ("/dev/pts", st.st_rdev, st.st_ino, save, &dostat);
-#else
- name = getttyname ("/dev/pts", st.st_dev, st.st_ino, save, &dostat);
-#endif
+ if (__xstat64 (_STAT_VER, dirs[i], &st1) == 0 && S_ISDIR (st1.st_mode))
+ name = getttyname (dirs[i], &st, save, &dostat);
+ else
+ __set_errno (save);
}
- else
- {
- __set_errno (save);
- name = NULL;
- }
-
+
if (!name && dostat != -1)
- {
-#ifdef _STATBUF_ST_RDEV
- name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
-#else
- name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
-#endif
- }
+ name = getttyname ("/dev", &st, save, &dostat);
if (!name && dostat != -1)
{
dostat = 1;
-#ifdef _STATBUF_ST_RDEV
- name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
-#else
- name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
-#endif
+ name = getttyname ("/dev", &st, save, &dostat);
}
return name;