1 #pragma ident "%Z%%M% %I% %E% SMI" 2 /* from UCB 5.4 5/19/86 */ 3 /* 4 * Copyright (c) 1985 Regents of the University of California. 5 * All rights reserved. The Berkeley software License Agreement 6 * specifies the terms and conditions for redistribution. 7 */ 8 9 #include <stdio.h> 10 #include <strings.h> 11 #include <ttyent.h> 12 13 static char *TTYFILE = "/etc/ttytab"; 14 #define LINE 256 15 static struct _ttyentjunk { 16 char zapchar; 17 FILE *tf; 18 char line[LINE]; 19 struct ttyent tty; 20 } *__ttyentjunk, *_ttyentjunk(); 21 22 static struct _ttyentjunk * 23 _ttyentjunk() 24 { 25 26 if (__ttyentjunk == 0) 27 __ttyentjunk = (struct _ttyentjunk *)calloc(1, sizeof (struct _ttyentjunk)); 28 return (__ttyentjunk); 29 } 30 31 setttyent() 32 { 33 register struct _ttyentjunk *t = _ttyentjunk(); 34 35 if (t == 0) 36 return; 37 if (t->tf == NULL) 38 t->tf = fopen(TTYFILE, "r"); 39 else 40 rewind(t->tf); 41 } 42 43 endttyent() 44 { 45 register struct _ttyentjunk *t = _ttyentjunk(); 46 47 if (t == 0) 48 return; 49 if (t->tf != NULL) { 50 (void) fclose(t->tf); 51 t->tf = NULL; 52 } 53 } 54 55 #define QUOTED 1 56 57 /* 58 * Skip over the current field, removing quotes, 59 * and return a pointer to the next field. 60 */ 61 static char * 62 skip(p) 63 register char *p; 64 { 65 register struct _ttyentjunk *t = _ttyentjunk(); 66 register char *cp = p; 67 register int c; 68 register int q = 0; 69 70 if (t == 0) 71 return (0); 72 for (; (c = *p) != '\0'; p++) { 73 if (c == '"') { 74 q ^= QUOTED; /* obscure, but nice */ 75 continue; 76 } 77 if (q == QUOTED && *p == '\\' && *(p+1) == '"') 78 p++; 79 *cp++ = *p; 80 if (q == QUOTED) 81 continue; 82 if (c == '#') { 83 t->zapchar = c; 84 *p = 0; 85 break; 86 } 87 if (c == '\t' || c == ' ' || c == '\n') { 88 t->zapchar = c; 89 *p++ = 0; 90 while ((c = *p) == '\t' || c == ' ' || c == '\n') 91 p++; 92 break; 93 } 94 } 95 *--cp = '\0'; 96 return (p); 97 } 98 99 static char * 100 value(p) 101 register char *p; 102 { 103 if ((p = index(p,'=')) == 0) 104 return(NULL); 105 p++; /* get past the = sign */ 106 return(p); 107 } 108 109 struct ttyent * 110 getttyent() 111 { 112 register struct _ttyentjunk *t = _ttyentjunk(); 113 register char *p; 114 register int c; 115 116 if (t == 0) 117 return (NULL); 118 if (t->tf == NULL) { 119 if ((t->tf = fopen(TTYFILE, "r")) == NULL) 120 return (NULL); 121 } 122 do { 123 p = fgets(t->line, LINE, t->tf); 124 if (p == NULL) 125 return (NULL); 126 while ((c = *p) == '\t' || c == ' ' || c == '\n') 127 p++; 128 } while (c == '\0' || c == '#'); 129 t->zapchar = 0; 130 t->tty.ty_name = p; 131 p = skip(p); 132 t->tty.ty_getty = p; 133 p = skip(p); 134 t->tty.ty_type = p; 135 p = skip(p); 136 t->tty.ty_status = 0; 137 t->tty.ty_window = NULL; 138 for (; *p; p = skip(p)) { 139 #define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n') 140 if (strncmp(p, "on", 2) == 0 && space(2)) 141 t->tty.ty_status |= TTY_ON; 142 else if (strncmp(p, "off", 3) == 0 && space(3)) 143 t->tty.ty_status &= ~TTY_ON; 144 else if (strncmp(p, "secure", 6) == 0 && space(6)) 145 t->tty.ty_status |= TTY_SECURE; 146 else if (strncmp(p, "local", 5) == 0 && space(5)) 147 t->tty.ty_status |= TTY_LOCAL; 148 else if (strncmp(p, "window=", 7) == 0) 149 t->tty.ty_window = value(p); 150 else 151 break; 152 } 153 if (t->zapchar == '#' || *p == '#') 154 while ((c = *++p) == ' ' || c == '\t') 155 ; 156 t->tty.ty_comment = p; 157 if (*p == 0) 158 t->tty.ty_comment = 0; 159 if (p = index(p, '\n')) 160 *p = '\0'; 161 return(&t->tty); 162 } 163