]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/posix/ttyname.c
Update.
[thirdparty/glibc.git] / sysdeps / posix / ttyname.c
CommitLineData
af69217f 1/* Copyright (C) 1991, 92, 93, 96, 97, 98 Free Software Foundation, Inc.
47707456 2 This file is part of the GNU C Library.
28f540f4 3
47707456
UD
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.
28f540f4 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
12 Library General Public License for more details.
28f540f4 13
47707456
UD
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. */
28f540f4 18
28f540f4
RM
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>
28
29char *__ttyname = NULL;
30
af69217f
UD
31static char * getttyname __P ((int fd, dev_t mydev, ino_t myino,
32 int save, int *dostat)) internal_function;
33
34static char *
35internal_function
36getttyname (fd, mydev, myino, save, dostat)
c4029823 37 int fd;
af69217f
UD
38 dev_t mydev;
39 ino_t myino;
40 int save;
41 int *dostat;
28f540f4 42{
c4029823 43 static const char dev[] = "/dev";
28f540f4
RM
44 static char *name;
45 static size_t namelen = 0;
46 struct stat st;
28f540f4 47 DIR *dirstream;
10dc2a90 48 struct dirent *d;
28f540f4
RM
49
50 dirstream = opendir (dev);
51 if (dirstream == NULL)
af69217f
UD
52 {
53 *dostat = -1;
54 return NULL;
55 }
28f540f4 56
10dc2a90 57 while ((d = readdir (dirstream)) != NULL)
da2d1bc5
UD
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"))
28f540f4 62 {
92777700
RM
63 size_t dlen = _D_ALLOC_NAMLEN (d);
64 if (sizeof (dev) + dlen > namelen)
28f540f4
RM
65 {
66 free (name);
92777700 67 namelen = 2 * (sizeof (dev) + dlen); /* Big enough. */
28f540f4
RM
68 name = malloc (namelen);
69 if (! name)
10dc2a90 70 {
af69217f 71 *dostat = -1;
10dc2a90
UD
72 /* Perhaps it helps to free the directory stream buffer. */
73 (void) closedir (dirstream);
74 return NULL;
75 }
86187531 76 *((char *) __mempcpy (name, dev, sizeof (dev) - 1)) = '/';
28f540f4 77 }
86187531 78 (void) __mempcpy (&name[sizeof (dev)], d->d_name, dlen);
af69217f
UD
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
85 )
28f540f4
RM
86 {
87 (void) closedir (dirstream);
88 __ttyname = name;
c4029823 89 __set_errno (save);
28f540f4
RM
90 return name;
91 }
92 }
93
94 (void) closedir (dirstream);
c4029823 95 __set_errno (save);
28f540f4
RM
96 return NULL;
97}
af69217f
UD
98
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. */
101char *
102ttyname (fd)
103 int fd;
104{
105 struct stat st;
106 int dostat = 0;
107 char *name;
108 int save = errno;
109
110 if (!__isatty (fd))
111 return NULL;
112
113 if (fstat (fd, &st) < 0)
114 return NULL;
115
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
121
122 if (!name && dostat != -1)
123 {
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
130 }
131
132 return name;
133}