getttyent.c 4.7 KB

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