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 * 4. 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 sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; 32 #endif /* LIBC_SCCS and not lint */ 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <ttyent.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <ctype.h> 40 #include <string.h> 41 42 static char zapchar; 43 static FILE *tf; 44 static size_t lbsize; 45 static char *line; 46 47 #define MALLOCCHUNK 100 48 49 static char *skip(char *); 50 static char *value(char *); 51 52 struct ttyent * 53 getttynam(const char *tty) 54 { 55 struct ttyent *t; 56 57 if (strncmp(tty, "/dev/", 5) == 0) 58 tty += 5; 59 setttyent(); 60 while ( (t = getttyent()) ) 61 if (!strcmp(tty, t->ty_name)) 62 break; 63 endttyent(); 64 return (t); 65 } 66 67 struct ttyent * 68 getttyent(void) 69 { 70 static struct ttyent tty; 71 char *p; 72 int c; 73 size_t i; 74 75 if (!tf && !setttyent()) 76 return (NULL); 77 for (;;) { 78 if (!fgets(p = line, lbsize, tf)) 79 return (NULL); 80 /* extend buffer if line was too big, and retry */ 81 while (!strchr(p, '\n') && !feof(tf)) { 82 i = strlen(p); 83 lbsize += MALLOCCHUNK; 84 if ((p = realloc(line, lbsize)) == NULL) { 85 (void)endttyent(); 86 return (NULL); 87 } 88 line = p; 89 if (!fgets(&line[i], lbsize - i, tf)) 90 return (NULL); 91 } 92 while (isspace((unsigned char)*p)) 93 ++p; 94 if (*p && *p != '#') 95 break; 96 } 97 98 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1]) 99 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 100 101 zapchar = 0; 102 tty.ty_name = p; 103 tty.ty_status = 0; 104 tty.ty_window = NULL; 105 tty.ty_group = _TTYS_NOGROUP; 106 107 p = skip(p); 108 if (!*(tty.ty_getty = p)) 109 tty.ty_getty = tty.ty_type = NULL; 110 else { 111 p = skip(p); 112 if (!*(tty.ty_type = p)) 113 tty.ty_type = NULL; 114 else { 115 /* compatibility kludge: handle network/dialup specially */ 116 if (scmp(_TTYS_DIALUP)) 117 tty.ty_status |= TTY_DIALUP; 118 else if (scmp(_TTYS_NETWORK)) 119 tty.ty_status |= TTY_NETWORK; 120 p = skip(p); 121 } 122 } 123 124 for (; *p; p = skip(p)) { 125 if (scmp(_TTYS_OFF)) 126 tty.ty_status &= ~TTY_ON; 127 else if (scmp(_TTYS_ON)) 128 tty.ty_status |= TTY_ON; 129 else if (scmp(_TTYS_SECURE)) 130 tty.ty_status |= TTY_SECURE; 131 else if (scmp(_TTYS_INSECURE)) 132 tty.ty_status &= ~TTY_SECURE; 133 else if (scmp(_TTYS_DIALUP)) 134 tty.ty_status |= TTY_DIALUP; 135 else if (scmp(_TTYS_NETWORK)) 136 tty.ty_status |= TTY_NETWORK; 137 else if (vcmp(_TTYS_WINDOW)) 138 tty.ty_window = value(p); 139 else if (vcmp(_TTYS_GROUP)) 140 tty.ty_group = value(p); 141 else 142 break; 143 } 144 145 if (zapchar == '#' || *p == '#') 146 while ((c = *++p) == ' ' || c == '\t') 147 ; 148 tty.ty_comment = p; 149 if (*p == 0) 150 tty.ty_comment = 0; 151 if ((p = strchr(p, '\n'))) 152 *p = '\0'; 153 return (&tty); 154 } 155 156 #define QUOTED 1 157 158 /* 159 * Skip over the current field, removing quotes, and return a pointer to 160 * the next field. 161 */ 162 static char * 163 skip(char *p) 164 { 165 char *t; 166 int c, q; 167 168 for (q = 0, t = p; (c = *p) != '\0'; p++) { 169 if (c == '"') { 170 q ^= QUOTED; /* obscure, but nice */ 171 continue; 172 } 173 if (q == QUOTED && *p == '\\' && *(p+1) == '"') 174 p++; 175 *t++ = *p; 176 if (q == QUOTED) 177 continue; 178 if (c == '#') { 179 zapchar = c; 180 *p = 0; 181 break; 182 } 183 if (c == '\t' || c == ' ' || c == '\n') { 184 zapchar = c; 185 *p++ = 0; 186 while ((c = *p) == '\t' || c == ' ' || c == '\n') 187 p++; 188 break; 189 } 190 } 191 *--t = '\0'; 192 return (p); 193 } 194 195 static char * 196 value(char *p) 197 { 198 199 return ((p = strchr(p, '=')) ? ++p : NULL); 200 } 201 202 int 203 setttyent(void) 204 { 205 206 if (line == NULL) { 207 if ((line = malloc(MALLOCCHUNK)) == NULL) 208 return (0); 209 lbsize = MALLOCCHUNK; 210 } 211 if (tf) { 212 rewind(tf); 213 return (1); 214 } else if ( (tf = fopen(_PATH_TTYS, "re")) ) 215 return (1); 216 return (0); 217 } 218 219 int 220 endttyent(void) 221 { 222 int rval; 223 224 /* 225 * NB: Don't free `line' because getttynam() 226 * may still be referencing it 227 */ 228 if (tf) { 229 rval = (fclose(tf) != EOF); 230 tf = NULL; 231 return (rval); 232 } 233 return (1); 234 } 235 236 static int 237 isttystat(const char *tty, int flag) 238 { 239 struct ttyent *t; 240 241 return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag); 242 } 243 244 245 int 246 isdialuptty(const char *tty) 247 { 248 249 return isttystat(tty, TTY_DIALUP); 250 } 251 252 int 253 isnettty(const char *tty) 254 { 255 256 return isttystat(tty, TTY_NETWORK); 257 } 258