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