getttyent.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #define _GNU_SOURCE
  30. #include <features.h>
  31. #include <ttyent.h>
  32. #include <stdio.h>
  33. #include <stdio_ext.h>
  34. #include <ctype.h>
  35. #include <string.h>
  36. static char zapchar;
  37. static FILE *tf;
  38. static struct ttyent tty;
  39. struct ttyent * getttynam(const char *tty)
  40. {
  41. register struct ttyent *t;
  42. setttyent();
  43. while ((t = getttyent()))
  44. if (!strcmp(tty, t->ty_name))
  45. break;
  46. endttyent();
  47. return (t);
  48. }
  49. /* Skip over the current field, removing quotes, and return
  50. * a pointer to the next field.
  51. */
  52. #define QUOTED 1
  53. static char * skip(register char *p)
  54. {
  55. register char *t;
  56. register int c, q;
  57. for (q = 0, t = p; (c = *p) != '\0'; p++) {
  58. if (c == '"') {
  59. q ^= QUOTED; /* obscure, but nice */
  60. continue;
  61. }
  62. if (q == QUOTED && *p == '\\' && *(p+1) == '"')
  63. p++;
  64. *t++ = *p;
  65. if (q == QUOTED)
  66. continue;
  67. if (c == '#') {
  68. zapchar = c;
  69. *p = 0;
  70. break;
  71. }
  72. if (c == '\t' || c == ' ' || c == '\n') {
  73. zapchar = c;
  74. *p++ = 0;
  75. while ((c = *p) == '\t' || c == ' ' || c == '\n')
  76. p++;
  77. break;
  78. }
  79. }
  80. *--t = '\0';
  81. return (p);
  82. }
  83. static char * value(register char *p)
  84. {
  85. return ((p = index(p, '=')) ? ++p : NULL);
  86. }
  87. struct ttyent * getttyent(void)
  88. {
  89. register int c;
  90. register char *p;
  91. static char *line = NULL;
  92. if (!tf && !setttyent())
  93. return (NULL);
  94. if (!line) {
  95. line = malloc(BUFSIZ);
  96. if (!line)
  97. abort();
  98. }
  99. flockfile (tf);
  100. for (;;) {
  101. if (!fgets_unlocked(p = line, sizeof(line), tf)) {
  102. funlockfile (tf);
  103. return (NULL);
  104. }
  105. /* skip lines that are too big */
  106. if (!index(p, '\n')) {
  107. while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
  108. ;
  109. continue;
  110. }
  111. while (isspace(*p))
  112. ++p;
  113. if (*p && *p != '#')
  114. break;
  115. }
  116. zapchar = 0;
  117. tty.ty_name = p;
  118. p = skip(p);
  119. if (!*(tty.ty_getty = p))
  120. tty.ty_getty = tty.ty_type = NULL;
  121. else {
  122. p = skip(p);
  123. if (!*(tty.ty_type = p))
  124. tty.ty_type = NULL;
  125. else
  126. p = skip(p);
  127. }
  128. tty.ty_status = 0;
  129. tty.ty_window = NULL;
  130. #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
  131. #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
  132. for (; *p; p = skip(p)) {
  133. if (scmp(_TTYS_OFF))
  134. tty.ty_status &= ~TTY_ON;
  135. else if (scmp(_TTYS_ON))
  136. tty.ty_status |= TTY_ON;
  137. else if (scmp(_TTYS_SECURE))
  138. tty.ty_status |= TTY_SECURE;
  139. else if (vcmp(_TTYS_WINDOW))
  140. tty.ty_window = value(p);
  141. else
  142. break;
  143. }
  144. /* We can release the lock only here since `zapchar' is global. */
  145. funlockfile(tf);
  146. if (zapchar == '#' || *p == '#')
  147. while ((c = *++p) == ' ' || c == '\t')
  148. ;
  149. tty.ty_comment = p;
  150. if (*p == 0)
  151. tty.ty_comment = 0;
  152. if ((p = index(p, '\n')))
  153. *p = '\0';
  154. return (&tty);
  155. }
  156. int setttyent(void)
  157. {
  158. if (tf) {
  159. rewind(tf);
  160. return (1);
  161. } else if ((tf = fopen(_PATH_TTYS, "r"))) {
  162. /* We do the locking ourselves. */
  163. #ifdef __UCLIBC_HAS_THREADS__
  164. __fsetlocking (tf, FSETLOCKING_BYCALLER);
  165. #endif
  166. return (1);
  167. }
  168. return (0);
  169. }
  170. int endttyent(void)
  171. {
  172. int rval;
  173. if (tf) {
  174. rval = !(fclose(tf) == EOF);
  175. tf = NULL;
  176. return (rval);
  177. }
  178. return (1);
  179. }