Update.
[platform/upstream/glibc.git] / login / openpty.c
1 /* 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 #include <fcntl.h>
21 #include <pty.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <termios.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27
28 #include "pty-internal.h"
29
30 int
31 openpty (pmast, pslave, pname, tio, wins)
32      int *pmast;
33      int *pslave;
34      char *pname;
35      struct termios *tio;
36      struct winsize *wins;
37 {
38   int pfd, tfd;
39   char name[PTYNAMELEN];
40
41   pfd = getpt ();
42   if (pfd == -1)
43     return -1;
44
45   if (grantpt (pfd))
46     goto bail;
47
48   if (unlockpt (pfd))
49     goto bail;
50
51   if (ptsname_r (pfd, name, PTYNAMELEN) != 0)
52     goto bail;
53
54   tfd = open (name, O_RDWR);
55   if (tfd == -1)
56     goto bail;
57
58   /* XXX Should we ignore errors here?  */
59   if(tio)
60     tcsetattr (tfd, TCSAFLUSH, tio);
61   if (wins)
62     ioctl (tfd, TIOCSWINSZ, wins);
63
64   *pmast = pfd;
65   *pslave = tfd;
66   if (pname != NULL)
67     strcpy (pname, name);
68   return 0;
69
70 bail:
71   close (pfd);
72   return -1;
73 }