summaryrefslogtreecommitdiff
path: root/corebinutils/stty/tests/mkpty.c
blob: 258fee18d49af34c2b88fdb9751412367339cffb (plain)
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
#define _XOPEN_SOURCE 700

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(void)
{
	char buffer[256];
	char *path;
	int master;
	ssize_t bytes;

	master = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC);
	if (master < 0) {
		if (errno == ENOENT || errno == ENODEV || errno == ENOSYS ||
		    errno == EPERM || errno == EACCES)
			return 77;
		fprintf(stderr, "mkpty: posix_openpt: %s\n", strerror(errno));
		return 1;
	}
	if (grantpt(master) != 0) {
		fprintf(stderr, "mkpty: grantpt: %s\n", strerror(errno));
		close(master);
		return 1;
	}
	if (unlockpt(master) != 0) {
		fprintf(stderr, "mkpty: unlockpt: %s\n", strerror(errno));
		close(master);
		return 1;
	}
	path = ptsname(master);
	if (path == NULL) {
		fprintf(stderr, "mkpty: ptsname: %s\n", strerror(errno));
		close(master);
		return 1;
	}
	if (printf("%s\n", path) < 0 || fflush(stdout) != 0) {
		fprintf(stderr, "mkpty: failed to write slave path\n");
		close(master);
		return 1;
	}
	while ((bytes = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0)
		;
	if (bytes < 0) {
		fprintf(stderr, "mkpty: read: %s\n", strerror(errno));
		close(master);
		return 1;
	}
	close(master);
	return 0;
}