OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdlib/] [ptsname.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1998 Free Software Foundation, Inc.
2
   This file is part of the GNU C Library.
3
   Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
4
 
5
   The GNU C Library is free software; you can redistribute it and/or
6
   modify it under the terms of the GNU Library General Public License as
7
   published by the Free Software Foundation; either version 2 of the
8
   License, or (at your option) any later version.
9
 
10
   The GNU C Library is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
   Library General Public License for more details.
14
 
15
   You should have received a copy of the GNU Library General Public
16
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
17
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
   Boston, MA 02111-1307, USA.  */
19
 
20
#define _STDIO_UTILITY                  /* For _int10tostr. */
21
#include <stdio.h>
22
#include <errno.h>
23
#include <paths.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <sys/ioctl.h>
27
#include <sys/stat.h>
28
#include <sys/sysmacros.h>
29
#include <termios.h>
30
#include <unistd.h>
31
 
32
 
33
#if !defined __UNIX98PTY_ONLY__
34
 
35
/* Check if DEV corresponds to a master pseudo terminal device.  */
36
#define MASTER_P(Dev)                                                         \
37
  (major ((Dev)) == 2                                                         \
38
   || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192)     \
39
   || (major ((Dev)) >= 128 && major ((Dev)) < 136))
40
 
41
/* Check if DEV corresponds to a master pseudo terminal device.  */
42
#define SLAVE_P(Dev)                                                          \
43
  (major ((Dev)) == 3                                                         \
44
   || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256)     \
45
   || (major ((Dev)) >= 136 && major ((Dev)) < 144))
46
 
47
/* Note that major number 4 corresponds to the old BSD style pseudo
48
   terminal devices.  As of Linux 2.1.115 these are no longer
49
   supported.  They have been replaced by major numbers 2 (masters)
50
   and 3 (slaves).  */
51
 
52
/* The are declared in getpt.c.  */
53
extern const char _ptyname1[];
54
extern const char _ptyname2[];
55
 
56
#endif
57
 
58
/* Directory where we can find the slave pty nodes.  */
59
#define _PATH_DEVPTS "/dev/pts/"
60
 
61
/* Store at most BUFLEN characters of the pathname of the slave pseudo
62
   terminal associated with the master FD is open on in BUF.
63
   Return 0 on success, otherwise an error number.  */
64
int ptsname_r (int fd, char *buf, size_t buflen)
65
{
66
  int save_errno = errno;
67
#if !defined __UNIX98PTY_ONLY__
68
  struct stat st;
69
#endif
70
  int ptyno;
71
 
72
  if (buf == NULL)
73
    {
74
      errno = EINVAL;
75
      return EINVAL;
76
    }
77
 
78
#if !defined __UNIX98PTY_ONLY__
79
  if (!isatty (fd))
80
    {
81
      errno = ENOTTY;
82
      return ENOTTY;
83
    }
84
#elif !defined TIOCGPTN
85
# error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
86
#endif
87
#ifdef TIOCGPTN
88
  if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
89
    {
90
      /* Buffer we use to print the number in. */
91
      char numbuf[__BUFLEN_INT10TOSTR];
92
      static const char devpts[] = _PATH_DEVPTS;
93
      char *p;
94
 
95
      p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
96
 
97
      if (buflen < sizeof devpts + &numbuf[sizeof numbuf - 1] - p)
98
        {
99
          errno = ERANGE;
100
          return ERANGE;
101
        }
102
 
103
      strcpy (buf, devpts);
104
      strcat (buf, p);
105
      /* Note: Don't bother with stat on the slave name and checking the
106
         driver's major device number - the ioctl above succeeded so
107
         we know the fd was a Unix'98 master and the /dev/pts/ prefix
108
         is set by definition.  If the name isn't really a slave PTY,
109
         the system is misconfigured anyway - something else will fail
110
         later.
111
         */
112
      errno = save_errno;
113
      return 0;
114
    }
115
#endif
116
#if defined __UNIX98PTY_ONLY__
117
  else
118
    {
119
      /* If the ioctl fails it wasn't a Unix 98 master PTY */
120
      errno = ENOTTY;
121
      return ENOTTY;
122
    }
123
#else
124
# if !defined TIOCGPTN
125
  else if (errno == EINVAL)
126
# endif
127
    {
128
      char *p;
129
 
130
      if (buflen < strlen (_PATH_TTY) + 3)
131
        {
132
          errno = ERANGE;
133
          return ERANGE;
134
        }
135
 
136
      if (fstat (fd, &st) < 0)
137
        return errno;
138
 
139
      /* Check if FD really is a master pseudo terminal.  */
140
      if (! MASTER_P (st.st_rdev))
141
        {
142
          errno = ENOTTY;
143
          return ENOTTY;
144
        }
145
 
146
      ptyno = minor (st.st_rdev);
147
      /* This is for the old BSD pseudo terminals.  As of Linux
148
         2.1.115 these are no longer supported.  */
149
      if (major (st.st_rdev) == 4)
150
        ptyno -= 128;
151
 
152
      if (ptyno / 16 >= strlen (_ptyname1))
153
        {
154
          errno = ENOTTY;
155
          return ENOTTY;
156
        }
157
 
158
      strcpy (buf, _PATH_TTY);
159
      p = buf + strlen (buf);
160
      p[0] = _ptyname1[ptyno / 16];
161
      p[1] = _ptyname2[ptyno % 16];
162
      p[2] = '\0';
163
    }
164
 
165
  if (stat(buf, &st) < 0)
166
    return errno;
167
 
168
  /* Check if the name we're about to return really corresponds to a
169
     slave pseudo terminal.  */
170
  if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
171
    {
172
      /* This really is a configuration problem.  */
173
      errno = ENOTTY;
174
      return ENOTTY;
175
    }
176
#endif
177
 
178
  errno = save_errno;
179
  return 0;
180
}
181
 
182
/* Return the pathname of the pseudo terminal slave assoicated with
183
   the master FD is open on, or NULL on errors.
184
   The returned storage is good until the next call to this function.  */
185
char *
186
ptsname (int fd)
187
{
188
  static char buffer[sizeof (_PATH_DEVPTS) + 20];
189
 
190
  return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
191
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.