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