1 /* $OpenBSD: getttyent.c,v 1.13 2013/11/24 23:51:29 deraadt 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((unsigned char)*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) && \ 97 isspace((unsigned char)p[sizeof(e) - 1]) 98 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 99 for (; *p; p = skip(p)) { 100 if (scmp(_TTYS_OFF)) 101 tty.ty_status &= ~TTY_ON; 102 else if (scmp(_TTYS_ON)) 103 tty.ty_status |= TTY_ON; 104 else if (scmp(_TTYS_SECURE)) 105 tty.ty_status |= TTY_SECURE; 106 else if (scmp(_TTYS_LOCAL)) 107 tty.ty_status |= TTY_LOCAL; 108 else if (scmp(_TTYS_RTSCTS)) 109 tty.ty_status |= TTY_RTSCTS; 110 else if (scmp(_TTYS_SOFTCAR)) 111 tty.ty_status |= TTY_SOFTCAR; 112 else if (scmp(_TTYS_MDMBUF)) 113 tty.ty_status |= TTY_MDMBUF; 114 else if (vcmp(_TTYS_WINDOW)) 115 tty.ty_window = value(p); 116 else 117 break; 118 } 119 120 if (zapchar == '#' || *p == '#') 121 while ((c = *++p) == ' ' || c == '\t') 122 ; 123 tty.ty_comment = p; 124 if (*p == 0) 125 tty.ty_comment = 0; 126 if ((p = strchr(p, '\n'))) 127 *p = '\0'; 128 return (&tty); 129 } 130 131 #define QUOTED 1 132 133 /* 134 * Skip over the current field, removing quotes, and return a pointer to 135 * the next field. 136 */ 137 static char * 138 skip(char *p) 139 { 140 char *t; 141 int c, q; 142 143 for (q = 0, t = p; (c = *p) != '\0'; p++) { 144 if (c == '"') { 145 q ^= QUOTED; /* obscure, but nice */ 146 continue; 147 } 148 if (q == QUOTED && *p == '\\' && *(p+1) == '"') 149 p++; 150 *t++ = *p; 151 if (q == QUOTED) 152 continue; 153 if (c == '#') { 154 zapchar = c; 155 *p = 0; 156 break; 157 } 158 if (c == '\t' || c == ' ' || c == '\n') { 159 zapchar = c; 160 *p++ = 0; 161 while ((c = *p) == '\t' || c == ' ' || c == '\n') 162 p++; 163 break; 164 } 165 } 166 *--t = '\0'; 167 return (p); 168 } 169 170 static char * 171 value(char *p) 172 { 173 174 return ((p = strchr(p, '=')) ? ++p : NULL); 175 } 176 177 int 178 setttyent(void) 179 { 180 181 if (tf) { 182 rewind(tf); 183 return (1); 184 } else if ((tf = fopen(_PATH_TTYS, "r"))) 185 return (1); 186 return (0); 187 } 188 189 int 190 endttyent(void) 191 { 192 int rval; 193 194 if (tf) { 195 rval = !(fclose(tf) == EOF); 196 tf = NULL; 197 return (rval); 198 } 199 return (1); 200 } 201