Update.
[glibc.git] / sysdeps / posix / ttyname.c
blob5ad45ae2a6877f40f42a5b423b6bb398e4cc4a46
1 /* Copyright (C) 1991, 92, 93, 96, 97, 98 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <dirent.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <stdlib.h>
29 char *__ttyname = NULL;
31 static char * getttyname __P ((int fd, dev_t mydev, ino_t myino,
32 int save, int *dostat)) internal_function;
34 static char *
35 internal_function
36 getttyname (fd, mydev, myino, save, dostat)
37 int fd;
38 dev_t mydev;
39 ino_t myino;
40 int save;
41 int *dostat;
43 static const char dev[] = "/dev";
44 static char *name;
45 static size_t namelen = 0;
46 struct stat st;
47 DIR *dirstream;
48 struct dirent *d;
50 dirstream = opendir (dev);
51 if (dirstream == NULL)
53 *dostat = -1;
54 return NULL;
57 while ((d = readdir (dirstream)) != NULL)
58 if (((ino_t) d->d_fileno == myino || *dostat)
59 && strcmp (d->d_name, "stdin")
60 && strcmp (d->d_name, "stdout")
61 && strcmp (d->d_name, "stderr"))
63 size_t dlen = _D_ALLOC_NAMLEN (d);
64 if (sizeof (dev) + dlen > namelen)
66 free (name);
67 namelen = 2 * (sizeof (dev) + dlen); /* Big enough. */
68 name = malloc (namelen);
69 if (! name)
71 *dostat = -1;
72 /* Perhaps it helps to free the directory stream buffer. */
73 (void) closedir (dirstream);
74 return NULL;
76 *((char *) __mempcpy (name, dev, sizeof (dev) - 1)) = '/';
78 (void) __mempcpy (&name[sizeof (dev)], d->d_name, dlen);
79 if (stat (name, &st) == 0
80 #ifdef _STATBUF_ST_RDEV
81 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
82 #else
83 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
84 #endif
87 (void) closedir (dirstream);
88 __ttyname = name;
89 __set_errno (save);
90 return name;
94 (void) closedir (dirstream);
95 __set_errno (save);
96 return NULL;
99 /* Return the pathname of the terminal FD is open on, or NULL on errors.
100 The returned storage is good only until the next call to this function. */
101 char *
102 ttyname (fd)
103 int fd;
105 struct stat st;
106 int dostat = 0;
107 char *name;
108 int save = errno;
110 if (!__isatty (fd))
111 return NULL;
113 if (fstat (fd, &st) < 0)
114 return NULL;
116 #ifdef _STATBUF_ST_RDEV
117 name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
118 #else
119 name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
120 #endif
122 if (!name && dostat != -1)
124 dostat = 1;
125 #ifdef _STATBUF_ST_RDEV
126 name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
127 #else
128 name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
129 #endif
132 return name;