Remove stray *_internal aliases (bug 14132).
[glibc.git] / sysdeps / posix / ttyname.c
blob17ed3af86739cda904d8fb05716aefc73e80b53e
1 /* Copyright (C) 1991-2014 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <limits.h>
20 #include <stddef.h>
21 #include <dirent.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdlib.h>
28 char *__ttyname;
30 static char *getttyname (int fd, dev_t mydev, ino_t myino,
31 int save, int *dostat) internal_function;
34 libc_freeres_ptr (static char *getttyname_name);
36 static char *
37 internal_function
38 getttyname (fd, mydev, myino, save, dostat)
39 int fd;
40 dev_t mydev;
41 ino_t myino;
42 int save;
43 int *dostat;
45 static const char dev[] = "/dev";
46 static size_t namelen;
47 struct stat st;
48 DIR *dirstream;
49 struct dirent *d;
51 dirstream = __opendir (dev);
52 if (dirstream == NULL)
54 *dostat = -1;
55 return NULL;
58 while ((d = __readdir (dirstream)) != NULL)
59 if (((ino_t) d->d_fileno == myino || *dostat)
60 && strcmp (d->d_name, "stdin")
61 && strcmp (d->d_name, "stdout")
62 && strcmp (d->d_name, "stderr"))
64 size_t dlen = _D_ALLOC_NAMLEN (d);
65 if (sizeof (dev) + dlen > namelen)
67 free (getttyname_name);
68 namelen = 2 * (sizeof (dev) + dlen); /* Big enough. */
69 getttyname_name = malloc (namelen);
70 if (! getttyname_name)
72 *dostat = -1;
73 /* Perhaps it helps to free the directory stream buffer. */
74 (void) __closedir (dirstream);
75 return NULL;
77 *((char *) __mempcpy (getttyname_name, dev, sizeof (dev) - 1))
78 = '/';
80 (void) __mempcpy (&getttyname_name[sizeof (dev)], d->d_name, dlen);
81 if (stat (getttyname_name, &st) == 0
82 #ifdef _STATBUF_ST_RDEV
83 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
84 #else
85 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
86 #endif
89 (void) __closedir (dirstream);
90 __ttyname = getttyname_name;
91 __set_errno (save);
92 return getttyname_name;
96 (void) __closedir (dirstream);
97 __set_errno (save);
98 return NULL;
101 /* Return the pathname of the terminal FD is open on, or NULL on errors.
102 The returned storage is good only until the next call to this function. */
103 char *
104 ttyname (fd)
105 int fd;
107 struct stat st;
108 int dostat = 0;
109 char *name;
110 int save = errno;
112 if (!__isatty (fd))
113 return NULL;
115 if (fstat (fd, &st) < 0)
116 return NULL;
118 #ifdef _STATBUF_ST_RDEV
119 name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
120 #else
121 name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
122 #endif
124 if (!name && dostat != -1)
126 dostat = 1;
127 #ifdef _STATBUF_ST_RDEV
128 name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
129 #else
130 name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
131 #endif
134 return name;