xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 28308)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #if defined(LIBC_SCCS) && !defined(lint)
8 static char sccsid[] = "@(#)getttyent.c	5.4 (Berkeley) 05/19/86";
9 #endif LIBC_SCCS and not lint
10 
11 #include <stdio.h>
12 #include <strings.h>
13 #include <ttyent.h>
14 
15 static char TTYFILE[] = "/etc/ttys";
16 static char zapchar;
17 static FILE *tf = NULL;
18 #define LINE 256
19 static char line[LINE];
20 static struct ttyent tty;
21 
22 setttyent()
23 {
24 	if (tf == NULL)
25 		tf = fopen(TTYFILE, "r");
26 	else
27 		rewind(tf);
28 }
29 
30 endttyent()
31 {
32 	if (tf != NULL) {
33 		(void) fclose(tf);
34 		tf = NULL;
35 	}
36 }
37 
38 #define QUOTED	1
39 
40 /*
41  * Skip over the current field, removing quotes,
42  * and return a pointer to the next field.
43  */
44 static char *
45 skip(p)
46 	register char *p;
47 {
48 	register char *t = p;
49 	register int c;
50 	register int q = 0;
51 
52 	for (; (c = *p) != '\0'; p++) {
53 		if (c == '"') {
54 			q ^= QUOTED;	/* obscure, but nice */
55 			continue;
56 		}
57 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
58 			p++;
59 		*t++ = *p;
60 		if (q == QUOTED)
61 			continue;
62 		if (c == '#') {
63 			zapchar = c;
64 			*p = 0;
65 			break;
66 		}
67 		if (c == '\t' || c == ' ' || c == '\n') {
68 			zapchar = c;
69 			*p++ = 0;
70 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
71 				p++;
72 			break;
73 		}
74 	}
75 	*--t = '\0';
76 	return (p);
77 }
78 
79 static char *
80 value(p)
81 	register char *p;
82 {
83 	if ((p = index(p,'=')) == 0)
84 		return(NULL);
85 	p++;			/* get past the = sign */
86 	return(p);
87 }
88 
89 struct ttyent *
90 getttyent()
91 {
92 	register char *p;
93 	register int c;
94 
95 	if (tf == NULL) {
96 		if ((tf = fopen(TTYFILE, "r")) == NULL)
97 			return (NULL);
98 	}
99 	do {
100 		p = fgets(line, LINE, tf);
101 		if (p == NULL)
102 			return (NULL);
103 		while ((c = *p) == '\t' || c == ' ' || c == '\n')
104 			p++;
105 	} while (c == '\0' || c == '#');
106 	zapchar = 0;
107 	tty.ty_name = p;
108 	p = skip(p);
109 	tty.ty_getty = p;
110 	p = skip(p);
111 	tty.ty_type = p;
112 	p = skip(p);
113 	tty.ty_status = 0;
114 	tty.ty_window = NULL;
115 	for (; *p; p = skip(p)) {
116 #define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n')
117 		if (strncmp(p, "on", 2) == 0 && space(2))
118 			tty.ty_status |= TTY_ON;
119 		else if (strncmp(p, "off", 3) == 0 && space(3))
120 			tty.ty_status &= ~TTY_ON;
121 		else if (strncmp(p, "secure", 6) == 0 && space(6))
122 			tty.ty_status |= TTY_SECURE;
123 		else if (strncmp(p, "window=", 7) == 0)
124 			tty.ty_window = value(p);
125 		else
126 			break;
127 	}
128 	if (zapchar == '#' || *p == '#')
129 		while ((c = *++p) == ' ' || c == '\t')
130 			;
131 	tty.ty_comment = p;
132 	if (*p == 0)
133 		tty.ty_comment = 0;
134 	if (p = index(p, '\n'))
135 		*p = '\0';
136 	return(&tty);
137 }
138