xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/getttyent.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 1985 Regents of the University of California.
30Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
40Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
50Sstevel@tonic-gate  */
60Sstevel@tonic-gate 
7*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
8*722Smuffin 
9*722Smuffin 
100Sstevel@tonic-gate #include <stdio.h>
110Sstevel@tonic-gate #include <strings.h>
120Sstevel@tonic-gate #include <ttyent.h>
130Sstevel@tonic-gate 
140Sstevel@tonic-gate static char *TTYFILE = "/etc/ttytab";
150Sstevel@tonic-gate #define LINE 256
160Sstevel@tonic-gate static struct _ttyentjunk {
170Sstevel@tonic-gate 	char	zapchar;
180Sstevel@tonic-gate 	FILE	*tf;
190Sstevel@tonic-gate 	char	line[LINE];
200Sstevel@tonic-gate 	struct	ttyent tty;
21*722Smuffin } *__ttyentjunk, *_ttyentjunk(void);
220Sstevel@tonic-gate 
230Sstevel@tonic-gate static struct _ttyentjunk *
_ttyentjunk(void)24*722Smuffin _ttyentjunk(void)
250Sstevel@tonic-gate {
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 	if (__ttyentjunk == 0)
280Sstevel@tonic-gate 		__ttyentjunk = (struct _ttyentjunk *)calloc(1, sizeof (struct _ttyentjunk));
290Sstevel@tonic-gate 	return (__ttyentjunk);
300Sstevel@tonic-gate }
310Sstevel@tonic-gate 
32*722Smuffin void
setttyent(void)33*722Smuffin setttyent(void)
340Sstevel@tonic-gate {
35*722Smuffin 	struct _ttyentjunk *t = _ttyentjunk();
360Sstevel@tonic-gate 
370Sstevel@tonic-gate 	if (t == 0)
380Sstevel@tonic-gate 		return;
390Sstevel@tonic-gate 	if (t->tf == NULL)
400Sstevel@tonic-gate 		t->tf = fopen(TTYFILE, "r");
410Sstevel@tonic-gate 	else
420Sstevel@tonic-gate 		rewind(t->tf);
430Sstevel@tonic-gate }
440Sstevel@tonic-gate 
45*722Smuffin void
endttyent(void)46*722Smuffin endttyent(void)
470Sstevel@tonic-gate {
48*722Smuffin 	struct _ttyentjunk *t = _ttyentjunk();
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	if (t == 0)
510Sstevel@tonic-gate 		return;
520Sstevel@tonic-gate 	if (t->tf != NULL) {
530Sstevel@tonic-gate 		(void) fclose(t->tf);
540Sstevel@tonic-gate 		t->tf = NULL;
550Sstevel@tonic-gate 	}
560Sstevel@tonic-gate }
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define QUOTED	1
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * Skip over the current field, removing quotes,
620Sstevel@tonic-gate  * and return a pointer to the next field.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate static char *
skip(char * p)65*722Smuffin skip(char *p)
660Sstevel@tonic-gate {
67*722Smuffin 	struct _ttyentjunk *t = _ttyentjunk();
68*722Smuffin 	char *cp = p;
69*722Smuffin 	int c;
70*722Smuffin 	int q = 0;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (t == 0)
730Sstevel@tonic-gate 		return (0);
740Sstevel@tonic-gate 	for (; (c = *p) != '\0'; p++) {
750Sstevel@tonic-gate 		if (c == '"') {
760Sstevel@tonic-gate 			q ^= QUOTED;	/* obscure, but nice */
770Sstevel@tonic-gate 			continue;
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
800Sstevel@tonic-gate 			p++;
810Sstevel@tonic-gate 		*cp++ = *p;
820Sstevel@tonic-gate 		if (q == QUOTED)
830Sstevel@tonic-gate 			continue;
840Sstevel@tonic-gate 		if (c == '#') {
850Sstevel@tonic-gate 			t->zapchar = c;
860Sstevel@tonic-gate 			*p = 0;
870Sstevel@tonic-gate 			break;
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 		if (c == '\t' || c == ' ' || c == '\n') {
900Sstevel@tonic-gate 			t->zapchar = c;
910Sstevel@tonic-gate 			*p++ = 0;
920Sstevel@tonic-gate 			while ((c = *p) == '\t' || c == ' ' || c == '\n')
930Sstevel@tonic-gate 				p++;
940Sstevel@tonic-gate 			break;
950Sstevel@tonic-gate 		}
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	*--cp = '\0';
980Sstevel@tonic-gate 	return (p);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static char *
value(char * p)102*722Smuffin value(char *p)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	if ((p = index(p,'=')) == 0)
105*722Smuffin 		return (NULL);
1060Sstevel@tonic-gate 	p++;			/* get past the = sign */
107*722Smuffin 	return (p);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate struct ttyent *
getttyent(void)111*722Smuffin getttyent(void)
1120Sstevel@tonic-gate {
113*722Smuffin 	struct _ttyentjunk *t = _ttyentjunk();
114*722Smuffin 	char *p;
115*722Smuffin 	int c;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (t == 0)
1180Sstevel@tonic-gate 		return (NULL);
1190Sstevel@tonic-gate 	if (t->tf == NULL) {
1200Sstevel@tonic-gate 		if ((t->tf = fopen(TTYFILE, "r")) == NULL)
1210Sstevel@tonic-gate 			return (NULL);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 	do {
1240Sstevel@tonic-gate 		p = fgets(t->line, LINE, t->tf);
1250Sstevel@tonic-gate 		if (p == NULL)
1260Sstevel@tonic-gate 			return (NULL);
1270Sstevel@tonic-gate 		while ((c = *p) == '\t' || c == ' ' || c == '\n')
1280Sstevel@tonic-gate 			p++;
1290Sstevel@tonic-gate 	} while (c == '\0' || c == '#');
1300Sstevel@tonic-gate 	t->zapchar = 0;
1310Sstevel@tonic-gate 	t->tty.ty_name = p;
1320Sstevel@tonic-gate 	p = skip(p);
1330Sstevel@tonic-gate 	t->tty.ty_getty = p;
1340Sstevel@tonic-gate 	p = skip(p);
1350Sstevel@tonic-gate 	t->tty.ty_type = p;
1360Sstevel@tonic-gate 	p = skip(p);
1370Sstevel@tonic-gate 	t->tty.ty_status = 0;
1380Sstevel@tonic-gate 	t->tty.ty_window = NULL;
1390Sstevel@tonic-gate 	for (; *p; p = skip(p)) {
1400Sstevel@tonic-gate #define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n')
1410Sstevel@tonic-gate 		if (strncmp(p, "on", 2) == 0 && space(2))
1420Sstevel@tonic-gate 			t->tty.ty_status |= TTY_ON;
1430Sstevel@tonic-gate 		else if (strncmp(p, "off", 3) == 0 && space(3))
1440Sstevel@tonic-gate 			t->tty.ty_status &= ~TTY_ON;
1450Sstevel@tonic-gate 		else if (strncmp(p, "secure", 6) == 0 && space(6))
1460Sstevel@tonic-gate 			t->tty.ty_status |= TTY_SECURE;
1470Sstevel@tonic-gate 		else if (strncmp(p, "local", 5) == 0 && space(5))
1480Sstevel@tonic-gate 			t->tty.ty_status |= TTY_LOCAL;
1490Sstevel@tonic-gate 		else if (strncmp(p, "window=", 7) == 0)
1500Sstevel@tonic-gate 			t->tty.ty_window = value(p);
1510Sstevel@tonic-gate 		else
1520Sstevel@tonic-gate 			break;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	if (t->zapchar == '#' || *p == '#')
1550Sstevel@tonic-gate 		while ((c = *++p) == ' ' || c == '\t')
1560Sstevel@tonic-gate 			;
1570Sstevel@tonic-gate 	t->tty.ty_comment = p;
1580Sstevel@tonic-gate 	if (*p == 0)
1590Sstevel@tonic-gate 		t->tty.ty_comment = 0;
1600Sstevel@tonic-gate 	if (p = index(p, '\n'))
1610Sstevel@tonic-gate 		*p = '\0';
162*722Smuffin 	return (&t->tty);
1630Sstevel@tonic-gate }
164