Add ttyname_r(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-04 22:25:42 +01:00
parent a0a8ed61d8
commit f34279cc60
4 changed files with 59 additions and 5 deletions

View File

@ -532,6 +532,7 @@ unistd/tfork.o \
unistd/truncateat.o \
unistd/truncate.o \
unistd/ttyname.o \
unistd/ttyname_r.o \
unistd/unlinkat.o \
unistd/unlink.o \
unistd/usleep.o \

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
This file is part of the Sortix C Library.
@ -320,7 +320,6 @@ int setreuid(uid_t, uid_t);
pid_t setsid(void);
void swab(const void* __restrict, void* __restrict, ssize_t);
void sync(void);
int ttyname_r(int, char*, size_t);
#endif
int access(const char*, int);
@ -390,6 +389,7 @@ int tcsetpgrp(int, pid_t);
int truncate(const char*, off_t);
int truncateat(int dirfd, const char*, off_t);
char* ttyname(int);
int ttyname_r(int, char*, size_t);
int usleep(useconds_t useconds);
int unlinkat(int, const char*, int);
int unlink(const char*);

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
This file is part of the Sortix C Library.
@ -18,13 +18,28 @@
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
unistd/ttyname.cpp
Returns the name of a terminal.
Returns the pathname of a terminal.
*******************************************************************************/
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
extern "C" char* ttyname(int fd)
{
return isatty(fd) ? (char*) "/dev/tty" : NULL;
static char* result = NULL;
static size_t result_size = 0;
while ( ttyname_r(fd, result, result_size) < 0 )
{
if ( errno != ERANGE )
return NULL;
size_t new_result_size = result_size ? 2 * result_size : 16;
char* new_result = (char*) realloc(result, new_result_size);
if ( !new_result )
return NULL;
result = new_result;
result_size = new_result_size;
}
return result;
}

38
libc/unistd/ttyname_r.cpp Normal file
View File

@ -0,0 +1,38 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
unistd/ttyname_r.cpp
Returns the pathname of a terminal.
*******************************************************************************/
#include <errno.h>
#include <string.h>
#include <unistd.h>
extern "C" int ttyname_r(int fd, char* path, size_t path_size)
{
if ( isatty(fd) < 1 )
return -1;
const char* result = "/dev/tty";
size_t result_length = strlen(result);
if ( result_length + 1 < path_size )
return errno = ERANGE, -1;
return strcpy(path, result), 0;
}