ttyname.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #define opendir __opendir
  2. #define closedir __closedir
  3. #define readdir __readdir
  4. #define isatty __isatty
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <assert.h>
  8. #include <unistd.h>
  9. #include <dirent.h>
  10. #include <sys/stat.h>
  11. /* Jan 1, 2004 Manuel Novoa III
  12. *
  13. * Kept the same approach, but rewrote the code for the most part.
  14. * Fixed some minor issues plus (as I recall) one SUSv3 errno case.
  15. */
  16. /* This is a fairly slow approach. We do a linear search through some
  17. * directories looking for a match. Yes this is lame. But it should
  18. * work, should be small, and will return names that match what is on
  19. * disk. Another approach we could use would be to use the info in
  20. * /proc/self/fd, but that is even more lame since it requires /proc */
  21. /* SUSv3 mandates TTY_NAME_MAX as 9. This is obviously insufficient.
  22. * However, there is no need to waste space and support non-standard
  23. * tty names either. So we compromise and use the following buffer
  24. * length. (Erik and Manuel agreed that 32 was more than reasonable.)
  25. *
  26. * If you change this, also change _SC_TTY_NAME_MAX in libc/unistd/sysconf.c
  27. */
  28. #define TTYNAME_BUFLEN 32
  29. static const char dirlist[] =
  30. /* 12345670123 */
  31. "\010/dev/vc/\0" /* Try /dev/vc first (be devfs compatible) */
  32. "\011/dev/tts/\0" /* and /dev/tts next (be devfs compatible) */
  33. "\011/dev/pty/\0" /* and /dev/pty next (be devfs compatible) */
  34. "\011/dev/pts/\0" /* and try /dev/pts next */
  35. "\005/dev/\0"; /* and try walking through /dev last */
  36. int attribute_hidden __ttyname_r(int fd, char *ubuf, size_t ubuflen)
  37. {
  38. struct dirent *d;
  39. struct stat st;
  40. struct stat dst;
  41. const char *p;
  42. char *s;
  43. DIR *fp;
  44. int rv;
  45. size_t len;
  46. char buf[TTYNAME_BUFLEN];
  47. if (__fstat(fd, &st) < 0) {
  48. return errno;
  49. }
  50. rv = ENOTTY; /* Set up the default return value. */
  51. if (!isatty(fd)) {
  52. goto DONE;
  53. }
  54. for (p = dirlist ; *p ; p += 1 + p[-1]) {
  55. len = *p++;
  56. assert(len + 2 <= TTYNAME_BUFLEN); /* dirname + 1 char + nul */
  57. __strcpy(buf, p);
  58. s = buf + len;
  59. len = (TTYNAME_BUFLEN-2) - len; /* Available non-nul space. */
  60. if (!(fp = opendir(p))) {
  61. continue;
  62. }
  63. while ((d = readdir(fp)) != NULL) {
  64. /* This should never trigger for standard names, but we
  65. * check it to be safe. */
  66. if (__strlen(d->d_name) > len) { /* Too big? */
  67. continue;
  68. }
  69. __strcpy(s, d->d_name);
  70. if ((__lstat(buf, &dst) == 0)
  71. #if 0
  72. /* Stupid filesystems like cramfs fail to guarantee that
  73. * st_ino and st_dev uniquely identify a file, contrary to
  74. * SuSv3, so we cannot be quite so precise as to require an
  75. * exact match. Settle for something less... Grumble... */
  76. && (st.st_dev == dst.st_dev) && (st.st_ino == dst.st_ino)
  77. #else
  78. && S_ISCHR(dst.st_mode) && (st.st_rdev == dst.st_rdev)
  79. #endif
  80. ) { /* Found it! */
  81. closedir(fp);
  82. /* We treat NULL buf as ERANGE rather than EINVAL. */
  83. rv = ERANGE;
  84. if (ubuf && (__strlen(buf) <= ubuflen)) {
  85. __strcpy(ubuf, buf);
  86. rv = 0;
  87. }
  88. goto DONE;
  89. }
  90. }
  91. closedir(fp);
  92. }
  93. DONE:
  94. __set_errno(rv);
  95. return rv;
  96. }
  97. strong_alias(__ttyname_r,ttyname_r)
  98. char *ttyname(int fd)
  99. {
  100. static char name[TTYNAME_BUFLEN];
  101. return __ttyname_r(fd, name, TTYNAME_BUFLEN) ? NULL : name;
  102. }