xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 16420)
1 /* @(#)getttyent.c	4.1 (Berkeley) 04/27/84 */
2 
3 #include <stdio.h>
4 #include <ttyent.h>
5 
6 static char TTYFILE[] = "/etc/ttys";
7 static char EMPTY[] = "";
8 static FILE *tf = NULL;
9 static char line[BUFSIZ+1];
10 static struct ttyent tty;
11 
12 setttyent()
13 {
14 	if (tf == NULL)
15 		tf = fopen(TTYFILE, "r");
16 	else
17 		rewind(tf);
18 }
19 
20 endttyent()
21 {
22 	if (tf != NULL) {
23 		(void) fclose(tf);
24 		tf = NULL;
25 	}
26 }
27 
28 static char *
29 skip(p)
30 register char *p;
31 {
32 	register int c;
33 
34 	while ((c = *p) != '\0') {
35 		if (c == '#') {
36 			*p = '\0';
37 			break;
38 		}
39 		if (c == '\t' || c == ' ' || c == '\n') {
40 			*p++ = '\0';
41 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
42 				p++;
43 			break;
44 		}
45 		p++;
46 	}
47 	return (p);
48 }
49 
50 struct ttyent *
51 getttyent()
52 {
53 	register char *p, *cp;
54 	register int c;
55 
56 	if (tf == NULL) {
57 		if ((tf = fopen(TTYFILE, "r")) == NULL)
58 			return (NULL);
59 	}
60 	do {
61 		p = fgets(line, BUFSIZ, tf);
62 		if (p == NULL)
63 			return (NULL);
64 		while ((c = *p) == '\t' || c == ' ' || c == '\n')
65 			p++;
66 	} while (c == '\0' || c == '#');
67 	tty.ty_name = p;
68 	p = skip(p);
69 	tty.ty_getty = p;
70 	p = skip(p);
71 	tty.ty_type = p;
72 	tty.ty_status = 0;
73 	for (p = skip(p); *p; p = cp) {
74 		cp = skip(p);
75 		if (strcmp(p, "on") == 0)
76 			tty.ty_status |= TTY_ON;
77 		else if (strcmp(p, "off") == 0)
78 			tty.ty_status &= ~TTY_ON;
79 		else if (strcmp(p, "secure") == 0)
80 			tty.ty_status |= TTY_SECURE;
81 	}
82 	tty.ty_comment = EMPTY;
83 	return (&tty);
84 }
85