getttyent.c 5.3 KB

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