Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / posix / ttyname_r.c
blobffae375e362e29c600604486382d65a287b20871
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 #ifndef MIN
29 # define MIN(a, b) ((a) < (b) ? (a) : (b))
30 #endif
32 static const char dev[] = "/dev";
34 static int getttyname_r (int fd, char *buf, size_t buflen,
35 dev_t mydev, ino_t myino, int save,
36 int *dostat) __THROW internal_function;
38 static int
39 internal_function
40 getttyname_r (fd, buf, buflen, mydev, myino, save, dostat)
41 int fd;
42 char *buf;
43 size_t buflen;
44 dev_t mydev;
45 ino_t myino;
46 int save;
47 int *dostat;
49 struct stat st;
50 DIR *dirstream;
51 struct dirent *d;
53 dirstream = __opendir (dev);
54 if (dirstream == NULL)
56 *dostat = -1;
57 return errno;
60 while ((d = __readdir (dirstream)) != NULL)
61 if (((ino_t) d->d_fileno == myino || *dostat)
62 && strcmp (d->d_name, "stdin")
63 && strcmp (d->d_name, "stdout")
64 && strcmp (d->d_name, "stderr"))
66 char *cp;
67 size_t needed = _D_EXACT_NAMLEN (d) + 1;
69 if (needed > buflen)
71 *dostat = -1;
72 (void) __closedir (dirstream);
73 __set_errno (ERANGE);
74 return ERANGE;
77 cp = __stpncpy (&buf[sizeof (dev)], d->d_name, needed);
78 cp[0] = '\0';
80 if (stat (buf, &st) == 0
81 #ifdef _STATBUF_ST_RDEV
82 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
83 #else
84 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
85 #endif
88 (void) __closedir (dirstream);
89 __set_errno (save);
90 return 0;
94 (void) __closedir (dirstream);
95 __set_errno (save);
96 /* It is not clear what to return in this case. `isatty' says FD
97 refers to a TTY but no entry in /dev has this inode. */
98 return ENOTTY;
101 /* Store at most BUFLEN character of the pathname of the terminal FD is
102 open on in BUF. Return 0 on success, otherwise an error number. */
104 __ttyname_r (fd, buf, buflen)
105 int fd;
106 char *buf;
107 size_t buflen;
109 struct stat st;
110 int dostat = 0;
111 int save = errno;
112 int ret;
114 /* Test for the absolute minimal size. This makes life easier inside
115 the loop. */
116 if (!buf)
118 __set_errno (EINVAL);
119 return EINVAL;
122 if (buflen < (int) (sizeof (dev) + 1))
124 __set_errno (ERANGE);
125 return ERANGE;
128 if (!__isatty (fd))
130 __set_errno (ENOTTY);
131 return ENOTTY;
134 if (fstat (fd, &st) < 0)
135 return errno;
137 /* Prepare the result buffer. */
138 memcpy (buf, dev, sizeof (dev) - 1);
139 buf[sizeof (dev) - 1] = '/';
140 buflen -= sizeof (dev);
142 #ifdef _STATBUF_ST_RDEV
143 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save,
144 &dostat);
145 #else
146 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save,
147 &dostat);
148 #endif
150 if (ret && dostat != -1)
152 dostat = 1;
153 #ifdef _STATBUF_ST_RDEV
154 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino,
155 save, &dostat);
156 #else
157 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino,
158 save, &dostat);
159 #endif
162 return ret;
165 weak_alias (__ttyname_r, ttyname_r)