libroot: add ptsname_r for POSIX.1/2024

Change-Id: I9996ff998b2b52a65a101cf7075a3ffb37220962
Reviewed-on: https://review.haiku-os.org/c/haiku/+/8519
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
Jérôme Duval 2024-11-04 18:37:41 +01:00 committed by waddlesplash
parent 8b5cc9cf52
commit 7a4e12b7b8
2 changed files with 22 additions and 8 deletions

View File

@ -183,6 +183,7 @@ extern int getsubopt(char **optionp, char * const *keylistp,
extern int posix_openpt(int openFlags);
extern int grantpt(int masterFD);
extern char* ptsname(int masterFD);
extern int ptsname_r(int masterFD, char *path, size_t maxSize);
extern int unlockpt(int masterFD);
/* internal accessor to value for MB_CUR_MAX */

View File

@ -5,6 +5,7 @@
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@ -27,19 +28,30 @@ grantpt(int masterFD)
}
char*
ptsname(int masterFD)
int
ptsname_r(int masterFD, char* name, size_t namesize)
{
int32 index;
if (ioctl(masterFD, B_IOCTL_GET_TTY_INDEX, &index, sizeof(index)) < 0)
return NULL;
return errno;
if (name == NULL)
return EINVAL;
static char buffer[32];
char letter = 'p';
snprintf(buffer, sizeof(buffer), "/dev/tt/%c%" B_PRIx32,
char(letter + index / 16), index % 16);
int length = snprintf(name, namesize, "/dev/tt/%c%" B_PRIx32, char(letter + index / 16),
index % 16);
return (length + 1) > (int)namesize ? ERANGE : 0;
}
char*
ptsname(int masterFD)
{
static char buffer[32];
errno = ptsname_r(masterFD, buffer, sizeof(buffer));
if (errno != 0 && errno != ERANGE)
return NULL;
return buffer;
}
@ -47,6 +59,7 @@ ptsname(int masterFD)
int
unlockpt(int masterFD)
{
// Noting to do ATM.
// Nothing to do ATM.
return 0;
}