-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpty.c
58 lines (48 loc) · 1.06 KB
/
pty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef pty_c
#define pty_c
// musl
//+depends termio open
//+def
int posix_openpt(int flags){
return open("/dev/ptmx", flags);
}
//+depends termio ioctl
//+def
int unlockpt(int fd){
int unlock = 0;
return ioctl(fd, TIOCSPTLCK, &unlock);
}
//+depends termio snprintf itodec ioctl open sprintf
//+def
int ptsname_r(int fd, char *buf, size_t len){
int pty, err;
if (!buf) len = 0;
if ((err = ioctl( fd, TIOCGPTN, &pty))) return -err;
// printf("pts: %d, %d\n",pty,len);
if (sprintf(buf, "/dev/pts/%d", pty) >= len) return ERANGE;
// printf("pts: %s\n",buf);
return 0;
}
//+depends termio ptsname_r snprintf itodec ioctl sprintf
//+def
char *ptsname(int fd){
static char buf[9 + sizeof(int)*3 + 1];
int err = ptsname_r(fd, buf, sizeof buf);
if (err) {
#ifdef mini_errno
errno = err;
#endif
return 0;
}
return buf;
}
//+depends termio fstat ptsname
//+def
int grantpt(int fd){
struct stat st;
if ((fstat(fd, &st))<0) return -1;
if ((chmod((char*)ptsname(fd), st.st_mode | S_IRUSR | S_IWUSR | S_IWGRP))<0)
return -1;
return 0;
}
#endif