Blame misc/getttyent.c

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 1989, 1993
Packit 6c4009
 *	The Regents of the University of California.  All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions
Packit 6c4009
 * are met:
Packit 6c4009
 * 1. Redistributions of source code must retain the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer.
Packit 6c4009
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer in the
Packit 6c4009
 *    documentation and/or other materials provided with the distribution.
Packit 6c4009
 * 4. Neither the name of the University nor the names of its contributors
Packit 6c4009
 *    may be used to endorse or promote products derived from this software
Packit 6c4009
 *    without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 6c4009
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 6c4009
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6c4009
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 6c4009
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 6c4009
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 6c4009
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 6c4009
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 6c4009
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 6c4009
 * SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#if defined(LIBC_SCCS) && !defined(lint)
Packit 6c4009
static char sccsid[] = "@(#)getttyent.c	8.1 (Berkeley) 6/4/93";
Packit 6c4009
#endif /* LIBC_SCCS and not lint */
Packit 6c4009
Packit 6c4009
#include <ttyent.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdio_ext.h>
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#define flockfile(s) _IO_flockfile (s)
Packit 6c4009
#define funlockfile(s) _IO_funlockfile (s)
Packit 6c4009
Packit 6c4009
static char zapchar;
Packit 6c4009
static FILE *tf;
Packit 6c4009
Packit 6c4009
struct ttyent *
Packit 6c4009
__getttynam (const char *tty)
Packit 6c4009
{
Packit 6c4009
	struct ttyent *t;
Packit 6c4009
Packit 6c4009
	__setttyent();
Packit 6c4009
	while ((t = __getttyent()))
Packit 6c4009
		if (!strcmp(tty, t->ty_name))
Packit 6c4009
			break;
Packit 6c4009
	__endttyent();
Packit 6c4009
	return (t);
Packit 6c4009
}
Packit 6c4009
weak_alias (__getttynam, getttynam)
Packit 6c4009
Packit 6c4009
static char *skip (char *) __THROW;
Packit 6c4009
static char *value (char *) __THROW;
Packit 6c4009
Packit 6c4009
struct ttyent *
Packit 6c4009
__getttyent (void)
Packit 6c4009
{
Packit 6c4009
	static struct ttyent tty;
Packit 6c4009
	int c;
Packit 6c4009
	char *p;
Packit 6c4009
#define	MAXLINELENGTH	100
Packit 6c4009
	static char line[MAXLINELENGTH];
Packit 6c4009
Packit 6c4009
	if (!tf && !__setttyent())
Packit 6c4009
		return (NULL);
Packit 6c4009
	flockfile (tf);
Packit 6c4009
	for (;;) {
Packit 6c4009
		if (!__fgets_unlocked(p = line, sizeof(line), tf)) {
Packit 6c4009
			funlockfile (tf);
Packit 6c4009
			return (NULL);
Packit 6c4009
		}
Packit 6c4009
		/* skip lines that are too big */
Packit 6c4009
		if (!strchr (p, '\n')) {
Packit 6c4009
			while ((c = __getc_unlocked(tf)) != '\n' && c != EOF)
Packit 6c4009
				;
Packit 6c4009
			continue;
Packit 6c4009
		}
Packit 6c4009
		while (isspace(*p))
Packit 6c4009
			++p;
Packit 6c4009
		if (*p && *p != '#')
Packit 6c4009
			break;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	zapchar = 0;
Packit 6c4009
	tty.ty_name = p;
Packit 6c4009
	p = skip(p);
Packit 6c4009
	if (!*(tty.ty_getty = p))
Packit 6c4009
		tty.ty_getty = tty.ty_type = NULL;
Packit 6c4009
	else {
Packit 6c4009
		p = skip(p);
Packit 6c4009
		if (!*(tty.ty_type = p))
Packit 6c4009
			tty.ty_type = NULL;
Packit 6c4009
		else
Packit 6c4009
			p = skip(p);
Packit 6c4009
	}
Packit 6c4009
	tty.ty_status = 0;
Packit 6c4009
	tty.ty_window = NULL;
Packit 6c4009
Packit 6c4009
#define	scmp(e)	!strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
Packit 6c4009
#define	vcmp(e)	!strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
Packit 6c4009
	for (; *p; p = skip(p)) {
Packit 6c4009
		if (scmp(_TTYS_OFF))
Packit 6c4009
			tty.ty_status &= ~TTY_ON;
Packit 6c4009
		else if (scmp(_TTYS_ON))
Packit 6c4009
			tty.ty_status |= TTY_ON;
Packit 6c4009
		else if (scmp(_TTYS_SECURE))
Packit 6c4009
			tty.ty_status |= TTY_SECURE;
Packit 6c4009
		else if (vcmp(_TTYS_WINDOW))
Packit 6c4009
			tty.ty_window = value(p);
Packit 6c4009
		else
Packit 6c4009
			break;
Packit 6c4009
	}
Packit 6c4009
	/* We can release the lock only here since `zapchar' is global.  */
Packit 6c4009
	funlockfile(tf);
Packit 6c4009
Packit 6c4009
	if (zapchar == '#' || *p == '#')
Packit 6c4009
		while ((c = *++p) == ' ' || c == '\t')
Packit 6c4009
			;
Packit 6c4009
	tty.ty_comment = p;
Packit 6c4009
	if (*p == 0)
Packit 6c4009
		tty.ty_comment = 0;
Packit 6c4009
	if ((p = strchr (p, '\n')))
Packit 6c4009
		*p = '\0';
Packit 6c4009
	return (&tty);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__getttyent)
Packit 6c4009
weak_alias (__getttyent, getttyent)
Packit 6c4009
Packit 6c4009
#define	QUOTED	1
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Skip over the current field, removing quotes, and return a pointer to
Packit 6c4009
 * the next field.
Packit 6c4009
 */
Packit 6c4009
static char *
Packit 6c4009
skip (char *p)
Packit 6c4009
{
Packit 6c4009
	char *t;
Packit 6c4009
	int c, q;
Packit 6c4009
Packit 6c4009
	for (q = 0, t = p; (c = *p) != '\0'; p++) {
Packit 6c4009
		if (c == '"') {
Packit 6c4009
			q ^= QUOTED;	/* obscure, but nice */
Packit 6c4009
			continue;
Packit 6c4009
		}
Packit 6c4009
		if (q == QUOTED && *p == '\\' && *(p+1) == '"')
Packit 6c4009
			p++;
Packit 6c4009
		*t++ = *p;
Packit 6c4009
		if (q == QUOTED)
Packit 6c4009
			continue;
Packit 6c4009
		if (c == '#') {
Packit 6c4009
			zapchar = c;
Packit 6c4009
			*p = 0;
Packit 6c4009
			break;
Packit 6c4009
		}
Packit 6c4009
		if (c == '\t' || c == ' ' || c == '\n') {
Packit 6c4009
			zapchar = c;
Packit 6c4009
			*p++ = 0;
Packit 6c4009
			while ((c = *p) == '\t' || c == ' ' || c == '\n')
Packit 6c4009
				p++;
Packit 6c4009
			break;
Packit 6c4009
		}
Packit 6c4009
	}
Packit 6c4009
	*--t = '\0';
Packit 6c4009
	return (p);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
value (char *p)
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
	return ((p = strchr (p, '=')) ? ++p : NULL);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__setttyent (void)
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
	if (tf) {
Packit 6c4009
		(void)rewind(tf);
Packit 6c4009
		return (1);
Packit 6c4009
	} else if ((tf = fopen(_PATH_TTYS, "rce"))) {
Packit 6c4009
		/* We do the locking ourselves.  */
Packit 6c4009
		__fsetlocking (tf, FSETLOCKING_BYCALLER);
Packit 6c4009
		return (1);
Packit 6c4009
	}
Packit 6c4009
	return (0);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__setttyent)
Packit 6c4009
weak_alias (__setttyent, setttyent)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__endttyent (void)
Packit 6c4009
{
Packit 6c4009
	int rval;
Packit 6c4009
Packit 6c4009
	if (tf) {
Packit 6c4009
		rval = !(fclose(tf) == EOF);
Packit 6c4009
		tf = NULL;
Packit 6c4009
		return (rval);
Packit 6c4009
	}
Packit 6c4009
	return (1);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__endttyent)
Packit 6c4009
weak_alias (__endttyent, endttyent)