xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 40204)
121343Sdist /*
239176Sbostic  * Copyright (c) 1989 The Regents of the University of California.
339176Sbostic  * All rights reserved.
439176Sbostic  *
539176Sbostic  * Redistribution and use in source and binary forms are permitted
639176Sbostic  * provided that the above copyright notice and this paragraph are
739176Sbostic  * duplicated in all such forms and that any documentation,
839176Sbostic  * advertising materials, and other materials related to such
939176Sbostic  * distribution and use acknowledge that the software was developed
1039176Sbostic  * by the University of California, Berkeley.  The name of the
1139176Sbostic  * University may not be used to endorse or promote products derived
1239176Sbostic  * from this software without specific prior written permission.
1339176Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1439176Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1539176Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621343Sdist  */
1716420Sralph 
1826562Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*40204Sbostic static char sccsid[] = "@(#)getttyent.c	5.6 (Berkeley) 02/22/90";
2039176Sbostic #endif /* LIBC_SCCS and not lint */
2121343Sdist 
2239176Sbostic #include <ttyent.h>
2316420Sralph #include <stdio.h>
2439176Sbostic #include <ctype.h>
2517882Sralph #include <strings.h>
2616420Sralph 
2728308Skarels static char zapchar;
2839176Sbostic static FILE *tf;
2916420Sralph 
3039176Sbostic struct ttyent *
3139176Sbostic getttynam(tty)
3239176Sbostic 	char *tty;
3316420Sralph {
3439176Sbostic 	register struct ttyent *t;
3539176Sbostic 
3639176Sbostic 	setttyent();
3739176Sbostic 	while (t = getttyent())
3839176Sbostic 		if (!strcmp(tty, t->ty_name))
3939176Sbostic 			break;
4039176Sbostic 	return(t);
4116420Sralph }
4216420Sralph 
4339176Sbostic struct ttyent *
4439176Sbostic getttyent()
4516420Sralph {
4639176Sbostic 	static struct ttyent tty;
4739176Sbostic 	register int c;
4839176Sbostic 	register char *p;
4939176Sbostic #define	MAXLINELENGTH	100
5039176Sbostic 	static char line[MAXLINELENGTH];
5139176Sbostic 	char *skip(), *value();
5239176Sbostic 
5339176Sbostic 	if (!tf && !setttyent())
5439176Sbostic 		return(NULL);
55*40204Sbostic 	for (;;) {
56*40204Sbostic 		if (!fgets(p = line, sizeof(line), tf))
5739176Sbostic 			return(NULL);
5839176Sbostic 		/* skip lines that are too big */
59*40204Sbostic 		if (!index(p, '\n')) {
6039176Sbostic 			while ((c = getc(tf)) != '\n' && c != EOF)
6139176Sbostic 				;
6239176Sbostic 			continue;
6339176Sbostic 		}
64*40204Sbostic 		while (isspace(*p))
65*40204Sbostic 			++p;
66*40204Sbostic 		if (*p && *p != '#')
67*40204Sbostic 			break;
68*40204Sbostic 	}
6939176Sbostic 
7039176Sbostic 	zapchar = 0;
7139176Sbostic 	tty.ty_name = p;
7239176Sbostic 	p = skip(p);
7339176Sbostic 	if (!*(tty.ty_getty = p))
7439176Sbostic 		tty.ty_getty = tty.ty_type = NULL;
7539176Sbostic 	else {
7639176Sbostic 		p = skip(p);
7739176Sbostic 		if (!*(tty.ty_type = p))
7839176Sbostic 			tty.ty_type = NULL;
7939176Sbostic 		else
8039176Sbostic 			p = skip(p);
8116420Sralph 	}
8239176Sbostic 	tty.ty_status = 0;
8339176Sbostic 	tty.ty_window = NULL;
8439176Sbostic 
8539176Sbostic #define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
8639176Sbostic #define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
8739176Sbostic 	for (; *p; p = skip(p)) {
8839176Sbostic 		if (scmp(_TTYS_OFF))
8939176Sbostic 			tty.ty_status &= ~TTY_ON;
9039176Sbostic 		else if (scmp(_TTYS_ON))
9139176Sbostic 			tty.ty_status |= TTY_ON;
9239176Sbostic 		else if (scmp(_TTYS_SECURE))
9339176Sbostic 			tty.ty_status |= TTY_SECURE;
9439176Sbostic 		else if (vcmp(_TTYS_WINDOW))
9539176Sbostic 			tty.ty_window = value(p);
9639176Sbostic 		else
9739176Sbostic 			break;
9839176Sbostic 	}
9939176Sbostic 
10039176Sbostic 	if (zapchar == '#' || *p == '#')
10139176Sbostic 		while ((c = *++p) == ' ' || c == '\t')
10239176Sbostic 			;
10339176Sbostic 	tty.ty_comment = p;
10439176Sbostic 	if (*p == 0)
10539176Sbostic 		tty.ty_comment = 0;
10639176Sbostic 	if (p = index(p, '\n'))
10739176Sbostic 		*p = '\0';
10839176Sbostic 	return(&tty);
10916420Sralph }
11016420Sralph 
11139176Sbostic #define	QUOTED	1
11217882Sralph 
11317882Sralph /*
11439176Sbostic  * Skip over the current field, removing quotes, and return a pointer to
11539176Sbostic  * the next field.
11617882Sralph  */
11716420Sralph static char *
11816420Sralph skip(p)
11917882Sralph 	register char *p;
12016420Sralph {
12139176Sbostic 	register char *t;
12239176Sbostic 	register int c, q;
12316420Sralph 
12439176Sbostic 	for (q = 0, t = p; (c = *p) != '\0'; p++) {
12517882Sralph 		if (c == '"') {
12617882Sralph 			q ^= QUOTED;	/* obscure, but nice */
12717882Sralph 			continue;
12817882Sralph 		}
12928308Skarels 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
13028308Skarels 			p++;
13128308Skarels 		*t++ = *p;
13217882Sralph 		if (q == QUOTED)
13317882Sralph 			continue;
13416420Sralph 		if (c == '#') {
13528308Skarels 			zapchar = c;
13628308Skarels 			*p = 0;
13716420Sralph 			break;
13816420Sralph 		}
13916420Sralph 		if (c == '\t' || c == ' ' || c == '\n') {
14028308Skarels 			zapchar = c;
14128308Skarels 			*p++ = 0;
14216420Sralph 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
14316420Sralph 				p++;
14416420Sralph 			break;
14516420Sralph 		}
14616420Sralph 	}
14728308Skarels 	*--t = '\0';
14839176Sbostic 	return(p);
14916420Sralph }
15016420Sralph 
15117882Sralph static char *
15217882Sralph value(p)
15317882Sralph 	register char *p;
15417882Sralph {
15539176Sbostic 	return((p = index(p, '=')) ? ++p : NULL);
15617882Sralph }
15717882Sralph 
15839176Sbostic setttyent()
15916420Sralph {
16039176Sbostic 	if (tf) {
16139176Sbostic 		(void)rewind(tf);
16239176Sbostic 		return(1);
16339176Sbostic 	} else if (tf = fopen(_PATH_TTYS, "r"))
16439176Sbostic 		return(1);
16539176Sbostic 	return(0);
16639176Sbostic }
16716420Sralph 
16839176Sbostic endttyent()
16939176Sbostic {
17039176Sbostic 	int rval;
17139176Sbostic 
17239176Sbostic 	if (tf) {
17339176Sbostic 		rval = !(fclose(tf) == EOF);
17439176Sbostic 		tf = NULL;
17539176Sbostic 		return(rval);
17616420Sralph 	}
17739176Sbostic 	return(1);
17816420Sralph }
179