JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
remove emallocz()
[st.git] / pty.c
1 /* See LICENSE file for copyright and license details. */
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #if !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
7 #include <pty.h>
8 #endif
9
10 extern int ptm, pts;
11
12 void
13 getpty(void) {
14         char *ptsdev;
15
16 #if defined(_GNU_SOURCE)
17         ptm = getpt();
18 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
19         ptm = posix_openpt(O_RDWR);
20 #else
21         ptm = open("/dev/ptmx", O_RDWR);
22         if(ptm == -1)
23                 if(openpty(&ptm, &pts, NULL, NULL, NULL) == -1)
24                         err(EXIT_FAILURE, "cannot open pty");
25 #endif
26 #if defined(_XOPEN_SOURCE)
27         if(ptm != -1) {
28                 if(grantpt(ptm) == -1)
29                         err(EXIT_FAILURE, "cannot grant access to pty");
30                 if(unlockpt(ptm) == -1)
31                         err(EXIT_FAILURE, "cannot unlock pty");
32                 ptsdev = ptsname(ptm);
33                 if(!ptsdev)
34                         err(EXIT_FAILURE, "slave pty name undefined");
35                 pts = open(ptsdev, O_RDWR);
36                 if(pts == -1)
37                         err(EXIT_FAILURE, "cannot open slave pty");
38         }
39         else
40                 err(EXIT_FAILURE, "cannot open pty");
41 #endif
42 }