getttyent.c 5.0 KB

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