diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-08-14 19:53:01 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-08-14 19:53:01 -0400 |
commit | 7fba286fa1203c3d51a24d307e6291e34fe19ad1 (patch) | |
tree | 536b3552a354a51125745d8710e7418c6dd5baf2 | |
parent | cc9d08dc300408a2d3ad783911d52b257d2425b4 (diff) |
implement (in a sadly Linux-specific way)
-rw-r--r-- | src/unistd/ttyname.c | 25 |
1 files 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 <unistd.h> +#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) |