xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 42625)
121343Sdist /*
239176Sbostic  * Copyright (c) 1989 The Regents of the University of California.
339176Sbostic  * All rights reserved.
439176Sbostic  *
5*42625Sbostic  * %sccs.include.redist.c%
621343Sdist  */
716420Sralph 
826562Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42625Sbostic static char sccsid[] = "@(#)getttyent.c	5.8 (Berkeley) 06/01/90";
1039176Sbostic #endif /* LIBC_SCCS and not lint */
1121343Sdist 
1239176Sbostic #include <ttyent.h>
1316420Sralph #include <stdio.h>
1439176Sbostic #include <ctype.h>
1542024Sbostic #include <string.h>
1616420Sralph 
1728308Skarels static char zapchar;
1839176Sbostic static FILE *tf;
1916420Sralph 
2039176Sbostic struct ttyent *
2139176Sbostic getttynam(tty)
2239176Sbostic 	char *tty;
2316420Sralph {
2439176Sbostic 	register struct ttyent *t;
2539176Sbostic 
2639176Sbostic 	setttyent();
2739176Sbostic 	while (t = getttyent())
2839176Sbostic 		if (!strcmp(tty, t->ty_name))
2939176Sbostic 			break;
3039176Sbostic 	return(t);
3116420Sralph }
3216420Sralph 
3339176Sbostic struct ttyent *
3439176Sbostic getttyent()
3516420Sralph {
3639176Sbostic 	static struct ttyent tty;
3739176Sbostic 	register int c;
3839176Sbostic 	register char *p;
3939176Sbostic #define	MAXLINELENGTH	100
4039176Sbostic 	static char line[MAXLINELENGTH];
4139176Sbostic 	char *skip(), *value();
4239176Sbostic 
4339176Sbostic 	if (!tf && !setttyent())
4439176Sbostic 		return(NULL);
4540204Sbostic 	for (;;) {
4640204Sbostic 		if (!fgets(p = line, sizeof(line), tf))
4739176Sbostic 			return(NULL);
4839176Sbostic 		/* skip lines that are too big */
4940204Sbostic 		if (!index(p, '\n')) {
5039176Sbostic 			while ((c = getc(tf)) != '\n' && c != EOF)
5139176Sbostic 				;
5239176Sbostic 			continue;
5339176Sbostic 		}
5440204Sbostic 		while (isspace(*p))
5540204Sbostic 			++p;
5640204Sbostic 		if (*p && *p != '#')
5740204Sbostic 			break;
5840204Sbostic 	}
5939176Sbostic 
6039176Sbostic 	zapchar = 0;
6139176Sbostic 	tty.ty_name = p;
6239176Sbostic 	p = skip(p);
6339176Sbostic 	if (!*(tty.ty_getty = p))
6439176Sbostic 		tty.ty_getty = tty.ty_type = NULL;
6539176Sbostic 	else {
6639176Sbostic 		p = skip(p);
6739176Sbostic 		if (!*(tty.ty_type = p))
6839176Sbostic 			tty.ty_type = NULL;
6939176Sbostic 		else
7039176Sbostic 			p = skip(p);
7116420Sralph 	}
7239176Sbostic 	tty.ty_status = 0;
7339176Sbostic 	tty.ty_window = NULL;
7439176Sbostic 
7539176Sbostic #define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
7639176Sbostic #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
7739176Sbostic 	for (; *p; p = skip(p)) {
7839176Sbostic 		if (scmp(_TTYS_OFF))
7939176Sbostic 			tty.ty_status &= ~TTY_ON;
8039176Sbostic 		else if (scmp(_TTYS_ON))
8139176Sbostic 			tty.ty_status |= TTY_ON;
8239176Sbostic 		else if (scmp(_TTYS_SECURE))
8339176Sbostic 			tty.ty_status |= TTY_SECURE;
8439176Sbostic 		else if (vcmp(_TTYS_WINDOW))
8539176Sbostic 			tty.ty_window = value(p);
8639176Sbostic 		else
8739176Sbostic 			break;
8839176Sbostic 	}
8939176Sbostic 
9039176Sbostic 	if (zapchar == '#' || *p == '#')
9139176Sbostic 		while ((c = *++p) == ' ' || c == '\t')
9239176Sbostic 			;
9339176Sbostic 	tty.ty_comment = p;
9439176Sbostic 	if (*p == 0)
9539176Sbostic 		tty.ty_comment = 0;
9639176Sbostic 	if (p = index(p, '\n'))
9739176Sbostic 		*p = '\0';
9839176Sbostic 	return(&tty);
9916420Sralph }
10016420Sralph 
10139176Sbostic #define	QUOTED	1
10217882Sralph 
10317882Sralph /*
10439176Sbostic  * Skip over the current field, removing quotes, and return a pointer to
10539176Sbostic  * the next field.
10617882Sralph  */
10716420Sralph static char *
10816420Sralph skip(p)
10917882Sralph 	register char *p;
11016420Sralph {
11139176Sbostic 	register char *t;
11239176Sbostic 	register int c, q;
11316420Sralph 
11439176Sbostic 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
11517882Sralph 		if (c == '"') {
11617882Sralph 			q ^= QUOTED;	/* obscure, but nice */
11717882Sralph 			continue;
11817882Sralph 		}
11928308Skarels 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
12028308Skarels 			p++;
12128308Skarels 		*t++ = *p;
12217882Sralph 		if (q == QUOTED)
12317882Sralph 			continue;
12416420Sralph 		if (c == '#') {
12528308Skarels 			zapchar = c;
12628308Skarels 			*p = 0;
12716420Sralph 			break;
12816420Sralph 		}
12916420Sralph 		if (c == '\t' || c == ' ' || c == '\n') {
13028308Skarels 			zapchar = c;
13128308Skarels 			*p++ = 0;
13216420Sralph 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
13316420Sralph 				p++;
13416420Sralph 			break;
13516420Sralph 		}
13616420Sralph 	}
13728308Skarels 	*--t = '\0';
13839176Sbostic 	return(p);
13916420Sralph }
14016420Sralph 
14117882Sralph static char *
14217882Sralph value(p)
14317882Sralph 	register char *p;
14417882Sralph {
14539176Sbostic 	return((p = index(p, '=')) ? ++p : NULL);
14617882Sralph }
14717882Sralph 
14839176Sbostic setttyent()
14916420Sralph {
15039176Sbostic 	if (tf) {
15139176Sbostic 		(void)rewind(tf);
15239176Sbostic 		return(1);
15339176Sbostic 	} else if (tf = fopen(_PATH_TTYS, "r"))
15439176Sbostic 		return(1);
15539176Sbostic 	return(0);
15639176Sbostic }
15716420Sralph 
15839176Sbostic endttyent()
15939176Sbostic {
16039176Sbostic 	int rval;
16139176Sbostic 
16239176Sbostic 	if (tf) {
16339176Sbostic 		rval = !(fclose(tf) == EOF);
16439176Sbostic 		tf = NULL;
16539176Sbostic 		return(rval);
16616420Sralph 	}
16739176Sbostic 	return(1);
16816420Sralph }
169