getttyent.c revision 20383
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; 1131573Srgrimes 1141573Srgrimes#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) 1151573Srgrimes#define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 1161573Srgrimes for (; *p; p = skip(p)) { 1171573Srgrimes if (scmp(_TTYS_OFF)) 1181573Srgrimes tty.ty_status &= ~TTY_ON; 1191573Srgrimes else if (scmp(_TTYS_ON)) 1201573Srgrimes tty.ty_status |= TTY_ON; 1211573Srgrimes else if (scmp(_TTYS_SECURE)) 1221573Srgrimes tty.ty_status |= TTY_SECURE; 1231573Srgrimes else if (vcmp(_TTYS_WINDOW)) 1241573Srgrimes tty.ty_window = value(p); 1251573Srgrimes else 1261573Srgrimes break; 1271573Srgrimes } 1281573Srgrimes 1291573Srgrimes if (zapchar == '#' || *p == '#') 1301573Srgrimes while ((c = *++p) == ' ' || c == '\t') 1311573Srgrimes ; 1321573Srgrimes tty.ty_comment = p; 1331573Srgrimes if (*p == 0) 1341573Srgrimes tty.ty_comment = 0; 13517141Sjkh if ( (p = index(p, '\n')) ) 1361573Srgrimes *p = '\0'; 1371573Srgrimes return (&tty); 1381573Srgrimes} 1391573Srgrimes 1401573Srgrimes#define QUOTED 1 1411573Srgrimes 1421573Srgrimes/* 1431573Srgrimes * Skip over the current field, removing quotes, and return a pointer to 1441573Srgrimes * the next field. 1451573Srgrimes */ 1461573Srgrimesstatic char * 1471573Srgrimesskip(p) 1481573Srgrimes register char *p; 1491573Srgrimes{ 1501573Srgrimes register char *t; 1511573Srgrimes register int c, q; 1521573Srgrimes 1531573Srgrimes for (q = 0, t = p; (c = *p) != '\0'; p++) { 1541573Srgrimes if (c == '"') { 1551573Srgrimes q ^= QUOTED; /* obscure, but nice */ 1561573Srgrimes continue; 1571573Srgrimes } 1581573Srgrimes if (q == QUOTED && *p == '\\' && *(p+1) == '"') 1591573Srgrimes p++; 1601573Srgrimes *t++ = *p; 1611573Srgrimes if (q == QUOTED) 1621573Srgrimes continue; 1631573Srgrimes if (c == '#') { 1641573Srgrimes zapchar = c; 1651573Srgrimes *p = 0; 1661573Srgrimes break; 1671573Srgrimes } 1681573Srgrimes if (c == '\t' || c == ' ' || c == '\n') { 1691573Srgrimes zapchar = c; 1701573Srgrimes *p++ = 0; 1711573Srgrimes while ((c = *p) == '\t' || c == ' ' || c == '\n') 1721573Srgrimes p++; 1731573Srgrimes break; 1741573Srgrimes } 1751573Srgrimes } 1761573Srgrimes *--t = '\0'; 1771573Srgrimes return (p); 1781573Srgrimes} 1791573Srgrimes 1801573Srgrimesstatic char * 1811573Srgrimesvalue(p) 1821573Srgrimes register char *p; 1831573Srgrimes{ 1841573Srgrimes 1851573Srgrimes return ((p = index(p, '=')) ? ++p : NULL); 1861573Srgrimes} 1871573Srgrimes 1881573Srgrimesint 1891573Srgrimessetttyent() 1901573Srgrimes{ 1911573Srgrimes 19219031Sjoerg if (line == NULL) { 19319031Sjoerg if ((line = malloc(MALLOCCHUNK)) == NULL) 19419031Sjoerg return (0); 19519031Sjoerg lbsize = MALLOCCHUNK; 19619031Sjoerg } 1971573Srgrimes if (tf) { 1989272Shsu rewind(tf); 1991573Srgrimes return (1); 20017141Sjkh } else if ( (tf = fopen(_PATH_TTYS, "r")) ) 2011573Srgrimes return (1); 2021573Srgrimes return (0); 2031573Srgrimes} 2041573Srgrimes 2051573Srgrimesint 2061573Srgrimesendttyent() 2071573Srgrimes{ 2081573Srgrimes int rval; 2091573Srgrimes 21020383Sjoerg /* 21120383Sjoerg * NB: Don't free `line' because getttynam() 21220383Sjoerg * may still be referencing it 21320383Sjoerg */ 2141573Srgrimes if (tf) { 21519031Sjoerg rval = (fclose(tf) != EOF); 2161573Srgrimes tf = NULL; 2171573Srgrimes return (rval); 2181573Srgrimes } 2191573Srgrimes return (1); 2201573Srgrimes} 221