getttyent.c 5.0 KB

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