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