support translation for getopt error messages
[musl] / src / unistd / ttyname_r.c
1 #include <unistd.h>
2 #include <errno.h>
3
4 void __procfdname(char *, unsigned);
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         __procfdname(procname, fd);
14         l = readlink(procname, name, size);
15
16         if (l < 0) return errno;
17         else if (l == size) return ERANGE;
18         else {
19                 name[l] = 0;
20                 return 0;
21         }
22 }