getttyent.c 5.1 KB

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