make linking of thread-start with explicit scheduling conditional
[musl] / src / unistd / ttyname_r.c
1 #include <unistd.h>
2 #include <errno.h>
3 #include <sys/stat.h>
4
5 void __procfdname(char *, unsigned);
6
7 int ttyname_r(int fd, char *name, size_t size)
8 {
9         struct stat st1, st2;
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
21         name[l] = 0;
22
23         if (stat(name, &st1) || fstat(fd, &st2))
24                 return errno;
25         if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
26                 return ENODEV;
27
28         return 0;
29 }