1 /* @(#)getttyent.c 4.2 (Berkeley) 01/30/85 */ 2 3 #include <stdio.h> 4 #include <strings.h> 5 #include <ttyent.h> 6 7 static char TTYFILE[] = "/etc/ttys"; 8 static char EMPTY[] = ""; 9 static FILE *tf = NULL; 10 #define LINE 256 11 static char line[LINE]; 12 static struct ttyent tty; 13 14 setttyent() 15 { 16 if (tf == NULL) 17 tf = fopen(TTYFILE, "r"); 18 else 19 rewind(tf); 20 } 21 22 endttyent() 23 { 24 if (tf != NULL) { 25 (void) fclose(tf); 26 tf = NULL; 27 } 28 } 29 30 #define QUOTED 1 31 32 /* 33 * Skip over the current field and 34 * return a pointer to the next field. 35 */ 36 static char * 37 skip(p) 38 register char *p; 39 { 40 register int c; 41 register int q = 0; 42 43 for (; (c = *p) != '\0'; p++) { 44 if (c == '"') { 45 q ^= QUOTED; /* obscure, but nice */ 46 continue; 47 } 48 if (q == QUOTED) 49 continue; 50 if (c == '#') { 51 *p = '\0'; 52 break; 53 } 54 if (c == '\t' || c == ' ' || c == '\n') { 55 *p++ = '\0'; 56 while ((c = *p) == '\t' || c == ' ' || c == '\n') 57 p++; 58 break; 59 } 60 } 61 return (p); 62 } 63 64 static char * 65 value(p) 66 register char *p; 67 { 68 if ((p = index(p,'=')) == 0) 69 return(NULL); 70 p++; /* get past the = sign */ 71 return(p); 72 } 73 74 /* get rid of quotes. */ 75 76 static 77 qremove(p) 78 register char *p; 79 { 80 register char *t; 81 82 for (t = p; *p; p++) 83 if (*p != '"') 84 *t++ = *p; 85 *t = '\0'; 86 } 87 88 struct ttyent * 89 getttyent() 90 { 91 register char *p; 92 register int c; 93 94 if (tf == NULL) { 95 if ((tf = fopen(TTYFILE, "r")) == NULL) 96 return (NULL); 97 } 98 do { 99 p = fgets(line, LINE, tf); 100 if (p == NULL) 101 return (NULL); 102 while ((c = *p) == '\t' || c == ' ' || c == '\n') 103 p++; 104 } while (c == '\0' || c == '#'); 105 tty.ty_name = p; 106 p = skip(p); 107 tty.ty_getty = p; 108 p = skip(p); 109 tty.ty_type = p; 110 p = skip(p); 111 tty.ty_status = 0; 112 tty.ty_window = EMPTY; 113 for (; *p; p = skip(p)) { 114 if (strncmp(p, "on", 2) == 0) 115 tty.ty_status |= TTY_ON; 116 else if (strncmp(p, "off", 3) == 0) 117 tty.ty_status &= ~TTY_ON; 118 else if (strncmp(p, "secure", 6) == 0) 119 tty.ty_status |= TTY_SECURE; 120 else if (strncmp(p, "window", 6) == 0) { 121 if ((tty.ty_window = value(p)) == NULL) 122 tty.ty_window = EMPTY; 123 } else 124 break; 125 } 126 tty.ty_comment = p; 127 if (p = index(p, '\n')) 128 *p = '\0'; 129 qremove(tty.ty_getty); 130 qremove(tty.ty_window); 131 qremove(tty.ty_comment); 132 return(&tty); 133 } 134