xref: /haiku/src/system/libroot/posix/stdlib/pty.cpp (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <stdlib.h>
7 
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 
12 #include <SupportDefs.h>
13 
14 #include <tty.h>
15 
16 
17 int
18 posix_openpt(int openFlags)
19 {
20 	return open("/dev/ptmx", openFlags);
21 }
22 
23 
24 int
25 grantpt(int masterFD)
26 {
27 	return ioctl(masterFD, B_IOCTL_GRANT_TTY);
28 }
29 
30 
31 int
32 ptsname_r(int masterFD, char* name, size_t namesize)
33 {
34 	int32 index;
35 	if (ioctl(masterFD, B_IOCTL_GET_TTY_INDEX, &index, sizeof(index)) < 0)
36 		return errno;
37 
38 	if (name == NULL)
39 		return EINVAL;
40 
41 	char letter = 'p';
42 	int length = snprintf(name, namesize, "/dev/tt/%c%" B_PRIx32, char(letter + index / 16),
43 		index % 16);
44 	return (length + 1) > (int)namesize ? ERANGE : 0;
45 }
46 
47 
48 char*
49 ptsname(int masterFD)
50 {
51 	static char buffer[32];
52 	errno = ptsname_r(masterFD, buffer, sizeof(buffer));
53 	if (errno != 0 && errno != ERANGE)
54 		return NULL;
55 	return buffer;
56 }
57 
58 
59 int
60 unlockpt(int masterFD)
61 {
62 	// Nothing to do ATM.
63 	return 0;
64 }
65 
66