getttyent.c revision 1574
180219Swpaul/* 280219Swpaul * Copyright (c) 1989, 1993 380219Swpaul * The Regents of the University of California. All rights reserved. 480219Swpaul * 580219Swpaul * Redistribution and use in source and binary forms, with or without 680219Swpaul * modification, are permitted provided that the following conditions 780219Swpaul * are met: 880219Swpaul * 1. Redistributions of source code must retain the above copyright 980219Swpaul * notice, this list of conditions and the following disclaimer. 1080219Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1180219Swpaul * notice, this list of conditions and the following disclaimer in the 1280219Swpaul * documentation and/or other materials provided with the distribution. 1380219Swpaul * 3. All advertising materials mentioning features or use of this software 1480219Swpaul * must display the following acknowledgement: 1580219Swpaul * This product includes software developed by the University of 1680219Swpaul * California, Berkeley and its contributors. 1780219Swpaul * 4. Neither the name of the University nor the names of its contributors 1880219Swpaul * may be used to endorse or promote products derived from this software 1980219Swpaul * without specific prior written permission. 2080219Swpaul * 2180219Swpaul * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2280219Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2380219Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2480219Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2580219Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2680219Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2780219Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2880219Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2980219Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3080219Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3180219Swpaul * SUCH DAMAGE. 3280219Swpaul */ 3380219Swpaul 3480219Swpaul#if defined(LIBC_SCCS) && !defined(lint) 3580219Swpaulstatic char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; 3680219Swpaul#endif /* LIBC_SCCS and not lint */ 3780219Swpaul 3880219Swpaul#include <ttyent.h> 3980219Swpaul#include <stdio.h> 4080219Swpaul#include <ctype.h> 4180219Swpaul#include <string.h> 4280219Swpaul 4380219Swpaulstatic char zapchar; 4480219Swpaulstatic FILE *tf; 4580219Swpaul 4680219Swpaulstruct ttyent * 4780219Swpaulgetttynam(tty) 4880219Swpaul const char *tty; 4980219Swpaul{ 5080219Swpaul register struct ttyent *t; 5180219Swpaul 5280219Swpaul setttyent(); 5380219Swpaul while (t = getttyent()) 5480219Swpaul if (!strcmp(tty, t->ty_name)) 5583115Sbrooks break; 5680219Swpaul endttyent(); 5780219Swpaul return (t); 5880219Swpaul} 5980219Swpaul 6080219Swpaulstruct ttyent * 6180219Swpaulgetttyent() 6280219Swpaul{ 6380219Swpaul static struct ttyent tty; 6480219Swpaul register int c; 6580219Swpaul register char *p; 6680219Swpaul#define MAXLINELENGTH 100 6780219Swpaul static char line[MAXLINELENGTH]; 6880219Swpaul static char *skip(), *value(); 6980219Swpaul 7080219Swpaul if (!tf && !setttyent()) 7180219Swpaul return (NULL); 7280219Swpaul for (;;) { 7380219Swpaul if (!fgets(p = line, sizeof(line), tf)) 7480219Swpaul return (NULL); 7580219Swpaul /* skip lines that are too big */ 7680219Swpaul if (!index(p, '\n')) { 7780219Swpaul while ((c = getc(tf)) != '\n' && c != EOF) 7880219Swpaul ; 7980219Swpaul continue; 8080219Swpaul } 8180219Swpaul while (isspace(*p)) 8280219Swpaul ++p; 8380219Swpaul if (*p && *p != '#') 8480229Swpaul break; 8580219Swpaul } 8680219Swpaul 8780219Swpaul zapchar = 0; 8880219Swpaul tty.ty_name = p; 8980219Swpaul p = skip(p); 9080219Swpaul if (!*(tty.ty_getty = p)) 9180219Swpaul tty.ty_getty = tty.ty_type = NULL; 9280219Swpaul else { 9380219Swpaul p = skip(p); 9480219Swpaul if (!*(tty.ty_type = p)) 9580219Swpaul tty.ty_type = NULL; 9680219Swpaul else 9780219Swpaul p = skip(p); 9880219Swpaul } 9980219Swpaul tty.ty_status = 0; 10080219Swpaul tty.ty_window = NULL; 10180219Swpaul 10280219Swpaul#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) 10380219Swpaul#define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 10480219Swpaul for (; *p; p = skip(p)) { 10580219Swpaul if (scmp(_TTYS_OFF)) 10680219Swpaul tty.ty_status &= ~TTY_ON; 10780219Swpaul else if (scmp(_TTYS_ON)) 10880219Swpaul tty.ty_status |= TTY_ON; 10980219Swpaul else if (scmp(_TTYS_SECURE)) 11080219Swpaul tty.ty_status |= TTY_SECURE; 11180219Swpaul else if (vcmp(_TTYS_WINDOW)) 11280219Swpaul tty.ty_window = value(p); 11392739Salfred else 11492739Salfred break; 11592739Salfred } 11692739Salfred 11792739Salfred if (zapchar == '#' || *p == '#') 11892739Salfred while ((c = *++p) == ' ' || c == '\t') 11992739Salfred ; 12092739Salfred tty.ty_comment = p; 12192739Salfred if (*p == 0) 12292739Salfred tty.ty_comment = 0; 12392739Salfred if (p = index(p, '\n')) 12480219Swpaul *p = '\0'; 12592739Salfred return (&tty); 12692739Salfred} 12792739Salfred 12892739Salfred#define QUOTED 1 12992739Salfred 13092739Salfred/* 13192739Salfred * Skip over the current field, removing quotes, and return a pointer to 13292739Salfred * the next field. 13392739Salfred */ 13492739Salfredstatic char * 13592739Salfredskip(p) 13680219Swpaul register char *p; 13792739Salfred{ 13892739Salfred register char *t; 13992739Salfred register int c, q; 14092739Salfred 14180219Swpaul for (q = 0, t = p; (c = *p) != '\0'; p++) { 14292739Salfred if (c == '"') { 14392739Salfred q ^= QUOTED; /* obscure, but nice */ 14492739Salfred continue; 14592739Salfred } 14692739Salfred if (q == QUOTED && *p == '\\' && *(p+1) == '"') 14792739Salfred p++; 14880219Swpaul *t++ = *p; 14992739Salfred if (q == QUOTED) 15092739Salfred continue; 15180219Swpaul if (c == '#') { 15292739Salfred zapchar = c; 15380219Swpaul *p = 0; 15492739Salfred break; 15592739Salfred } 15692739Salfred if (c == '\t' || c == ' ' || c == '\n') { 15780219Swpaul zapchar = c; 15880219Swpaul *p++ = 0; 15980219Swpaul while ((c = *p) == '\t' || c == ' ' || c == '\n') 16080219Swpaul p++; 16180219Swpaul break; 16280219Swpaul } 16380219Swpaul } 16480219Swpaul *--t = '\0'; 16580219Swpaul return (p); 16680219Swpaul} 16780219Swpaul 16880219Swpaulstatic char * 16980219Swpaulvalue(p) 17080219Swpaul register char *p; 17180219Swpaul{ 17280219Swpaul 17380219Swpaul return ((p = index(p, '=')) ? ++p : NULL); 17480219Swpaul} 17580219Swpaul 17680219Swpaulint 17780219Swpaulsetttyent() 17880219Swpaul{ 17980219Swpaul 18080219Swpaul if (tf) { 18180219Swpaul (void)rewind(tf); 18280219Swpaul return (1); 18380219Swpaul } else if (tf = fopen(_PATH_TTYS, "r")) 18480219Swpaul return (1); 18580219Swpaul return (0); 18680219Swpaul} 18780219Swpaul 18880219Swpaulint 18980219Swpaulendttyent() 19080219Swpaul{ 19180219Swpaul int rval; 19280219Swpaul 19380219Swpaul if (tf) { 19480219Swpaul rval = !(fclose(tf) == EOF); 19580219Swpaul tf = NULL; 19680219Swpaul return (rval); 19780219Swpaul } 19880219Swpaul return (1); 19980219Swpaul} 20080219Swpaul