1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11
12 #include <ttyent.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16
17 static char zapchar;
18 static FILE *tf;
19
20 struct ttyent *
getttynam(tty)21 getttynam(tty)
22 const char *tty;
23 {
24 register struct ttyent *t;
25
26 setttyent();
27 while (t = getttyent())
28 if (!strcmp(tty, t->ty_name))
29 break;
30 endttyent();
31 return (t);
32 }
33
34 struct ttyent *
getttyent()35 getttyent()
36 {
37 static struct ttyent tty;
38 register int c;
39 register char *p;
40 #define MAXLINELENGTH 100
41 static char line[MAXLINELENGTH];
42 static char *skip(), *value();
43
44 if (!tf && !setttyent())
45 return (NULL);
46 for (;;) {
47 if (!fgets(p = line, sizeof(line), tf))
48 return (NULL);
49 /* skip lines that are too big */
50 if (!index(p, '\n')) {
51 while ((c = getc(tf)) != '\n' && c != EOF)
52 ;
53 continue;
54 }
55 while (isspace(*p))
56 ++p;
57 if (*p && *p != '#')
58 break;
59 }
60
61 zapchar = 0;
62 tty.ty_name = p;
63 p = skip(p);
64 if (!*(tty.ty_getty = p))
65 tty.ty_getty = tty.ty_type = NULL;
66 else {
67 p = skip(p);
68 if (!*(tty.ty_type = p))
69 tty.ty_type = NULL;
70 else
71 p = skip(p);
72 }
73 tty.ty_status = 0;
74 tty.ty_window = NULL;
75
76 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
77 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
78 for (; *p; p = skip(p)) {
79 if (scmp(_TTYS_OFF))
80 tty.ty_status &= ~TTY_ON;
81 else if (scmp(_TTYS_ON))
82 tty.ty_status |= TTY_ON;
83 else if (scmp(_TTYS_SECURE))
84 tty.ty_status |= TTY_SECURE;
85 else if (vcmp(_TTYS_WINDOW))
86 tty.ty_window = value(p);
87 else
88 break;
89 }
90
91 if (zapchar == '#' || *p == '#')
92 while ((c = *++p) == ' ' || c == '\t')
93 ;
94 tty.ty_comment = p;
95 if (*p == 0)
96 tty.ty_comment = 0;
97 if (p = index(p, '\n'))
98 *p = '\0';
99 return (&tty);
100 }
101
102 #define QUOTED 1
103
104 /*
105 * Skip over the current field, removing quotes, and return a pointer to
106 * the next field.
107 */
108 static char *
skip(p)109 skip(p)
110 register char *p;
111 {
112 register char *t;
113 register int c, q;
114
115 for (q = 0, t = p; (c = *p) != '\0'; p++) {
116 if (c == '"') {
117 q ^= QUOTED; /* obscure, but nice */
118 continue;
119 }
120 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
121 p++;
122 *t++ = *p;
123 if (q == QUOTED)
124 continue;
125 if (c == '#') {
126 zapchar = c;
127 *p = 0;
128 break;
129 }
130 if (c == '\t' || c == ' ' || c == '\n') {
131 zapchar = c;
132 *p++ = 0;
133 while ((c = *p) == '\t' || c == ' ' || c == '\n')
134 p++;
135 break;
136 }
137 }
138 *--t = '\0';
139 return (p);
140 }
141
142 static char *
value(p)143 value(p)
144 register char *p;
145 {
146
147 return ((p = index(p, '=')) ? ++p : NULL);
148 }
149
150 int
setttyent()151 setttyent()
152 {
153
154 if (tf) {
155 (void)rewind(tf);
156 return (1);
157 } else if (tf = fopen(_PATH_TTYS, "r"))
158 return (1);
159 return (0);
160 }
161
162 int
endttyent()163 endttyent()
164 {
165 int rval;
166
167 if (tf) {
168 rval = !(fclose(tf) == EOF);
169 tf = NULL;
170 return (rval);
171 }
172 return (1);
173 }
174