cuserid: support invocation with a null pointer argument
[musl.git] / src / unistd / ttyname_r.c
blob82acb75e19d60a6a075e7398dc23d9b4f8729e5d
1 #include <unistd.h>
2 #include <errno.h>
3 #include <sys/stat.h>
4 #include "syscall.h"
6 int ttyname_r(int fd, char *name, size_t size)
8 struct stat st1, st2;
9 char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2];
10 ssize_t l;
12 if (!isatty(fd)) return errno;
14 __procfdname(procname, fd);
15 l = readlink(procname, name, size);
17 if (l < 0) return errno;
18 else if (l == size) return ERANGE;
20 name[l] = 0;
22 if (stat(name, &st1) || fstat(fd, &st2))
23 return errno;
24 if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
25 return ENODEV;
27 return 0;