ttyname.c 2.9 KB

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