]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/posix/ttyname_r.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / posix / ttyname_r.c
CommitLineData
04277e02 1/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
47707456 2 This file is part of the GNU C Library.
60478656 3
47707456 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
60478656 8
47707456
UD
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
41bdb6e2 12 Lesser General Public License for more details.
60478656 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
60478656
RM
17
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>
27
28#ifndef MIN
29# define MIN(a, b) ((a) < (b) ? (a) : (b))
30#endif
31
af69217f
UD
32static const char dev[] = "/dev";
33
79937577
UD
34static int getttyname_r (int fd, char *buf, size_t buflen,
35 dev_t mydev, ino_t myino, int save,
b41bd5bc 36 int *dostat) __THROW;
af69217f
UD
37
38static int
f63f2bfd
JM
39getttyname_r (int fd, char *buf, size_t buflen, dev_t mydev, ino_t myino,
40 int save, int *dostat)
60478656 41{
60478656 42 struct stat st;
60478656 43 DIR *dirstream;
10dc2a90 44 struct dirent *d;
60478656 45
50304ef0 46 dirstream = __opendir (dev);
60478656 47 if (dirstream == NULL)
af69217f
UD
48 {
49 *dostat = -1;
50 return errno;
51 }
60478656 52
50304ef0 53 while ((d = __readdir (dirstream)) != NULL)
da2d1bc5
UD
54 if (((ino_t) d->d_fileno == myino || *dostat)
55 && strcmp (d->d_name, "stdin")
56 && strcmp (d->d_name, "stdout")
57 && strcmp (d->d_name, "stderr"))
60478656
RM
58 {
59 char *cp;
ba1ffaa1
UD
60 size_t needed = _D_EXACT_NAMLEN (d) + 1;
61
62 if (needed > buflen)
63 {
af69217f 64 *dostat = -1;
50304ef0 65 (void) __closedir (dirstream);
ba1ffaa1
UD
66 __set_errno (ERANGE);
67 return ERANGE;
68 }
60478656 69
ba1ffaa1 70 cp = __stpncpy (&buf[sizeof (dev)], d->d_name, needed);
60478656
RM
71 cp[0] = '\0';
72
af69217f
UD
73 if (stat (buf, &st) == 0
74#ifdef _STATBUF_ST_RDEV
75 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
76#else
77 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
78#endif
79 )
60478656 80 {
50304ef0 81 (void) __closedir (dirstream);
c4029823 82 __set_errno (save);
60478656
RM
83 return 0;
84 }
85 }
86
50304ef0 87 (void) __closedir (dirstream);
c4029823 88 __set_errno (save);
ba1ffaa1
UD
89 /* It is not clear what to return in this case. `isatty' says FD
90 refers to a TTY but no entry in /dev has this inode. */
91 return ENOTTY;
60478656 92}
af69217f
UD
93
94/* Store at most BUFLEN character of the pathname of the terminal FD is
95 open on in BUF. Return 0 on success, otherwise an error number. */
96int
bd2260a2 97__ttyname_r (int fd, char *buf, size_t buflen)
af69217f
UD
98{
99 struct stat st;
100 int dostat = 0;
101 int save = errno;
102 int ret;
103
104 /* Test for the absolute minimal size. This makes life easier inside
105 the loop. */
106 if (!buf)
107 {
108 __set_errno (EINVAL);
109 return EINVAL;
110 }
111
112 if (buflen < (int) (sizeof (dev) + 1))
113 {
114 __set_errno (ERANGE);
115 return ERANGE;
116 }
117
118 if (!__isatty (fd))
119 {
120 __set_errno (ENOTTY);
121 return ENOTTY;
122 }
123
124 if (fstat (fd, &st) < 0)
125 return errno;
126
127 /* Prepare the result buffer. */
128 memcpy (buf, dev, sizeof (dev) - 1);
129 buf[sizeof (dev) - 1] = '/';
130 buflen -= sizeof (dev);
131
132#ifdef _STATBUF_ST_RDEV
133 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save,
134 &dostat);
135#else
136 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save,
137 &dostat);
138#endif
139
140 if (ret && dostat != -1)
141 {
142 dostat = 1;
143#ifdef _STATBUF_ST_RDEV
144 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino,
145 save, &dostat);
146#else
147 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino,
148 save, &dostat);
149#endif
150 }
151
152 return ret;
153}
154
23396375 155weak_alias (__ttyname_r, ttyname_r)