|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1989 The Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that: (1) source distributions retain this entire copyright ! 7: * notice and comment, and (2) distributions including binaries display ! 8: * the following acknowledgement: ``This product includes software ! 9: * developed by the University of California, Berkeley and its contributors'' ! 10: * in the documentation or other materials provided with the distribution ! 11: * and in all advertising materials mentioning features or use of this ! 12: * software. Neither the name of the University nor the names of its ! 13: * contributors may be used to endorse or promote products derived ! 14: * from this software without specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #if defined(LIBC_SCCS) && !defined(lint) ! 21: static char sccsid[] = "@(#)getttyent.c 5.8 (Berkeley) 6/1/90"; ! 22: #endif /* LIBC_SCCS and not lint */ ! 23: ! 24: #include <ttyent.h> ! 25: #include <stdio.h> ! 26: #include <ctype.h> ! 27: #include <string.h> ! 28: ! 29: static char zapchar; ! 30: static FILE *tf; ! 31: ! 32: struct ttyent * ! 33: getttynam(tty) ! 34: char *tty; ! 35: { ! 36: register struct ttyent *t; ! 37: ! 38: setttyent(); ! 39: while (t = getttyent()) ! 40: if (!strcmp(tty, t->ty_name)) ! 41: break; ! 42: return(t); ! 43: } ! 44: ! 45: struct ttyent * ! 46: getttyent() ! 47: { ! 48: static struct ttyent tty; ! 49: register int c; ! 50: register char *p; ! 51: #define MAXLINELENGTH 100 ! 52: static char line[MAXLINELENGTH]; ! 53: char *skip(), *value(); ! 54: ! 55: if (!tf && !setttyent()) ! 56: return(NULL); ! 57: for (;;) { ! 58: if (!fgets(p = line, sizeof(line), tf)) ! 59: return(NULL); ! 60: /* skip lines that are too big */ ! 61: if (!index(p, '\n')) { ! 62: while ((c = getc(tf)) != '\n' && c != EOF) ! 63: ; ! 64: continue; ! 65: } ! 66: while (isspace(*p)) ! 67: ++p; ! 68: if (*p && *p != '#') ! 69: break; ! 70: } ! 71: ! 72: zapchar = 0; ! 73: tty.ty_name = p; ! 74: p = skip(p); ! 75: if (!*(tty.ty_getty = p)) ! 76: tty.ty_getty = tty.ty_type = NULL; ! 77: else { ! 78: p = skip(p); ! 79: if (!*(tty.ty_type = p)) ! 80: tty.ty_type = NULL; ! 81: else ! 82: p = skip(p); ! 83: } ! 84: tty.ty_status = 0; ! 85: tty.ty_window = NULL; ! 86: ! 87: #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) ! 88: #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' ! 89: for (; *p; p = skip(p)) { ! 90: if (scmp(_TTYS_OFF)) ! 91: tty.ty_status &= ~TTY_ON; ! 92: else if (scmp(_TTYS_ON)) ! 93: tty.ty_status |= TTY_ON; ! 94: else if (scmp(_TTYS_SECURE)) ! 95: tty.ty_status |= TTY_SECURE; ! 96: else if (vcmp(_TTYS_WINDOW)) ! 97: tty.ty_window = value(p); ! 98: else ! 99: break; ! 100: } ! 101: ! 102: if (zapchar == '#' || *p == '#') ! 103: while ((c = *++p) == ' ' || c == '\t') ! 104: ; ! 105: tty.ty_comment = p; ! 106: if (*p == 0) ! 107: tty.ty_comment = 0; ! 108: if (p = index(p, '\n')) ! 109: *p = '\0'; ! 110: return(&tty); ! 111: } ! 112: ! 113: #define QUOTED 1 ! 114: ! 115: /* ! 116: * Skip over the current field, removing quotes, and return a pointer to ! 117: * the next field. ! 118: */ ! 119: static char * ! 120: skip(p) ! 121: register char *p; ! 122: { ! 123: register char *t; ! 124: register int c, q; ! 125: ! 126: for (q = 0, t = p; (c = *p) != '\0'; p++) { ! 127: if (c == '"') { ! 128: q ^= QUOTED; /* obscure, but nice */ ! 129: continue; ! 130: } ! 131: if (q == QUOTED && *p == '\\' && *(p+1) == '"') ! 132: p++; ! 133: *t++ = *p; ! 134: if (q == QUOTED) ! 135: continue; ! 136: if (c == '#') { ! 137: zapchar = c; ! 138: *p = 0; ! 139: break; ! 140: } ! 141: if (c == '\t' || c == ' ' || c == '\n') { ! 142: zapchar = c; ! 143: *p++ = 0; ! 144: while ((c = *p) == '\t' || c == ' ' || c == '\n') ! 145: p++; ! 146: break; ! 147: } ! 148: } ! 149: *--t = '\0'; ! 150: return(p); ! 151: } ! 152: ! 153: static char * ! 154: value(p) ! 155: register char *p; ! 156: { ! 157: return((p = index(p, '=')) ? ++p : NULL); ! 158: } ! 159: ! 160: setttyent() ! 161: { ! 162: if (tf) { ! 163: (void)rewind(tf); ! 164: return(1); ! 165: } else if (tf = fopen(_PATH_TTYS, "r")) ! 166: return(1); ! 167: return(0); ! 168: } ! 169: ! 170: endttyent() ! 171: { ! 172: int rval; ! 173: ! 174: if (tf) { ! 175: rval = !(fclose(tf) == EOF); ! 176: tf = NULL; ! 177: return(rval); ! 178: } ! 179: return(1); ! 180: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.