xref: /csrg-svn/lib/libc/gen/getttyent.c (revision 28281)
121343Sdist /*
221343Sdist  * Copyright (c) 1983 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*28281Slepreau static char sccsid[] = "@(#)getttyent.c	5.3 (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";
1616420Sralph static char EMPTY[] = "";
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 
3817882Sralph #define QUOTED 1
3917882Sralph 
4017882Sralph /*
4117882Sralph  * Skip over the current field and
4217882Sralph  * return a pointer to the next field.
4317882Sralph  */
4416420Sralph static char *
4516420Sralph skip(p)
4617882Sralph 	register char *p;
4716420Sralph {
4816420Sralph 	register int c;
4917882Sralph 	register int q = 0;
5016420Sralph 
5117882Sralph 	for (; (c = *p) != '\0'; p++) {
5217882Sralph 		if (c == '"') {
5317882Sralph 			q ^= QUOTED;	/* obscure, but nice */
5417882Sralph 			continue;
5517882Sralph 		}
5617882Sralph 		if (q == QUOTED)
5717882Sralph 			continue;
5816420Sralph 		if (c == '#') {
5916420Sralph 			*p = '\0';
6016420Sralph 			break;
6116420Sralph 		}
6216420Sralph 		if (c == '\t' || c == ' ' || c == '\n') {
6316420Sralph 			*p++ = '\0';
6416420Sralph 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
6516420Sralph 				p++;
6616420Sralph 			break;
6716420Sralph 		}
6816420Sralph 	}
6916420Sralph 	return (p);
7016420Sralph }
7116420Sralph 
7217882Sralph static char *
7317882Sralph value(p)
7417882Sralph 	register char *p;
7517882Sralph {
7617882Sralph 	if ((p = index(p,'=')) == 0)
7717882Sralph 		return(NULL);
7817882Sralph 	p++;			/* get past the = sign */
7917882Sralph 	return(p);
8017882Sralph }
8117882Sralph 
8217882Sralph /* get rid of quotes. */
8317882Sralph 
8417882Sralph static
8517882Sralph qremove(p)
8617882Sralph 	register char *p;
8717882Sralph {
8817882Sralph 	register char *t;
8917882Sralph 
9017882Sralph 	for (t = p; *p; p++)
9117882Sralph 		if (*p != '"')
9217882Sralph 			*t++ = *p;
9317882Sralph 	*t = '\0';
9417882Sralph }
9517882Sralph 
9616420Sralph struct ttyent *
9716420Sralph getttyent()
9816420Sralph {
9917882Sralph 	register char *p;
10016420Sralph 	register int c;
10116420Sralph 
10216420Sralph 	if (tf == NULL) {
10316420Sralph 		if ((tf = fopen(TTYFILE, "r")) == NULL)
10416420Sralph 			return (NULL);
10516420Sralph 	}
10616420Sralph 	do {
10717882Sralph 		p = fgets(line, LINE, tf);
10816420Sralph 		if (p == NULL)
10916420Sralph 			return (NULL);
11016420Sralph 		while ((c = *p) == '\t' || c == ' ' || c == '\n')
11116420Sralph 			p++;
11216420Sralph 	} while (c == '\0' || c == '#');
11316420Sralph 	tty.ty_name = p;
11416420Sralph 	p = skip(p);
11516420Sralph 	tty.ty_getty = p;
11616420Sralph 	p = skip(p);
11716420Sralph 	tty.ty_type = p;
11817882Sralph 	p = skip(p);
11916420Sralph 	tty.ty_status = 0;
12017882Sralph 	tty.ty_window = EMPTY;
12117882Sralph 	for (; *p; p = skip(p)) {
122*28281Slepreau #define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n')
123*28281Slepreau 		if (strncmp(p, "on", 2) == 0 && space(2))
12416420Sralph 			tty.ty_status |= TTY_ON;
125*28281Slepreau 		else if (strncmp(p, "off", 3) == 0 && space(3))
12616420Sralph 			tty.ty_status &= ~TTY_ON;
127*28281Slepreau 		else if (strncmp(p, "secure", 6) == 0 && space(6))
12816420Sralph 			tty.ty_status |= TTY_SECURE;
12917882Sralph 		else if (strncmp(p, "window", 6) == 0) {
13017882Sralph 			if ((tty.ty_window = value(p)) == NULL)
13117882Sralph 				tty.ty_window = EMPTY;
13217882Sralph 		} else
13317882Sralph 			break;
13416420Sralph 	}
13517882Sralph 	tty.ty_comment = p;
13617882Sralph 	if (p = index(p, '\n'))
13717882Sralph 		*p = '\0';
13817882Sralph 	qremove(tty.ty_getty);
13917882Sralph 	qremove(tty.ty_window);
14017882Sralph 	qremove(tty.ty_comment);
14117882Sralph 	return(&tty);
14216420Sralph }
143