diff options
Diffstat (limited to 'src/unistd/ttyname.c')
| -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) |
