getttyent.c revision 21189
11573Srgrimes/* 21573Srgrimes * Copyright (c) 1989, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 3. All advertising materials mentioning features or use of this software 141573Srgrimes * must display the following acknowledgement: 151573Srgrimes * This product includes software developed by the University of 161573Srgrimes * California, Berkeley and its contributors. 171573Srgrimes * 4. Neither the name of the University nor the names of its contributors 181573Srgrimes * may be used to endorse or promote products derived from this software 191573Srgrimes * without specific prior written permission. 201573Srgrimes * 211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311573Srgrimes * SUCH DAMAGE. 321573Srgrimes */ 331573Srgrimes 341573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 351573Srgrimesstatic char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; 361573Srgrimes#endif /* LIBC_SCCS and not lint */ 371573Srgrimes 381573Srgrimes#include <ttyent.h> 391573Srgrimes#include <stdio.h> 4019031Sjoerg#include <stdlib.h> 411573Srgrimes#include <ctype.h> 421573Srgrimes#include <string.h> 431573Srgrimes 441573Srgrimesstatic char zapchar; 451573Srgrimesstatic FILE *tf; 4619031Sjoergstatic size_t lbsize; 4719031Sjoergstatic char *line; 481573Srgrimes 4919031Sjoerg#define MALLOCCHUNK 100 5019031Sjoerg 5119031Sjoergstatic char *skip __P((char *)); 5219031Sjoergstatic char *value __P((char *)); 5319031Sjoerg 541573Srgrimesstruct ttyent * 551573Srgrimesgetttynam(tty) 561573Srgrimes const char *tty; 571573Srgrimes{ 581573Srgrimes register struct ttyent *t; 591573Srgrimes 601573Srgrimes setttyent(); 6117141Sjkh while ( (t = getttyent()) ) 621573Srgrimes if (!strcmp(tty, t->ty_name)) 631573Srgrimes break; 641573Srgrimes endttyent(); 651573Srgrimes return (t); 661573Srgrimes} 671573Srgrimes 681573Srgrimesstruct ttyent * 691573Srgrimesgetttyent() 701573Srgrimes{ 711573Srgrimes static struct ttyent tty; 7219031Sjoerg register char *p; 731573Srgrimes register int c; 7419031Sjoerg size_t i; 751573Srgrimes 761573Srgrimes if (!tf && !setttyent()) 771573Srgrimes return (NULL); 781573Srgrimes for (;;) { 7919031Sjoerg if (!fgets(p = line, lbsize, tf)) 801573Srgrimes return (NULL); 8119031Sjoerg /* extend buffer if line was too big, and retry */ 8219031Sjoerg while (!index(p, '\n')) { 8319031Sjoerg i = strlen(p); 8419031Sjoerg lbsize += MALLOCCHUNK; 8519082Sjoerg if ((p = realloc(line, lbsize)) == NULL) { 8619031Sjoerg (void)endttyent(); 8719031Sjoerg return (NULL); 8819031Sjoerg } 8919082Sjoerg line = p; 9019031Sjoerg if (!fgets(&line[i], lbsize - i, tf)) 9119031Sjoerg return (NULL); 921573Srgrimes } 931573Srgrimes while (isspace(*p)) 941573Srgrimes ++p; 951573Srgrimes if (*p && *p != '#') 961573Srgrimes break; 971573Srgrimes } 981573Srgrimes 991573Srgrimes zapchar = 0; 1001573Srgrimes tty.ty_name = p; 1011573Srgrimes p = skip(p); 1021573Srgrimes if (!*(tty.ty_getty = p)) 1031573Srgrimes tty.ty_getty = tty.ty_type = NULL; 1041573Srgrimes else { 1051573Srgrimes p = skip(p); 1061573Srgrimes if (!*(tty.ty_type = p)) 1071573Srgrimes tty.ty_type = NULL; 1081573Srgrimes else 1091573Srgrimes p = skip(p); 1101573Srgrimes } 1111573Srgrimes tty.ty_status = 0; 1121573Srgrimes tty.ty_window = NULL; 11321189Sdavidn tty.ty_group = _TTYS_NOGROUP; 1141573Srgrimes 1151573Srgrimes#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) 1161573Srgrimes#define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 1171573Srgrimes for (; *p; p = skip(p)) { 1181573Srgrimes if (scmp(_TTYS_OFF)) 1191573Srgrimes tty.ty_status &= ~TTY_ON; 1201573Srgrimes else if (scmp(_TTYS_ON)) 1211573Srgrimes tty.ty_status |= TTY_ON; 1221573Srgrimes else if (scmp(_TTYS_SECURE)) 1231573Srgrimes tty.ty_status |= TTY_SECURE; 1241573Srgrimes else if (vcmp(_TTYS_WINDOW)) 1251573Srgrimes tty.ty_window = value(p); 12621189Sdavidn else if (vcmp(_TTYS_GROUP)) 12721189Sdavidn tty.ty_group = value(p); 1281573Srgrimes else 1291573Srgrimes break; 1301573Srgrimes } 1311573Srgrimes 1321573Srgrimes if (zapchar == '#' || *p == '#') 1331573Srgrimes while ((c = *++p) == ' ' || c == '\t') 1341573Srgrimes ; 1351573Srgrimes tty.ty_comment = p; 1361573Srgrimes if (*p == 0) 1371573Srgrimes tty.ty_comment = 0; 13817141Sjkh if ( (p = index(p, '\n')) ) 1391573Srgrimes *p = '\0'; 1401573Srgrimes return (&tty); 1411573Srgrimes} 1421573Srgrimes 1431573Srgrimes#define QUOTED 1 1441573Srgrimes 1451573Srgrimes/* 1461573Srgrimes * Skip over the current field, removing quotes, and return a pointer to 1471573Srgrimes * the next field. 1481573Srgrimes */ 1491573Srgrimesstatic char * 1501573Srgrimesskip(p) 1511573Srgrimes register char *p; 1521573Srgrimes{ 1531573Srgrimes register char *t; 1541573Srgrimes register int c, q; 1551573Srgrimes 1561573Srgrimes for (q = 0, t = p; (c = *p) != '\0'; p++) { 1571573Srgrimes if (c == '"') { 1581573Srgrimes q ^= QUOTED; /* obscure, but nice */ 1591573Srgrimes continue; 1601573Srgrimes } 1611573Srgrimes if (q == QUOTED && *p == '\\' && *(p+1) == '"') 1621573Srgrimes p++; 1631573Srgrimes *t++ = *p; 1641573Srgrimes if (q == QUOTED) 1651573Srgrimes continue; 1661573Srgrimes if (c == '#') { 1671573Srgrimes zapchar = c; 1681573Srgrimes *p = 0; 1691573Srgrimes break; 1701573Srgrimes } 1711573Srgrimes if (c == '\t' || c == ' ' || c == '\n') { 1721573Srgrimes zapchar = c; 1731573Srgrimes *p++ = 0; 1741573Srgrimes while ((c = *p) == '\t' || c == ' ' || c == '\n') 1751573Srgrimes p++; 1761573Srgrimes break; 1771573Srgrimes } 1781573Srgrimes } 1791573Srgrimes *--t = '\0'; 1801573Srgrimes return (p); 1811573Srgrimes} 1821573Srgrimes 1831573Srgrimesstatic char * 1841573Srgrimesvalue(p) 1851573Srgrimes register char *p; 1861573Srgrimes{ 1871573Srgrimes 1881573Srgrimes return ((p = index(p, '=')) ? ++p : NULL); 1891573Srgrimes} 1901573Srgrimes 1911573Srgrimesint 1921573Srgrimessetttyent() 1931573Srgrimes{ 1941573Srgrimes 19519031Sjoerg if (line == NULL) { 19619031Sjoerg if ((line = malloc(MALLOCCHUNK)) == NULL) 19719031Sjoerg return (0); 19819031Sjoerg lbsize = MALLOCCHUNK; 19919031Sjoerg } 2001573Srgrimes if (tf) { 2019272Shsu rewind(tf); 2021573Srgrimes return (1); 20317141Sjkh } else if ( (tf = fopen(_PATH_TTYS, "r")) ) 2041573Srgrimes return (1); 2051573Srgrimes return (0); 2061573Srgrimes} 2071573Srgrimes 2081573Srgrimesint 2091573Srgrimesendttyent() 2101573Srgrimes{ 2111573Srgrimes int rval; 2121573Srgrimes 21320383Sjoerg /* 21420383Sjoerg * NB: Don't free `line' because getttynam() 21520383Sjoerg * may still be referencing it 21620383Sjoerg */ 2171573Srgrimes if (tf) { 21819031Sjoerg rval = (fclose(tf) != EOF); 2191573Srgrimes tf = NULL; 2201573Srgrimes return (rval); 2211573Srgrimes } 2221573Srgrimes return (1); 2231573Srgrimes} 224