OSDN Git Service

Use internal hidden versions of __login_tty/__openpty
[uclinux-h8/uClibc.git] / libutil / openpty.c
1 /* Copyright (C) 1998, 1999 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 #include <errno.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <pty.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <termios.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29
30 extern int getpt (void);
31 extern int grantpt (int fd);
32 extern int ptsname_r (int fd, char *buf, size_t buflen);
33 extern int unlockpt (int fd);
34
35 /* BCS: the following function is, IMO, overkill */
36 #if 0
37 /* Return the result of ptsname_r in the buffer pointed to by PTS,
38    which should be of length BUF_LEN.  If it is too long to fit in
39    this buffer, a sufficiently long buffer is allocated using malloc,
40    and returned in PTS.  0 is returned upon success, -1 otherwise.  */
41 static int
42 pts_name (int fd, char **pts, size_t buf_len)
43 {
44   int rv;
45   char *buf = *pts;
46
47   for (;;)
48     {
49       char *new_buf;
50
51       if (buf_len)
52         {
53           rv = ptsname_r (fd, buf, buf_len);
54
55           if (rv != 0 || memchr (buf, '\0', buf_len))
56             /* We either got an error, or we succeeded and the
57                returned name fit in the buffer.  */
58             break;
59
60           /* Try again with a longer buffer.  */
61           buf_len += buf_len;   /* Double it */
62         }
63       else
64         /* No initial buffer; start out by mallocing one.  */
65         buf_len = 128;          /* First time guess.  */
66
67       if (buf != *pts)
68         /* We've already malloced another buffer at least once.  */
69         new_buf = realloc (buf, buf_len);
70       else
71         new_buf = malloc (buf_len);
72       if (! new_buf)
73         {
74           rv = -1;
75           errno = ENOMEM;
76           break;
77         }
78       buf = new_buf;
79     }
80
81   if (rv == 0)
82     *pts = buf;         /* Return buffer to the user.  */
83   else if (buf != *pts)
84     free (buf);         /* Free what we malloced when returning an error.  */
85
86   return rv;
87 }
88 #endif
89
90 /* Create pseudo tty master slave pair and set terminal attributes
91    according to TERMP and WINP.  Return handles for both ends in
92    AMASTER and ASLAVE, and return the name of the slave end in NAME.  */
93 int attribute_hidden
94 __openpty (int *amaster, int *aslave, char *name, struct termios *termp,
95          struct winsize *winp)
96 {
97 #if 0
98 #ifdef PATH_MAX
99   char _buf[PATH_MAX];
100 #else
101   char _buf[512];
102 #endif
103   char *buf = _buf;
104 #else
105 #ifdef PATH_MAX
106   char buf[PATH_MAX];
107 #else
108   char buf[512];
109 #endif
110 #endif
111   int master, slave;
112
113   master = getpt ();
114   if (master == -1)
115     return -1;
116
117   if (grantpt (master))
118     goto fail;
119
120   if (unlockpt (master))
121     goto fail;
122
123 #if 0
124   if (pts_name (master, &buf, sizeof (_buf)))
125 #else
126   if (ptsname_r (master, buf, sizeof buf))
127 #endif
128     goto fail;
129
130   slave = open (buf, O_RDWR | O_NOCTTY);
131   if (slave == -1)
132     {
133 #if 0
134       if (buf != _buf)
135         free (buf);
136 #endif
137       goto fail;
138     }
139
140   /* XXX Should we ignore errors here?  */
141   if(termp)
142     tcsetattr (slave, TCSAFLUSH, termp);
143   if (winp)
144     ioctl (slave, TIOCSWINSZ, winp);
145
146   *amaster = master;
147   *aslave = slave;
148   if (name != NULL)
149     strcpy (name, buf);
150
151 #if 0
152   if (buf != _buf)
153     free (buf);
154 #endif
155   return 0;
156
157  fail:
158   close (master);
159   return -1;
160 }
161
162 strong_alias(__openpty,openpty)