* sysdeps/generic/sysconf.c: Include <grp.h> and <pwd.h>.
[glibc.git] / sysdeps / posix / ttyname.c
blobb7a5d85661df353f4c63801697c8906d2423c56e
1 /* Copyright (C) 1991,92,93,96,97,98,2000,2002 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 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;
31 static char *getttyname (int fd, dev_t mydev, ino_t myino,
32 int save, int *dostat) internal_function;
35 libc_freeres_ptr (static char *getttyname_name);
37 static char *
38 internal_function
39 getttyname (fd, mydev, myino, save, dostat)
40 int fd;
41 dev_t mydev;
42 ino_t myino;
43 int save;
44 int *dostat;
46 static const char dev[] = "/dev";
47 static size_t namelen;
48 struct stat st;
49 DIR *dirstream;
50 struct dirent *d;
52 dirstream = __opendir (dev);
53 if (dirstream == NULL)
55 *dostat = -1;
56 return NULL;
59 while ((d = __readdir (dirstream)) != NULL)
60 if (((ino_t) d->d_fileno == myino || *dostat)
61 && strcmp (d->d_name, "stdin")
62 && strcmp (d->d_name, "stdout")
63 && strcmp (d->d_name, "stderr"))
65 size_t dlen = _D_ALLOC_NAMLEN (d);
66 if (sizeof (dev) + dlen > namelen)
68 free (getttyname_name);
69 namelen = 2 * (sizeof (dev) + dlen); /* Big enough. */
70 getttyname_name = malloc (namelen);
71 if (! getttyname_name)
73 *dostat = -1;
74 /* Perhaps it helps to free the directory stream buffer. */
75 (void) __closedir (dirstream);
76 return NULL;
78 *((char *) __mempcpy (getttyname_name, dev, sizeof (dev) - 1))
79 = '/';
81 (void) __mempcpy (&getttyname_name[sizeof (dev)], d->d_name, dlen);
82 if (stat (getttyname_name, &st) == 0
83 #ifdef _STATBUF_ST_RDEV
84 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
85 #else
86 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
87 #endif
90 (void) __closedir (dirstream);
91 __ttyname = getttyname_name;
92 __set_errno (save);
93 return getttyname_name;
97 (void) __closedir (dirstream);
98 __set_errno (save);
99 return NULL;
102 /* Return the pathname of the terminal FD is open on, or NULL on errors.
103 The returned storage is good only until the next call to this function. */
104 char *
105 ttyname (fd)
106 int fd;
108 struct stat st;
109 int dostat = 0;
110 char *name;
111 int save = errno;
113 if (!__isatty (fd))
114 return NULL;
116 if (fstat (fd, &st) < 0)
117 return NULL;
119 #ifdef _STATBUF_ST_RDEV
120 name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
121 #else
122 name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
123 #endif
125 if (!name && dostat != -1)
127 dostat = 1;
128 #ifdef _STATBUF_ST_RDEV
129 name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
130 #else
131 name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
132 #endif
135 return name;