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