Commit | Line | Data |
---|---|---|
bb0cfa24 | 1 | /* |
06c5d283 KB |
2 | * Copyright (c) 1989 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
bb0cfa24 DF |
16 | */ |
17 | ||
2ce81398 | 18 | #if defined(LIBC_SCCS) && !defined(lint) |
06c5d283 KB |
19 | static char sccsid[] = "@(#)getttyent.c 5.5 (Berkeley) %G%"; |
20 | #endif /* LIBC_SCCS and not lint */ | |
d011ae29 | 21 | |
06c5d283 | 22 | #include <ttyent.h> |
d011ae29 | 23 | #include <stdio.h> |
06c5d283 | 24 | #include <ctype.h> |
18971fdd | 25 | #include <strings.h> |
d011ae29 | 26 | |
9c787b65 | 27 | static char zapchar; |
06c5d283 | 28 | static FILE *tf; |
d011ae29 | 29 | |
06c5d283 KB |
30 | struct ttyent * |
31 | getttynam(tty) | |
32 | char *tty; | |
d011ae29 | 33 | { |
06c5d283 KB |
34 | register struct ttyent *t; |
35 | ||
36 | setttyent(); | |
37 | while (t = getttyent()) | |
38 | if (!strcmp(tty, t->ty_name)) | |
39 | break; | |
40 | return(t); | |
d011ae29 RC |
41 | } |
42 | ||
06c5d283 KB |
43 | struct ttyent * |
44 | getttyent() | |
d011ae29 | 45 | { |
06c5d283 KB |
46 | static struct ttyent tty; |
47 | register int c; | |
48 | register char *p; | |
49 | #define MAXLINELENGTH 100 | |
50 | static char line[MAXLINELENGTH]; | |
51 | char *skip(), *value(); | |
52 | ||
53 | if (!tf && !setttyent()) | |
54 | return(NULL); | |
55 | do { | |
56 | if (!fgets(line, sizeof(line), tf)) | |
57 | return(NULL); | |
58 | /* skip lines that are too big */ | |
59 | if (!index(line, '\n')) { | |
60 | while ((c = getc(tf)) != '\n' && c != EOF) | |
61 | ; | |
62 | continue; | |
63 | } | |
64 | for (p = line; isspace(*p); ++p) | |
65 | ; | |
66 | } while (!*p || *p == '#'); | |
67 | ||
68 | zapchar = 0; | |
69 | tty.ty_name = p; | |
70 | p = skip(p); | |
71 | if (!*(tty.ty_getty = p)) | |
72 | tty.ty_getty = tty.ty_type = NULL; | |
73 | else { | |
74 | p = skip(p); | |
75 | if (!*(tty.ty_type = p)) | |
76 | tty.ty_type = NULL; | |
77 | else | |
78 | p = skip(p); | |
d011ae29 | 79 | } |
06c5d283 KB |
80 | tty.ty_status = 0; |
81 | tty.ty_window = NULL; | |
82 | ||
83 | #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) | |
84 | #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' | |
85 | for (; *p; p = skip(p)) { | |
86 | if (scmp(_TTYS_OFF)) | |
87 | tty.ty_status &= ~TTY_ON; | |
88 | else if (scmp(_TTYS_ON)) | |
89 | tty.ty_status |= TTY_ON; | |
90 | else if (scmp(_TTYS_SECURE)) | |
91 | tty.ty_status |= TTY_SECURE; | |
92 | else if (vcmp(_TTYS_WINDOW)) | |
93 | tty.ty_window = value(p); | |
94 | else | |
95 | break; | |
96 | } | |
97 | ||
98 | if (zapchar == '#' || *p == '#') | |
99 | while ((c = *++p) == ' ' || c == '\t') | |
100 | ; | |
101 | tty.ty_comment = p; | |
102 | if (*p == 0) | |
103 | tty.ty_comment = 0; | |
104 | if (p = index(p, '\n')) | |
105 | *p = '\0'; | |
106 | return(&tty); | |
d011ae29 RC |
107 | } |
108 | ||
06c5d283 | 109 | #define QUOTED 1 |
18971fdd RC |
110 | |
111 | /* | |
06c5d283 KB |
112 | * Skip over the current field, removing quotes, and return a pointer to |
113 | * the next field. | |
18971fdd | 114 | */ |
d011ae29 RC |
115 | static char * |
116 | skip(p) | |
18971fdd | 117 | register char *p; |
d011ae29 | 118 | { |
06c5d283 KB |
119 | register char *t; |
120 | register int c, q; | |
d011ae29 | 121 | |
06c5d283 | 122 | for (q = 0, t = p; (c = *p) != '\0'; p++) { |
18971fdd RC |
123 | if (c == '"') { |
124 | q ^= QUOTED; /* obscure, but nice */ | |
125 | continue; | |
126 | } | |
9c787b65 MK |
127 | if (q == QUOTED && *p == '\\' && *(p+1) == '"') |
128 | p++; | |
129 | *t++ = *p; | |
18971fdd RC |
130 | if (q == QUOTED) |
131 | continue; | |
d011ae29 | 132 | if (c == '#') { |
9c787b65 MK |
133 | zapchar = c; |
134 | *p = 0; | |
d011ae29 RC |
135 | break; |
136 | } | |
137 | if (c == '\t' || c == ' ' || c == '\n') { | |
9c787b65 MK |
138 | zapchar = c; |
139 | *p++ = 0; | |
d011ae29 RC |
140 | while ((c = *p) == '\t' || c == ' ' || c == '\n') |
141 | p++; | |
142 | break; | |
143 | } | |
d011ae29 | 144 | } |
9c787b65 | 145 | *--t = '\0'; |
06c5d283 | 146 | return(p); |
d011ae29 RC |
147 | } |
148 | ||
18971fdd RC |
149 | static char * |
150 | value(p) | |
151 | register char *p; | |
152 | { | |
06c5d283 | 153 | return((p = index(p, '=')) ? ++p : NULL); |
18971fdd RC |
154 | } |
155 | ||
06c5d283 | 156 | setttyent() |
d011ae29 | 157 | { |
06c5d283 KB |
158 | if (tf) { |
159 | (void)rewind(tf); | |
160 | return(1); | |
161 | } else if (tf = fopen(_PATH_TTYS, "r")) | |
162 | return(1); | |
163 | return(0); | |
164 | } | |
d011ae29 | 165 | |
06c5d283 KB |
166 | endttyent() |
167 | { | |
168 | int rval; | |
169 | ||
170 | if (tf) { | |
171 | rval = !(fclose(tf) == EOF); | |
172 | tf = NULL; | |
173 | return(rval); | |
d011ae29 | 174 | } |
06c5d283 | 175 | return(1); |
d011ae29 | 176 | } |