1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)getttyent.c 5.8 (Berkeley) 06/01/90"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <ttyent.h> 13 #include <stdio.h> 14 #include <ctype.h> 15 #include <string.h> 16 17 static char zapchar; 18 static FILE *tf; 19 20 struct ttyent * 21 getttynam(tty) 22 char *tty; 23 { 24 register struct ttyent *t; 25 26 setttyent(); 27 while (t = getttyent()) 28 if (!strcmp(tty, t->ty_name)) 29 break; 30 return(t); 31 } 32 33 struct ttyent * 34 getttyent() 35 { 36 static struct ttyent tty; 37 register int c; 38 register char *p; 39 #define MAXLINELENGTH 100 40 static char line[MAXLINELENGTH]; 41 char *skip(), *value(); 42 43 if (!tf && !setttyent()) 44 return(NULL); 45 for (;;) { 46 if (!fgets(p = line, sizeof(line), tf)) 47 return(NULL); 48 /* skip lines that are too big */ 49 if (!index(p, '\n')) { 50 while ((c = getc(tf)) != '\n' && c != EOF) 51 ; 52 continue; 53 } 54 while (isspace(*p)) 55 ++p; 56 if (*p && *p != '#') 57 break; 58 } 59 60 zapchar = 0; 61 tty.ty_name = p; 62 p = skip(p); 63 if (!*(tty.ty_getty = p)) 64 tty.ty_getty = tty.ty_type = NULL; 65 else { 66 p = skip(p); 67 if (!*(tty.ty_type = p)) 68 tty.ty_type = NULL; 69 else 70 p = skip(p); 71 } 72 tty.ty_status = 0; 73 tty.ty_window = NULL; 74 75 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) 76 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 77 for (; *p; p = skip(p)) { 78 if (scmp(_TTYS_OFF)) 79 tty.ty_status &= ~TTY_ON; 80 else if (scmp(_TTYS_ON)) 81 tty.ty_status |= TTY_ON; 82 else if (scmp(_TTYS_SECURE)) 83 tty.ty_status |= TTY_SECURE; 84 else if (vcmp(_TTYS_WINDOW)) 85 tty.ty_window = value(p); 86 else 87 break; 88 } 89 90 if (zapchar == '#' || *p == '#') 91 while ((c = *++p) == ' ' || c == '\t') 92 ; 93 tty.ty_comment = p; 94 if (*p == 0) 95 tty.ty_comment = 0; 96 if (p = index(p, '\n')) 97 *p = '\0'; 98 return(&tty); 99 } 100 101 #define QUOTED 1 102 103 /* 104 * Skip over the current field, removing quotes, and return a pointer to 105 * the next field. 106 */ 107 static char * 108 skip(p) 109 register char *p; 110 { 111 register char *t; 112 register int c, q; 113 114 for (q = 0, t = p; (c = *p) != '\0'; p++) { 115 if (c == '"') { 116 q ^= QUOTED; /* obscure, but nice */ 117 continue; 118 } 119 if (q == QUOTED && *p == '\\' && *(p+1) == '"') 120 p++; 121 *t++ = *p; 122 if (q == QUOTED) 123 continue; 124 if (c == '#') { 125 zapchar = c; 126 *p = 0; 127 break; 128 } 129 if (c == '\t' || c == ' ' || c == '\n') { 130 zapchar = c; 131 *p++ = 0; 132 while ((c = *p) == '\t' || c == ' ' || c == '\n') 133 p++; 134 break; 135 } 136 } 137 *--t = '\0'; 138 return(p); 139 } 140 141 static char * 142 value(p) 143 register char *p; 144 { 145 return((p = index(p, '=')) ? ++p : NULL); 146 } 147 148 setttyent() 149 { 150 if (tf) { 151 (void)rewind(tf); 152 return(1); 153 } else if (tf = fopen(_PATH_TTYS, "r")) 154 return(1); 155 return(0); 156 } 157 158 endttyent() 159 { 160 int rval; 161 162 if (tf) { 163 rval = !(fclose(tf) == EOF); 164 tf = NULL; 165 return(rval); 166 } 167 return(1); 168 } 169