Commit | Line | Data |
---|---|---|
bb0cfa24 | 1 | /* |
74155b62 KB |
2 | * Copyright (c) 1989, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
06c5d283 | 4 | * |
269a7923 | 5 | * %sccs.include.redist.c% |
bb0cfa24 DF |
6 | */ |
7 | ||
2ce81398 | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
74155b62 | 9 | static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) %G%"; |
06c5d283 | 10 | #endif /* LIBC_SCCS and not lint */ |
d011ae29 | 11 | |
06c5d283 | 12 | #include <ttyent.h> |
d011ae29 | 13 | #include <stdio.h> |
06c5d283 | 14 | #include <ctype.h> |
6ebcb998 | 15 | #include <string.h> |
d011ae29 | 16 | |
9c787b65 | 17 | static char zapchar; |
06c5d283 | 18 | static FILE *tf; |
d011ae29 | 19 | |
06c5d283 KB |
20 | struct ttyent * |
21 | getttynam(tty) | |
c5980113 | 22 | const char *tty; |
d011ae29 | 23 | { |
06c5d283 KB |
24 | register struct ttyent *t; |
25 | ||
26 | setttyent(); | |
27 | while (t = getttyent()) | |
28 | if (!strcmp(tty, t->ty_name)) | |
29 | break; | |
0d344169 MK |
30 | endttyent(); |
31 | return (t); | |
d011ae29 RC |
32 | } |
33 | ||
06c5d283 KB |
34 | struct ttyent * |
35 | getttyent() | |
d011ae29 | 36 | { |
06c5d283 KB |
37 | static struct ttyent tty; |
38 | register int c; | |
39 | register char *p; | |
40 | #define MAXLINELENGTH 100 | |
41 | static char line[MAXLINELENGTH]; | |
c5980113 | 42 | static char *skip(), *value(); |
06c5d283 KB |
43 | |
44 | if (!tf && !setttyent()) | |
0d344169 | 45 | return (NULL); |
7baa3ecd KB |
46 | for (;;) { |
47 | if (!fgets(p = line, sizeof(line), tf)) | |
0d344169 | 48 | return (NULL); |
06c5d283 | 49 | /* skip lines that are too big */ |
7baa3ecd | 50 | if (!index(p, '\n')) { |
06c5d283 KB |
51 | while ((c = getc(tf)) != '\n' && c != EOF) |
52 | ; | |
53 | continue; | |
54 | } | |
7baa3ecd KB |
55 | while (isspace(*p)) |
56 | ++p; | |
57 | if (*p && *p != '#') | |
58 | break; | |
59 | } | |
06c5d283 KB |
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); | |
d011ae29 | 72 | } |
06c5d283 KB |
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'; | |
0d344169 | 99 | return (&tty); |
d011ae29 RC |
100 | } |
101 | ||
06c5d283 | 102 | #define QUOTED 1 |
18971fdd RC |
103 | |
104 | /* | |
06c5d283 KB |
105 | * Skip over the current field, removing quotes, and return a pointer to |
106 | * the next field. | |
18971fdd | 107 | */ |
d011ae29 RC |
108 | static char * |
109 | skip(p) | |
18971fdd | 110 | register char *p; |
d011ae29 | 111 | { |
06c5d283 KB |
112 | register char *t; |
113 | register int c, q; | |
d011ae29 | 114 | |
06c5d283 | 115 | for (q = 0, t = p; (c = *p) != '\0'; p++) { |
18971fdd RC |
116 | if (c == '"') { |
117 | q ^= QUOTED; /* obscure, but nice */ | |
118 | continue; | |
119 | } | |
9c787b65 MK |
120 | if (q == QUOTED && *p == '\\' && *(p+1) == '"') |
121 | p++; | |
122 | *t++ = *p; | |
18971fdd RC |
123 | if (q == QUOTED) |
124 | continue; | |
d011ae29 | 125 | if (c == '#') { |
9c787b65 MK |
126 | zapchar = c; |
127 | *p = 0; | |
d011ae29 RC |
128 | break; |
129 | } | |
130 | if (c == '\t' || c == ' ' || c == '\n') { | |
9c787b65 MK |
131 | zapchar = c; |
132 | *p++ = 0; | |
d011ae29 RC |
133 | while ((c = *p) == '\t' || c == ' ' || c == '\n') |
134 | p++; | |
135 | break; | |
136 | } | |
d011ae29 | 137 | } |
9c787b65 | 138 | *--t = '\0'; |
0d344169 | 139 | return (p); |
d011ae29 RC |
140 | } |
141 | ||
18971fdd RC |
142 | static char * |
143 | value(p) | |
144 | register char *p; | |
145 | { | |
0d344169 MK |
146 | |
147 | return ((p = index(p, '=')) ? ++p : NULL); | |
18971fdd RC |
148 | } |
149 | ||
c5980113 | 150 | int |
06c5d283 | 151 | setttyent() |
d011ae29 | 152 | { |
0d344169 | 153 | |
06c5d283 KB |
154 | if (tf) { |
155 | (void)rewind(tf); | |
0d344169 | 156 | return (1); |
06c5d283 | 157 | } else if (tf = fopen(_PATH_TTYS, "r")) |
0d344169 MK |
158 | return (1); |
159 | return (0); | |
06c5d283 | 160 | } |
d011ae29 | 161 | |
c5980113 | 162 | int |
06c5d283 KB |
163 | endttyent() |
164 | { | |
165 | int rval; | |
166 | ||
167 | if (tf) { | |
168 | rval = !(fclose(tf) == EOF); | |
169 | tf = NULL; | |
0d344169 | 170 | return (rval); |
d011ae29 | 171 | } |
0d344169 | 172 | return (1); |
d011ae29 | 173 | } |