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