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