f86fbd9c9bccd0dc33c7ab6dacb2dfe17382309c
[musl] / src / unistd / ttyname_r.c
1 #include <unistd.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 int ttyname_r(int fd, char *name, size_t size)
7 {
8         char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2];
9         ssize_t l;
10
11         if (!isatty(fd)) return ENOTTY;
12
13         snprintf(procname, sizeof procname, "/proc/self/fd/%d", fd);
14         l = readlink(procname, name, size);
15
16         if (l < 0) return errno;
17         else if (l == size) return ERANGE;
18         else return 0;
19 }