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