OSDN Git Service

Add in libutil, based on Cory Visi's variant of Michael Shmulevich's libutil
[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 /* Return the result of ptsname_r in the buffer pointed to by PTS,
36    which should be of length BUF_LEN.  If it is too long to fit in
37    this buffer, a sufficiently long buffer is allocated using malloc,
38    and returned in PTS.  0 is returned upon success, -1 otherwise.  */
39 static int
40 pts_name (int fd, char **pts, size_t buf_len)
41 {
42   int rv;
43   char *buf = *pts;
44
45   for (;;)
46     {
47       char *new_buf;
48
49       if (buf_len)
50         {
51           rv = ptsname_r (fd, buf, buf_len);
52
53           if (rv != 0 || memchr (buf, '\0', buf_len))
54             /* We either got an error, or we succeeded and the
55                returned name fit in the buffer.  */
56             break;
57
58           /* Try again with a longer buffer.  */
59           buf_len += buf_len;   /* Double it */
60         }
61       else
62         /* No initial buffer; start out by mallocing one.  */
63         buf_len = 128;          /* First time guess.  */
64
65       if (buf != *pts)
66         /* We've already malloced another buffer at least once.  */
67         new_buf = realloc (buf, buf_len);
68       else
69         new_buf = malloc (buf_len);
70       if (! new_buf)
71         {
72           rv = -1;
73           errno = ENOMEM;
74           break;
75         }
76       buf = new_buf;
77     }
78
79   if (rv == 0)
80     *pts = buf;         /* Return buffer to the user.  */
81   else if (buf != *pts)
82     free (buf);         /* Free what we malloced when returning an error.  */
83
84   return rv;
85 }
86
87 /* Create pseudo tty master slave pair and set terminal attributes
88    according to TERMP and WINP.  Return handles for both ends in
89    AMASTER and ASLAVE, and return the name of the slave end in NAME.  */
90 int
91 openpty (int *amaster, int *aslave, char *name, struct termios *termp,
92          struct winsize *winp)
93 {
94 #ifdef PATH_MAX
95   char _buf[PATH_MAX];
96 #else
97   char _buf[512];
98 #endif
99   char *buf = _buf;
100   int master, slave;
101
102   master = getpt ();
103   if (master == -1)
104     return -1;
105
106   if (grantpt (master))
107     goto fail;
108
109   if (unlockpt (master))
110     goto fail;
111
112   if (pts_name (master, &buf, sizeof (_buf)))
113     goto fail;
114
115   slave = open (buf, O_RDWR | O_NOCTTY);
116   if (slave == -1)
117     {
118       if (buf != _buf)
119         free (buf);
120
121       goto fail;
122     }
123
124   /* XXX Should we ignore errors here?  */
125   if(termp)
126     tcsetattr (slave, TCSAFLUSH, termp);
127   if (winp)
128     ioctl (slave, TIOCSWINSZ, winp);
129
130   *amaster = master;
131   *aslave = slave;
132   if (name != NULL)
133     strcpy (name, buf);
134
135   if (buf != _buf)
136     free (buf);
137   return 0;
138
139  fail:
140   close (master);
141   return -1;
142 }