-
Notifications
You must be signed in to change notification settings - Fork 0
/
spemu.c
88 lines (75 loc) · 1.58 KB
/
spemu.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include <unistd.h>
const char * const RED = "\x1b[38;5;1m";
const char * const GREEN = "\x1b[38;5;1m";
const char * const YELLOW = "\x1b[38;5;3m";
const char * const BLUE = "\x1b[38;5;4m";
const char * const END = "\x1b[0m";
extern int errno;
int fd = -1;
char *buf = NULL;
void sigint_handler(int _)
{
printf("\rGracefully quitting...\n");
free(buf);
if (fd > 0)
{
close(fd);
}
exit(0);
}
int main(void)
{
signal(SIGINT, sigint_handler);
pid_t pid = getpid();
printf("%sPID%s: %s%d%s\n", BLUE, END, YELLOW, pid, END);
fd = posix_openpt(O_RDWR | O_NOCTTY);
if (fd < 0)
{
fprintf(
stderr, "%sCouldn't open PTY%s: %s (%d)\n",
RED, END, strerror(errno), errno
);
exit(1);
}
printf(
"Opened PTY with %sfd%s=%s%d%s\n",
BLUE, END, YELLOW, fd, END
);
int err = grantpt(fd);
if (err)
{
fprintf(
stderr, "%sFailed to grant access to the PTY device%s: %s (%d)\n",
RED, END, strerror(errno), errno
);
exit(1);
}
printf("Granted access to the PTY device.\n");
err = unlockpt(fd);
if (err)
{
fprintf(
stderr, "%sFailed to unlock the PTY device%s: %s (%d)\n",
RED, END, strerror(errno), errno
);
exit(1);
}
printf(
"Unlocked the PTY %sdevice%s: %s%s%s\n",
BLUE, END, YELLOW, ptsname(fd), END
);
for (;;)
{
char *buf = NULL;
size_t bufsz = 0;
size_t charcnt = getline(&buf, &bufsz, stdin);
write(fd, buf, charcnt);
}
return 0;
}