ttyslot.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* $Id$ */
  2. #ifdef __USG
  3. /* system V, so no /etc/ttys file. In this case, scan the
  4. /etc/utmp file
  5. */
  6. struct utmp {
  7. char ut_name[8];
  8. char ut_id[4];
  9. char ut_line[12];
  10. short ut_pid;
  11. short ut_type;
  12. struct exit_status {
  13. short e_termination;
  14. short e_exit;
  15. } ut_exit;
  16. long ut_time;
  17. };
  18. #define FILENAME "/etc/utmp"
  19. #else
  20. #define FILENAME "/etc/ttys"
  21. #endif
  22. char *ttyname();
  23. char *rindex();
  24. ttyslot()
  25. {
  26. register char *tp, *p;
  27. int fd;
  28. int retval = 1;
  29. #ifdef __USG
  30. struct utmp buf;
  31. #else
  32. char buf[32];
  33. #endif
  34. if (! (tp=ttyname(0)) && ! (tp=ttyname(1)) && !(tp=ttyname(2)))
  35. return 0;
  36. if (! (p = rindex(tp, '/')))
  37. p = tp;
  38. else
  39. p++;
  40. if ((fd = open(FILENAME, 0)) < 0) return 0;
  41. #ifdef __USG
  42. while (read(fd, (char *) &buf, sizeof(buf)) == sizeof(buf)) {
  43. /* processes associated with a terminal ...
  44. unfortunately we cannot use the include file because
  45. some systems have a different one ...
  46. INIT_PROCESS, DEAD_PROCESS, USER_PROCESS, LOGIN_PROCESS
  47. */
  48. if ((buf.ut_type >= 5 && buf.ut_type <= 8) &&
  49. ! strncmp(buf.ut_line, p, sizeof(buf.ut_line))) {
  50. close(fd);
  51. return retval;
  52. }
  53. retval++;
  54. }
  55. close(fd);
  56. return 0;
  57. #else
  58. for (;;) {
  59. tp = buf;
  60. for (;;tp++) {
  61. if (read(fd, tp, 1) != 1) {
  62. close(fd);
  63. return 0;
  64. }
  65. if (*tp == '\n' || tp >= &buf[31]) {
  66. *tp = 0;
  67. if (tp < buf+2) buf[2] = '\0';
  68. tp = buf+2;
  69. break;
  70. }
  71. }
  72. if (! strcmp(p, tp)) {
  73. close(fd);
  74. return retval;
  75. }
  76. retval++;
  77. }
  78. /*NOTREACHED*/
  79. #endif
  80. }