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