xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 28281)
1 /*
2  * Copyright (c) 1983 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.3 (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 EMPTY[] = "";
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 and
42  * return a pointer to the next field.
43  */
44 static char *
45 skip(p)
46 	register char *p;
47 {
48 	register int c;
49 	register int q = 0;
50 
51 	for (; (c = *p) != '\0'; p++) {
52 		if (c == '"') {
53 			q ^= QUOTED;	/* obscure, but nice */
54 			continue;
55 		}
56 		if (q == QUOTED)
57 			continue;
58 		if (c == '#') {
59 			*p = '\0';
60 			break;
61 		}
62 		if (c == '\t' || c == ' ' || c == '\n') {
63 			*p++ = '\0';
64 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
65 				p++;
66 			break;
67 		}
68 	}
69 	return (p);
70 }
71 
72 static char *
73 value(p)
74 	register char *p;
75 {
76 	if ((p = index(p,'=')) == 0)
77 		return(NULL);
78 	p++;			/* get past the = sign */
79 	return(p);
80 }
81 
82 /* get rid of quotes. */
83 
84 static
85 qremove(p)
86 	register char *p;
87 {
88 	register char *t;
89 
90 	for (t = p; *p; p++)
91 		if (*p != '"')
92 			*t++ = *p;
93 	*t = '\0';
94 }
95 
96 struct ttyent *
97 getttyent()
98 {
99 	register char *p;
100 	register int c;
101 
102 	if (tf == NULL) {
103 		if ((tf = fopen(TTYFILE, "r")) == NULL)
104 			return (NULL);
105 	}
106 	do {
107 		p = fgets(line, LINE, tf);
108 		if (p == NULL)
109 			return (NULL);
110 		while ((c = *p) == '\t' || c == ' ' || c == '\n')
111 			p++;
112 	} while (c == '\0' || c == '#');
113 	tty.ty_name = p;
114 	p = skip(p);
115 	tty.ty_getty = p;
116 	p = skip(p);
117 	tty.ty_type = p;
118 	p = skip(p);
119 	tty.ty_status = 0;
120 	tty.ty_window = EMPTY;
121 	for (; *p; p = skip(p)) {
122 #define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n')
123 		if (strncmp(p, "on", 2) == 0 && space(2))
124 			tty.ty_status |= TTY_ON;
125 		else if (strncmp(p, "off", 3) == 0 && space(3))
126 			tty.ty_status &= ~TTY_ON;
127 		else if (strncmp(p, "secure", 6) == 0 && space(6))
128 			tty.ty_status |= TTY_SECURE;
129 		else if (strncmp(p, "window", 6) == 0) {
130 			if ((tty.ty_window = value(p)) == NULL)
131 				tty.ty_window = EMPTY;
132 		} else
133 			break;
134 	}
135 	tty.ty_comment = p;
136 	if (p = index(p, '\n'))
137 		*p = '\0';
138 	qremove(tty.ty_getty);
139 	qremove(tty.ty_window);
140 	qremove(tty.ty_comment);
141 	return(&tty);
142 }
143