xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 28308)
121343Sdist /*
2*28308Skarels  * Copyright (c) 1985 Regents of the University of California.
321343Sdist  * All rights reserved.  The Berkeley software License Agreement
421343Sdist  * specifies the terms and conditions for redistribution.
521343Sdist  */
616420Sralph 
726562Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*28308Skarels static char sccsid[] = "@(#)getttyent.c	5.4 (Berkeley) 05/19/86";
926562Sdonn #endif LIBC_SCCS and not lint
1021343Sdist 
1116420Sralph #include <stdio.h>
1217882Sralph #include <strings.h>
1316420Sralph #include <ttyent.h>
1416420Sralph 
1516420Sralph static char TTYFILE[] = "/etc/ttys";
16*28308Skarels static char zapchar;
1716420Sralph static FILE *tf = NULL;
1817882Sralph #define LINE 256
1917882Sralph static char line[LINE];
2016420Sralph static struct ttyent tty;
2116420Sralph 
2216420Sralph setttyent()
2316420Sralph {
2416420Sralph 	if (tf == NULL)
2516420Sralph 		tf = fopen(TTYFILE, "r");
2616420Sralph 	else
2716420Sralph 		rewind(tf);
2816420Sralph }
2916420Sralph 
3016420Sralph endttyent()
3116420Sralph {
3216420Sralph 	if (tf != NULL) {
3316420Sralph 		(void) fclose(tf);
3416420Sralph 		tf = NULL;
3516420Sralph 	}
3616420Sralph }
3716420Sralph 
38*28308Skarels #define QUOTED	1
3917882Sralph 
4017882Sralph /*
41*28308Skarels  * Skip over the current field, removing quotes,
42*28308Skarels  * and return a pointer to the next field.
4317882Sralph  */
4416420Sralph static char *
4516420Sralph skip(p)
4617882Sralph 	register char *p;
4716420Sralph {
48*28308Skarels 	register char *t = p;
4916420Sralph 	register int c;
5017882Sralph 	register int q = 0;
5116420Sralph 
5217882Sralph 	for (; (c = *p) != '\0'; p++) {
5317882Sralph 		if (c == '"') {
5417882Sralph 			q ^= QUOTED;	/* obscure, but nice */
5517882Sralph 			continue;
5617882Sralph 		}
57*28308Skarels 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
58*28308Skarels 			p++;
59*28308Skarels 		*t++ = *p;
6017882Sralph 		if (q == QUOTED)
6117882Sralph 			continue;
6216420Sralph 		if (c == '#') {
63*28308Skarels 			zapchar = c;
64*28308Skarels 			*p = 0;
6516420Sralph 			break;
6616420Sralph 		}
6716420Sralph 		if (c == '\t' || c == ' ' || c == '\n') {
68*28308Skarels 			zapchar = c;
69*28308Skarels 			*p++ = 0;
7016420Sralph 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
7116420Sralph 				p++;
7216420Sralph 			break;
7316420Sralph 		}
7416420Sralph 	}
75*28308Skarels 	*--t = '\0';
7616420Sralph 	return (p);
7716420Sralph }
7816420Sralph 
7917882Sralph static char *
8017882Sralph value(p)
8117882Sralph 	register char *p;
8217882Sralph {
8317882Sralph 	if ((p = index(p,'=')) == 0)
8417882Sralph 		return(NULL);
8517882Sralph 	p++;			/* get past the = sign */
8617882Sralph 	return(p);
8717882Sralph }
8817882Sralph 
8916420Sralph struct ttyent *
9016420Sralph getttyent()
9116420Sralph {
9217882Sralph 	register char *p;
9316420Sralph 	register int c;
9416420Sralph 
9516420Sralph 	if (tf == NULL) {
9616420Sralph 		if ((tf = fopen(TTYFILE, "r")) == NULL)
9716420Sralph 			return (NULL);
9816420Sralph 	}
9916420Sralph 	do {
10017882Sralph 		p = fgets(line, LINE, tf);
10116420Sralph 		if (p == NULL)
10216420Sralph 			return (NULL);
10316420Sralph 		while ((c = *p) == '\t' || c == ' ' || c == '\n')
10416420Sralph 			p++;
10516420Sralph 	} while (c == '\0' || c == '#');
106*28308Skarels 	zapchar = 0;
10716420Sralph 	tty.ty_name = p;
10816420Sralph 	p = skip(p);
10916420Sralph 	tty.ty_getty = p;
11016420Sralph 	p = skip(p);
11116420Sralph 	tty.ty_type = p;
11217882Sralph 	p = skip(p);
11316420Sralph 	tty.ty_status = 0;
114*28308Skarels 	tty.ty_window = NULL;
11517882Sralph 	for (; *p; p = skip(p)) {
11628281Slepreau #define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n')
11728281Slepreau 		if (strncmp(p, "on", 2) == 0 && space(2))
11816420Sralph 			tty.ty_status |= TTY_ON;
11928281Slepreau 		else if (strncmp(p, "off", 3) == 0 && space(3))
12016420Sralph 			tty.ty_status &= ~TTY_ON;
12128281Slepreau 		else if (strncmp(p, "secure", 6) == 0 && space(6))
12216420Sralph 			tty.ty_status |= TTY_SECURE;
123*28308Skarels 		else if (strncmp(p, "window=", 7) == 0)
124*28308Skarels 			tty.ty_window = value(p);
125*28308Skarels 		else
12617882Sralph 			break;
12716420Sralph 	}
128*28308Skarels 	if (zapchar == '#' || *p == '#')
129*28308Skarels 		while ((c = *++p) == ' ' || c == '\t')
130*28308Skarels 			;
13117882Sralph 	tty.ty_comment = p;
132*28308Skarels 	if (*p == 0)
133*28308Skarels 		tty.ty_comment = 0;
13417882Sralph 	if (p = index(p, '\n'))
13517882Sralph 		*p = '\0';
13617882Sralph 	return(&tty);
13716420Sralph }
138