getttyent.c 4.8 KB

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