1*60594Sbostic /*-
2*60594Sbostic  * Copyright (c) 1993 The Regents of the University of California.
338027Sbostic  * All rights reserved.
438027Sbostic  *
538027Sbostic  * This code is derived from software contributed to Berkeley by
638027Sbostic  * Bill Jolitz.
738027Sbostic  *
8*60594Sbostic  * %sccs.include.redist.c%
938027Sbostic  */
1038027Sbostic 
1138027Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*60594Sbostic static char sccsid[] = "@(#)getttyent.c	5.2 (Berkeley) 05/29/93";
1338027Sbostic #endif /* LIBC_SCCS and not lint */
1438027Sbostic 
1538027Sbostic #include <stdio.h>
1638027Sbostic #include <strings.h>
1738027Sbostic #include <ttyent.h>
1838027Sbostic 
1938027Sbostic static char TTYFILE[] = "/etc/ttys";
2038027Sbostic static char zapchar;
2138027Sbostic static FILE *tf = NULL;
2238027Sbostic #define LINE 256
2338027Sbostic static char line[LINE];
2438027Sbostic static struct ttyent tty;
2538027Sbostic #define NOPTS 32
2638027Sbostic static char *options[NOPTS];
2738027Sbostic 
setttyent()2838027Sbostic setttyent()
2938027Sbostic {
3038027Sbostic 	if (tf == NULL)
3138027Sbostic 		tf = fopen(TTYFILE, "r");
3238027Sbostic 	else
3338027Sbostic 		rewind(tf);
3438027Sbostic }
3538027Sbostic 
endttyent()3638027Sbostic endttyent()
3738027Sbostic {
3838027Sbostic 	if (tf != NULL) {
3938027Sbostic 		(void) fclose(tf);
4038027Sbostic 		tf = NULL;
4138027Sbostic 	}
4238027Sbostic }
4338027Sbostic 
4438027Sbostic #define QUOTED	1
4538027Sbostic 
4638027Sbostic /*
4738027Sbostic  * Skip over the current field, removing quotes,
4838027Sbostic  * and return a pointer to the next field.
4938027Sbostic  */
5038027Sbostic static char *
skip(p)5138027Sbostic skip(p)
5238027Sbostic 	register char *p;
5338027Sbostic {
5438027Sbostic 	register char *t = p;
5538027Sbostic 	register int c;
5638027Sbostic 	register int q = 0;
5738027Sbostic 
5838027Sbostic 	for (; (c = *p) != '\0'; p++) {
5938027Sbostic 		if (c == '"') {
6038027Sbostic 			q ^= QUOTED;	/* obscure, but nice */
6138027Sbostic 			continue;
6238027Sbostic 		}
6338027Sbostic 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
6438027Sbostic 			p++;
6538027Sbostic 		*t++ = *p;
6638027Sbostic 		if (q == QUOTED)
6738027Sbostic 			continue;
6838027Sbostic 		if (c == '#') {
6938027Sbostic 			zapchar = c;
7038027Sbostic 			*p = 0;
7138027Sbostic 			break;
7238027Sbostic 		}
7338027Sbostic 		if (c == '\t' || c == ' ' || c == '\n') {
7438027Sbostic 			zapchar = c;
7538027Sbostic 			*p++ = 0;
7638027Sbostic 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
7738027Sbostic 				p++;
7838027Sbostic 			break;
7938027Sbostic 		}
8038027Sbostic 	}
8138027Sbostic 	*--t = '\0';
8238027Sbostic 	return (p);
8338027Sbostic }
8438027Sbostic 
8538027Sbostic static char *
value(p)8638027Sbostic value(p)
8738027Sbostic 	register char *p;
8838027Sbostic {
8938027Sbostic 	if ((p = index(p,'=')) == 0)
9038027Sbostic 		return(NULL);
9138027Sbostic 	p++;			/* get past the = sign */
9238027Sbostic 	return(p);
9338027Sbostic }
9438027Sbostic 
9538027Sbostic struct ttyent *
getttyent()9638027Sbostic getttyent()
9738027Sbostic {
9838027Sbostic 	register char *p;
9938027Sbostic 	register int c;
10038027Sbostic 	register char **op;
10138027Sbostic 
10238027Sbostic 	if (tf == NULL) {
10338027Sbostic 		if ((tf = fopen(TTYFILE, "r")) == NULL)
10438027Sbostic 			return (NULL);
10538027Sbostic 	}
10638027Sbostic 	do {
10738027Sbostic 		p = fgets(line, LINE, tf);
10838027Sbostic 		if (p == NULL)
10938027Sbostic 			return (NULL);
11038027Sbostic 		while ((c = *p) == '\t' || c == ' ' || c == '\n')
11138027Sbostic 			p++;
11238027Sbostic 	} while (c == '\0' || c == '#');
11338027Sbostic 	zapchar = 0;
11438027Sbostic 	tty.ty_name = p;
11538027Sbostic 	p = skip(p);
11638027Sbostic 	tty.ty_getty = p;
11738027Sbostic 	p = skip(p);
11838027Sbostic 	tty.ty_type = p;
11938027Sbostic 	p = skip(p);
12038027Sbostic 	tty.ty_status = 0;
12138027Sbostic 	tty.ty_window = NULL;
12238027Sbostic 	tty.ty_flags = options ;
12338027Sbostic 	op = options ;
12438027Sbostic 	for (; *p; p = skip(p)) {
12538027Sbostic 		*op++ = p ; /* bounds check ? */
12638027Sbostic #define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n')
12738027Sbostic 		if (strncmp(p, "on", 2) == 0 && space(2))
12838027Sbostic 			tty.ty_status |= TTY_ON;
12938027Sbostic 		else if (strncmp(p, "off", 3) == 0 && space(3))
13038027Sbostic 			tty.ty_status &= ~TTY_ON;
13138027Sbostic 		else if (strncmp(p, "secure", 6) == 0 && space(6))
13238027Sbostic 			tty.ty_status |= TTY_SECURE;
13338027Sbostic 		else if (strncmp(p, "window=", 7) == 0)
13438027Sbostic 			tty.ty_window = value(p);
13538027Sbostic 		else if (zapchar == '#' || *p == '#')
13638027Sbostic 			break;
13738027Sbostic 	}
13838027Sbostic 	*op = 0 ;	/* end sentinel */
13938027Sbostic 	if (zapchar == '#' || *p == '#')
14038027Sbostic 		while ((c = *++p) == ' ' || c == '\t')
14138027Sbostic 			;
14238027Sbostic 	tty.ty_comment = p;
14338027Sbostic 	if (*p == 0)
14438027Sbostic 		tty.ty_comment = 0;
14538027Sbostic 	if (p = index(p, '\n'))
14638027Sbostic 		*p = '\0';
14738027Sbostic 	return(&tty);
14838027Sbostic }
149