getttyent.c 5.4 KB

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