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