From 7fba286fa1203c3d51a24d307e6291e34fe19ad1 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Fri, 14 Aug 2020 19:53:01 -0400 Subject: implement (in a sadly Linux-specific way) --- src/unistd/ttyname.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/unistd/ttyname.c b/src/unistd/ttyname.c index 2d4e3cab..216637c5 100644 --- a/src/unistd/ttyname.c +++ b/src/unistd/ttyname.c @@ -1,11 +1,32 @@ #include "stddef.h" #include "sys/types.h" #include +#include "limits.h" +#include "stdio.h" +#include "_unistd.h" +#include "_syscall.h" + +#ifndef PATH_MAX +#define PATH_MAX _POSIX_PATH_MAX +#endif + +#ifndef readlink +#define readlink(_path, _buf, _bufsiz) __syscall(__syscall_lookup(readlink), _path, _buf, _bufsiz, 0, 0, 0) +#endif char *ttyname(int fildes) { - (void)fildes; - return NULL; + char path[PATH_MAX + 1]; + + if (!isatty(fildes)) { + return NULL; + } + + sprintf(path, "/proc/self/fd/%d", fildes); + if (readlink(path, __unistd.ttyname, sizeof(__unistd.ttyname)) == -1) { + return NULL; + } + return __unistd.ttyname; } /* POSIX(1) -- cgit v1.2.1