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