xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 39176)
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.5 (Berkeley) 09/19/89";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <ttyent.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <strings.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 	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);
79 	}
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);
107 }
108 
109 #define	QUOTED	1
110 
111 /*
112  * Skip over the current field, removing quotes, and return a pointer to
113  * the next field.
114  */
115 static char *
116 skip(p)
117 	register char *p;
118 {
119 	register char *t;
120 	register int c, q;
121 
122 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
123 		if (c == '"') {
124 			q ^= QUOTED;	/* obscure, but nice */
125 			continue;
126 		}
127 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
128 			p++;
129 		*t++ = *p;
130 		if (q == QUOTED)
131 			continue;
132 		if (c == '#') {
133 			zapchar = c;
134 			*p = 0;
135 			break;
136 		}
137 		if (c == '\t' || c == ' ' || c == '\n') {
138 			zapchar = c;
139 			*p++ = 0;
140 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
141 				p++;
142 			break;
143 		}
144 	}
145 	*--t = '\0';
146 	return(p);
147 }
148 
149 static char *
150 value(p)
151 	register char *p;
152 {
153 	return((p = index(p, '=')) ? ++p : NULL);
154 }
155 
156 setttyent()
157 {
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 }
165 
166 endttyent()
167 {
168 	int rval;
169 
170 	if (tf) {
171 		rval = !(fclose(tf) == EOF);
172 		tf = NULL;
173 		return(rval);
174 	}
175 	return(1);
176 }
177