1*17882Sralph /* @(#)getttyent.c 4.2 (Berkeley) 01/30/85 */ 216420Sralph 316420Sralph #include <stdio.h> 4*17882Sralph #include <strings.h> 516420Sralph #include <ttyent.h> 616420Sralph 716420Sralph static char TTYFILE[] = "/etc/ttys"; 816420Sralph static char EMPTY[] = ""; 916420Sralph static FILE *tf = NULL; 10*17882Sralph #define LINE 256 11*17882Sralph static char line[LINE]; 1216420Sralph static struct ttyent tty; 1316420Sralph 1416420Sralph setttyent() 1516420Sralph { 1616420Sralph if (tf == NULL) 1716420Sralph tf = fopen(TTYFILE, "r"); 1816420Sralph else 1916420Sralph rewind(tf); 2016420Sralph } 2116420Sralph 2216420Sralph endttyent() 2316420Sralph { 2416420Sralph if (tf != NULL) { 2516420Sralph (void) fclose(tf); 2616420Sralph tf = NULL; 2716420Sralph } 2816420Sralph } 2916420Sralph 30*17882Sralph #define QUOTED 1 31*17882Sralph 32*17882Sralph /* 33*17882Sralph * Skip over the current field and 34*17882Sralph * return a pointer to the next field. 35*17882Sralph */ 3616420Sralph static char * 3716420Sralph skip(p) 38*17882Sralph register char *p; 3916420Sralph { 4016420Sralph register int c; 41*17882Sralph register int q = 0; 4216420Sralph 43*17882Sralph for (; (c = *p) != '\0'; p++) { 44*17882Sralph if (c == '"') { 45*17882Sralph q ^= QUOTED; /* obscure, but nice */ 46*17882Sralph continue; 47*17882Sralph } 48*17882Sralph if (q == QUOTED) 49*17882Sralph continue; 5016420Sralph if (c == '#') { 5116420Sralph *p = '\0'; 5216420Sralph break; 5316420Sralph } 5416420Sralph if (c == '\t' || c == ' ' || c == '\n') { 5516420Sralph *p++ = '\0'; 5616420Sralph while ((c = *p) == '\t' || c == ' ' || c == '\n') 5716420Sralph p++; 5816420Sralph break; 5916420Sralph } 6016420Sralph } 6116420Sralph return (p); 6216420Sralph } 6316420Sralph 64*17882Sralph static char * 65*17882Sralph value(p) 66*17882Sralph register char *p; 67*17882Sralph { 68*17882Sralph if ((p = index(p,'=')) == 0) 69*17882Sralph return(NULL); 70*17882Sralph p++; /* get past the = sign */ 71*17882Sralph return(p); 72*17882Sralph } 73*17882Sralph 74*17882Sralph /* get rid of quotes. */ 75*17882Sralph 76*17882Sralph static 77*17882Sralph qremove(p) 78*17882Sralph register char *p; 79*17882Sralph { 80*17882Sralph register char *t; 81*17882Sralph 82*17882Sralph for (t = p; *p; p++) 83*17882Sralph if (*p != '"') 84*17882Sralph *t++ = *p; 85*17882Sralph *t = '\0'; 86*17882Sralph } 87*17882Sralph 8816420Sralph struct ttyent * 8916420Sralph getttyent() 9016420Sralph { 91*17882Sralph register char *p; 9216420Sralph register int c; 9316420Sralph 9416420Sralph if (tf == NULL) { 9516420Sralph if ((tf = fopen(TTYFILE, "r")) == NULL) 9616420Sralph return (NULL); 9716420Sralph } 9816420Sralph do { 99*17882Sralph p = fgets(line, LINE, tf); 10016420Sralph if (p == NULL) 10116420Sralph return (NULL); 10216420Sralph while ((c = *p) == '\t' || c == ' ' || c == '\n') 10316420Sralph p++; 10416420Sralph } while (c == '\0' || c == '#'); 10516420Sralph tty.ty_name = p; 10616420Sralph p = skip(p); 10716420Sralph tty.ty_getty = p; 10816420Sralph p = skip(p); 10916420Sralph tty.ty_type = p; 110*17882Sralph p = skip(p); 11116420Sralph tty.ty_status = 0; 112*17882Sralph tty.ty_window = EMPTY; 113*17882Sralph for (; *p; p = skip(p)) { 114*17882Sralph if (strncmp(p, "on", 2) == 0) 11516420Sralph tty.ty_status |= TTY_ON; 116*17882Sralph else if (strncmp(p, "off", 3) == 0) 11716420Sralph tty.ty_status &= ~TTY_ON; 118*17882Sralph else if (strncmp(p, "secure", 6) == 0) 11916420Sralph tty.ty_status |= TTY_SECURE; 120*17882Sralph else if (strncmp(p, "window", 6) == 0) { 121*17882Sralph if ((tty.ty_window = value(p)) == NULL) 122*17882Sralph tty.ty_window = EMPTY; 123*17882Sralph } else 124*17882Sralph break; 12516420Sralph } 126*17882Sralph tty.ty_comment = p; 127*17882Sralph if (p = index(p, '\n')) 128*17882Sralph *p = '\0'; 129*17882Sralph qremove(tty.ty_getty); 130*17882Sralph qremove(tty.ty_window); 131*17882Sralph qremove(tty.ty_comment); 132*17882Sralph return(&tty); 13316420Sralph } 134