getttyent.c revision 1.3
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#if defined(LIBC_SCCS) && !defined(lint) 35static char rcsid[] = "$OpenBSD: getttyent.c,v 1.3 1996/09/15 09:31:03 tholo Exp $"; 36#endif /* LIBC_SCCS and not lint */ 37 38#include <ttyent.h> 39#include <stdio.h> 40#include <ctype.h> 41#include <string.h> 42 43static char zapchar; 44static FILE *tf; 45 46static char *skip __P((char *)); 47static char *value __P((char *)); 48 49struct ttyent * 50getttynam(tty) 51 const char *tty; 52{ 53 register struct ttyent *t; 54 55 setttyent(); 56 while (t = getttyent()) 57 if (!strcmp(tty, t->ty_name)) 58 break; 59 endttyent(); 60 return (t); 61} 62 63struct ttyent * 64getttyent() 65{ 66 static struct ttyent tty; 67 register int c; 68 register char *p; 69#define MAXLINELENGTH 200 70 static char line[MAXLINELENGTH]; 71 72 if (!tf && !setttyent()) 73 return (NULL); 74 for (;;) { 75 if (!fgets(p = line, sizeof(line), tf)) 76 return (NULL); 77 /* skip lines that are too big */ 78 if (!strchr(p, '\n')) { 79 while ((c = getc(tf)) != '\n' && c != EOF) 80 ; 81 continue; 82 } 83 while (isspace(*p)) 84 ++p; 85 if (*p && *p != '#') 86 break; 87 } 88 89 zapchar = 0; 90 tty.ty_name = p; 91 p = skip(p); 92 if (!*(tty.ty_getty = p)) 93 tty.ty_getty = tty.ty_type = NULL; 94 else { 95 p = skip(p); 96 if (!*(tty.ty_type = p)) 97 tty.ty_type = NULL; 98 else 99 p = skip(p); 100 } 101 tty.ty_status = 0; 102 tty.ty_window = NULL; 103 104#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) 105#define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 106 for (; *p; p = skip(p)) { 107 if (scmp(_TTYS_OFF)) 108 tty.ty_status &= ~TTY_ON; 109 else if (scmp(_TTYS_ON)) 110 tty.ty_status |= TTY_ON; 111 else if (scmp(_TTYS_SECURE)) 112 tty.ty_status |= TTY_SECURE; 113 else if (scmp(_TTYS_LOCAL)) 114 tty.ty_status |= TTY_LOCAL; 115 else if (scmp(_TTYS_RTSCTS)) 116 tty.ty_status |= TTY_RTSCTS; 117 else if (scmp(_TTYS_SOFTCAR)) 118 tty.ty_status |= TTY_SOFTCAR; 119 else if (scmp(_TTYS_MDMBUF)) 120 tty.ty_status |= TTY_MDMBUF; 121 else if (vcmp(_TTYS_WINDOW)) 122 tty.ty_window = value(p); 123 else 124 break; 125 } 126 127 if (zapchar == '#' || *p == '#') 128 while ((c = *++p) == ' ' || c == '\t') 129 ; 130 tty.ty_comment = p; 131 if (*p == 0) 132 tty.ty_comment = 0; 133 if (p = strchr(p, '\n')) 134 *p = '\0'; 135 return (&tty); 136} 137 138#define QUOTED 1 139 140/* 141 * Skip over the current field, removing quotes, and return a pointer to 142 * the next field. 143 */ 144static char * 145skip(p) 146 register char *p; 147{ 148 register char *t; 149 register int c, q; 150 151 for (q = 0, t = p; (c = *p) != '\0'; p++) { 152 if (c == '"') { 153 q ^= QUOTED; /* obscure, but nice */ 154 continue; 155 } 156 if (q == QUOTED && *p == '\\' && *(p+1) == '"') 157 p++; 158 *t++ = *p; 159 if (q == QUOTED) 160 continue; 161 if (c == '#') { 162 zapchar = c; 163 *p = 0; 164 break; 165 } 166 if (c == '\t' || c == ' ' || c == '\n') { 167 zapchar = c; 168 *p++ = 0; 169 while ((c = *p) == '\t' || c == ' ' || c == '\n') 170 p++; 171 break; 172 } 173 } 174 *--t = '\0'; 175 return (p); 176} 177 178static char * 179value(p) 180 register char *p; 181{ 182 183 return ((p = strchr(p, '=')) ? ++p : NULL); 184} 185 186int 187setttyent() 188{ 189 190 if (tf) { 191 rewind(tf); 192 return (1); 193 } else if (tf = fopen(_PATH_TTYS, "r")) 194 return (1); 195 return (0); 196} 197 198int 199endttyent() 200{ 201 int rval; 202 203 if (tf) { 204 rval = !(fclose(tf) == EOF); 205 tf = NULL; 206 return (rval); 207 } 208 return (1); 209} 210