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