1 /* $NetBSD: getttyent.c,v 1.9 1995/06/16 07:05:31 jtc 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 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; 39 #else 40 static char rcsid[] = "$NetBSD: getttyent.c,v 1.9 1995/06/16 07:05:31 jtc Exp $"; 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 #include <ttyent.h> 45 #include <stdio.h> 46 #include <ctype.h> 47 #include <string.h> 48 49 static char zapchar; 50 static FILE *tf; 51 52 struct ttyent * 53 getttynam(tty) 54 const char *tty; 55 { 56 register struct ttyent *t; 57 58 setttyent(); 59 while (t = getttyent()) 60 if (!strcmp(tty, t->ty_name)) 61 break; 62 endttyent(); 63 return (t); 64 } 65 66 struct ttyent * 67 getttyent() 68 { 69 static struct ttyent tty; 70 register int c; 71 register char *p; 72 #define MAXLINELENGTH 200 73 static char line[MAXLINELENGTH]; 74 static char *skip(), *value(); 75 76 if (!tf && !setttyent()) 77 return (NULL); 78 for (;;) { 79 if (!fgets(p = line, sizeof(line), tf)) 80 return (NULL); 81 /* skip lines that are too big */ 82 if (!strchr(p, '\n')) { 83 while ((c = getc(tf)) != '\n' && c != EOF) 84 ; 85 continue; 86 } 87 while (isspace(*p)) 88 ++p; 89 if (*p && *p != '#') 90 break; 91 } 92 93 zapchar = 0; 94 tty.ty_name = p; 95 p = skip(p); 96 if (!*(tty.ty_getty = p)) 97 tty.ty_getty = tty.ty_type = NULL; 98 else { 99 p = skip(p); 100 if (!*(tty.ty_type = p)) 101 tty.ty_type = NULL; 102 else 103 p = skip(p); 104 } 105 tty.ty_status = 0; 106 tty.ty_window = NULL; 107 108 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) 109 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 110 for (; *p; p = skip(p)) { 111 if (scmp(_TTYS_OFF)) 112 tty.ty_status &= ~TTY_ON; 113 else if (scmp(_TTYS_ON)) 114 tty.ty_status |= TTY_ON; 115 else if (scmp(_TTYS_SECURE)) 116 tty.ty_status |= TTY_SECURE; 117 else if (scmp(_TTYS_LOCAL)) 118 tty.ty_status |= TTY_LOCAL; 119 else if (scmp(_TTYS_RTSCTS)) 120 tty.ty_status |= TTY_RTSCTS; 121 else if (scmp(_TTYS_SOFTCAR)) 122 tty.ty_status |= TTY_SOFTCAR; 123 else if (scmp(_TTYS_MDMBUF)) 124 tty.ty_status |= TTY_MDMBUF; 125 else if (vcmp(_TTYS_WINDOW)) 126 tty.ty_window = value(p); 127 else 128 break; 129 } 130 131 if (zapchar == '#' || *p == '#') 132 while ((c = *++p) == ' ' || c == '\t') 133 ; 134 tty.ty_comment = p; 135 if (*p == 0) 136 tty.ty_comment = 0; 137 if (p = strchr(p, '\n')) 138 *p = '\0'; 139 return (&tty); 140 } 141 142 #define QUOTED 1 143 144 /* 145 * Skip over the current field, removing quotes, and return a pointer to 146 * the next field. 147 */ 148 static char * 149 skip(p) 150 register char *p; 151 { 152 register char *t; 153 register int c, q; 154 155 for (q = 0, t = p; (c = *p) != '\0'; p++) { 156 if (c == '"') { 157 q ^= QUOTED; /* obscure, but nice */ 158 continue; 159 } 160 if (q == QUOTED && *p == '\\' && *(p+1) == '"') 161 p++; 162 *t++ = *p; 163 if (q == QUOTED) 164 continue; 165 if (c == '#') { 166 zapchar = c; 167 *p = 0; 168 break; 169 } 170 if (c == '\t' || c == ' ' || c == '\n') { 171 zapchar = c; 172 *p++ = 0; 173 while ((c = *p) == '\t' || c == ' ' || c == '\n') 174 p++; 175 break; 176 } 177 } 178 *--t = '\0'; 179 return (p); 180 } 181 182 static char * 183 value(p) 184 register char *p; 185 { 186 187 return ((p = strchr(p, '=')) ? ++p : NULL); 188 } 189 190 int 191 setttyent() 192 { 193 194 if (tf) { 195 rewind(tf); 196 return (1); 197 } else if (tf = fopen(_PATH_TTYS, "r")) 198 return (1); 199 return (0); 200 } 201 202 int 203 endttyent() 204 { 205 int rval; 206 207 if (tf) { 208 rval = !(fclose(tf) == EOF); 209 tf = NULL; 210 return (rval); 211 } 212 return (1); 213 } 214