debloat code that depends on /proc/self/fd/%d with shared function
[musl] / src / unistd / ttyname_r.c
1 #include <unistd.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 void __procfdname(char *, unsigned);
7
8 int ttyname_r(int fd, char *name, size_t size)
9 {
10         char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2];
11         ssize_t l;
12
13         if (!isatty(fd)) return ENOTTY;
14
15         __procfdname(procname, fd);
16         l = readlink(procname, name, size);
17
18         if (l < 0) return errno;
19         else if (l == size) return ERANGE;
20         else {
21                 name[l] = 0;
22                 return 0;
23         }
24 }