]> sourceware.org Git - glibc.git/blame - sysdeps/posix/ttyname.c
Convert 231 sysdeps function definitions to prototype style.
[glibc.git] / sysdeps / posix / ttyname.c
CommitLineData
b168057a 1/* Copyright (C) 1991-2015 Free Software Foundation, Inc.
47707456 2 This file is part of the GNU C Library.
28f540f4 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.
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
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 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/>. */
28f540f4 17
28f540f4
RM
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
b8fd5502 28char *__ttyname;
28f540f4 29
b8fd5502
UD
30static char *getttyname (int fd, dev_t mydev, ino_t myino,
31 int save, int *dostat) internal_function;
32
33
c877418f 34libc_freeres_ptr (static char *getttyname_name);
af69217f
UD
35
36static char *
37internal_function
bd2260a2 38getttyname (int fd, dev_t mydev, ino_t myino, int save, int *dostat)
28f540f4 39{
c4029823 40 static const char dev[] = "/dev";
b8fd5502 41 static size_t namelen;
28f540f4 42 struct stat st;
28f540f4 43 DIR *dirstream;
10dc2a90 44 struct dirent *d;
28f540f4 45
50304ef0 46 dirstream = __opendir (dev);
28f540f4 47 if (dirstream == NULL)
af69217f
UD
48 {
49 *dostat = -1;
50 return NULL;
51 }
28f540f4 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"))
28f540f4 58 {
92777700
RM
59 size_t dlen = _D_ALLOC_NAMLEN (d);
60 if (sizeof (dev) + dlen > namelen)
28f540f4 61 {
b8fd5502 62 free (getttyname_name);
92777700 63 namelen = 2 * (sizeof (dev) + dlen); /* Big enough. */
b8fd5502
UD
64 getttyname_name = malloc (namelen);
65 if (! getttyname_name)
10dc2a90 66 {
af69217f 67 *dostat = -1;
10dc2a90 68 /* Perhaps it helps to free the directory stream buffer. */
50304ef0 69 (void) __closedir (dirstream);
10dc2a90
UD
70 return NULL;
71 }
b8fd5502
UD
72 *((char *) __mempcpy (getttyname_name, dev, sizeof (dev) - 1))
73 = '/';
28f540f4 74 }
b8fd5502
UD
75 (void) __mempcpy (&getttyname_name[sizeof (dev)], d->d_name, dlen);
76 if (stat (getttyname_name, &st) == 0
af69217f
UD
77#ifdef _STATBUF_ST_RDEV
78 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
79#else
80 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
81#endif
82 )
28f540f4 83 {
50304ef0 84 (void) __closedir (dirstream);
b8fd5502 85 __ttyname = getttyname_name;
c4029823 86 __set_errno (save);
b8fd5502 87 return getttyname_name;
28f540f4
RM
88 }
89 }
90
50304ef0 91 (void) __closedir (dirstream);
c4029823 92 __set_errno (save);
28f540f4
RM
93 return NULL;
94}
af69217f
UD
95
96/* Return the pathname of the terminal FD is open on, or NULL on errors.
97 The returned storage is good only until the next call to this function. */
98char *
bd2260a2 99ttyname (int fd)
af69217f
UD
100{
101 struct stat st;
102 int dostat = 0;
103 char *name;
104 int save = errno;
105
106 if (!__isatty (fd))
107 return NULL;
108
109 if (fstat (fd, &st) < 0)
110 return NULL;
111
112#ifdef _STATBUF_ST_RDEV
113 name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
114#else
115 name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
116#endif
117
118 if (!name && dostat != -1)
119 {
120 dostat = 1;
121#ifdef _STATBUF_ST_RDEV
122 name = getttyname (fd, st.st_rdev, st.st_ino, save, &dostat);
123#else
124 name = getttyname (fd, st.st_dev, st.st_ino, save, &dostat);
125#endif
126 }
127
128 return name;
129}
This page took 0.603379 seconds and 5 git commands to generate.