getttyent.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include <stdlib.h>
  37. #ifdef __UCLIBC_HAS_THREADS__
  38. #include <pthread.h>
  39. #endif
  40. static char zapchar;
  41. static FILE *tf;
  42. static struct ttyent tty;
  43. struct ttyent * getttynam(const char *tty)
  44. {
  45. register struct ttyent *t;
  46. setttyent();
  47. while ((t = getttyent()))
  48. if (!strcmp(tty, t->ty_name))
  49. break;
  50. endttyent();
  51. return (t);
  52. }
  53. /* Skip over the current field, removing quotes, and return
  54. * a pointer to the next field.
  55. */
  56. #define QUOTED 1
  57. static char * skip(register char *p)
  58. {
  59. register char *t;
  60. register int c, q;
  61. for (q = 0, t = p; (c = *p) != '\0'; p++) {
  62. if (c == '"') {
  63. q ^= QUOTED; /* obscure, but nice */
  64. continue;
  65. }
  66. if (q == QUOTED && *p == '\\' && *(p+1) == '"')
  67. p++;
  68. *t++ = *p;
  69. if (q == QUOTED)
  70. continue;
  71. if (c == '#') {
  72. zapchar = c;
  73. *p = 0;
  74. break;
  75. }
  76. if (c == '\t' || c == ' ' || c == '\n') {
  77. zapchar = c;
  78. *p++ = 0;
  79. while ((c = *p) == '\t' || c == ' ' || c == '\n')
  80. p++;
  81. break;
  82. }
  83. }
  84. *--t = '\0';
  85. return (p);
  86. }
  87. static char * value(register char *p)
  88. {
  89. return ((p = strchr(p, '=')) ? ++p : NULL);
  90. }
  91. struct ttyent * getttyent(void)
  92. {
  93. register int c;
  94. register char *p;
  95. static char *line = NULL;
  96. if (!tf && !setttyent())
  97. return (NULL);
  98. if (!line) {
  99. line = malloc(BUFSIZ);
  100. if (!line)
  101. abort();
  102. }
  103. __STDIO_ALWAYS_THREADLOCK(tf);
  104. for (;;) {
  105. if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
  106. __STDIO_ALWAYS_THREADUNLOCK(tf);
  107. return (NULL);
  108. }
  109. /* skip lines that are too big */
  110. if (!strchr(p, '\n')) {
  111. while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
  112. ;
  113. continue;
  114. }
  115. while (isspace(*p))
  116. ++p;
  117. if (*p && *p != '#')
  118. break;
  119. }
  120. zapchar = 0;
  121. tty.ty_name = p;
  122. p = skip(p);
  123. if (!*(tty.ty_getty = p))
  124. tty.ty_getty = tty.ty_type = NULL;
  125. else {
  126. p = skip(p);
  127. if (!*(tty.ty_type = p))
  128. tty.ty_type = NULL;
  129. else
  130. p = skip(p);
  131. }
  132. tty.ty_status = 0;
  133. tty.ty_window = NULL;
  134. #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
  135. #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
  136. for (; *p; p = skip(p)) {
  137. if (scmp(_TTYS_OFF))
  138. tty.ty_status &= ~TTY_ON;
  139. else if (scmp(_TTYS_ON))
  140. tty.ty_status |= TTY_ON;
  141. else if (scmp(_TTYS_SECURE))
  142. tty.ty_status |= TTY_SECURE;
  143. else if (vcmp(_TTYS_WINDOW))
  144. tty.ty_window = value(p);
  145. else
  146. break;
  147. }
  148. /* We can release the lock only here since `zapchar' is global. */
  149. __STDIO_ALWAYS_THREADUNLOCK(tf);
  150. if (zapchar == '#' || *p == '#')
  151. while ((c = *++p) == ' ' || c == '\t')
  152. ;
  153. tty.ty_comment = p;
  154. if (*p == 0)
  155. tty.ty_comment = 0;
  156. if ((p = strchr(p, '\n')))
  157. *p = '\0';
  158. return (&tty);
  159. }
  160. int setttyent(void)
  161. {
  162. if (tf) {
  163. rewind(tf);
  164. return (1);
  165. } else if ((tf = fopen(_PATH_TTYS, "r"))) {
  166. /* We do the locking ourselves. */
  167. #ifdef __UCLIBC_HAS_THREADS__
  168. __fsetlocking (tf, FSETLOCKING_BYCALLER);
  169. #endif
  170. return (1);
  171. }
  172. return (0);
  173. }
  174. int endttyent(void)
  175. {
  176. int rval;
  177. if (tf) {
  178. rval = !(fclose(tf) == EOF);
  179. tf = NULL;
  180. return (rval);
  181. }
  182. return (1);
  183. }