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