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