ttyname_r.c 416 B

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