diff --git a/libc/Makefile b/libc/Makefile
index 95ee10d7..45fcfc88 100644
--- a/libc/Makefile
+++ b/libc/Makefile
@@ -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 \
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 14257fc6..63221b63 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -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*);
diff --git a/libc/unistd/ttyname.cpp b/libc/unistd/ttyname.cpp
index 13f4c86a..1bb908a9 100644
--- a/libc/unistd/ttyname.cpp
+++ b/libc/unistd/ttyname.cpp
@@ -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 .
unistd/ttyname.cpp
- Returns the name of a terminal.
+ Returns the pathname of a terminal.
*******************************************************************************/
+#include
+#include
#include
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;
}
diff --git a/libc/unistd/ttyname_r.cpp b/libc/unistd/ttyname_r.cpp
new file mode 100644
index 00000000..b604914a
--- /dev/null
+++ b/libc/unistd/ttyname_r.cpp
@@ -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 .
+
+ unistd/ttyname_r.cpp
+ Returns the pathname of a terminal.
+
+*******************************************************************************/
+
+#include
+#include
+#include
+
+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;
+}