diff options
| author | Matthias-Christian Ott <ott@enolink.de> | 2008-06-10 17:30:15 +0200 |
|---|---|---|
| committer | Matthias-Christian Ott <ott@enolink.de> | 2008-06-10 17:30:15 +0200 |
| commit | 47d8633f157156ef4701ce71ed7eff766206c107 (patch) | |
| tree | 78190354f0b6784063f5434821b40bbe0ebd8d1e /pty.c | |
| parent | update TODO (diff) | |
| download | st-47d8633f157156ef4701ce71ed7eff766206c107.tar.gz st-47d8633f157156ef4701ce71ed7eff766206c107.zip | |
source getpty() out to pty.c
Diffstat (limited to 'pty.c')
| -rw-r--r-- | pty.c | 41 |
1 files changed, 41 insertions, 0 deletions
| @@ -0,0 +1,41 @@ | |||
| 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 | } | ||
