getttyent.c 4.8 KB

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