use isinf(3) and isnan(3) instead of rolling our own
[unix-history] / usr / src / lib / libc / gen / getttyent.c
CommitLineData
bb0cfa24 1/*
06c5d283
KB
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
269a7923 5 * %sccs.include.redist.c%
bb0cfa24
DF
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
c5980113 9static char sccsid[] = "@(#)getttyent.c 5.9 (Berkeley) %G%";
06c5d283 10#endif /* LIBC_SCCS and not lint */
d011ae29 11
06c5d283 12#include <ttyent.h>
d011ae29 13#include <stdio.h>
06c5d283 14#include <ctype.h>
6ebcb998 15#include <string.h>
d011ae29 16
9c787b65 17static char zapchar;
06c5d283 18static FILE *tf;
d011ae29 19
06c5d283
KB
20struct ttyent *
21getttynam(tty)
c5980113 22 const char *tty;
d011ae29 23{
06c5d283
KB
24 register struct ttyent *t;
25
26 setttyent();
27 while (t = getttyent())
28 if (!strcmp(tty, t->ty_name))
29 break;
30 return(t);
d011ae29
RC
31}
32
06c5d283
KB
33struct ttyent *
34getttyent()
d011ae29 35{
06c5d283
KB
36 static struct ttyent tty;
37 register int c;
38 register char *p;
39#define MAXLINELENGTH 100
40 static char line[MAXLINELENGTH];
c5980113 41 static char *skip(), *value();
06c5d283
KB
42
43 if (!tf && !setttyent())
44 return(NULL);
7baa3ecd
KB
45 for (;;) {
46 if (!fgets(p = line, sizeof(line), tf))
06c5d283
KB
47 return(NULL);
48 /* skip lines that are too big */
7baa3ecd 49 if (!index(p, '\n')) {
06c5d283
KB
50 while ((c = getc(tf)) != '\n' && c != EOF)
51 ;
52 continue;
53 }
7baa3ecd
KB
54 while (isspace(*p))
55 ++p;
56 if (*p && *p != '#')
57 break;
58 }
06c5d283
KB
59
60 zapchar = 0;
61 tty.ty_name = p;
62 p = skip(p);
63 if (!*(tty.ty_getty = p))
64 tty.ty_getty = tty.ty_type = NULL;
65 else {
66 p = skip(p);
67 if (!*(tty.ty_type = p))
68 tty.ty_type = NULL;
69 else
70 p = skip(p);
d011ae29 71 }
06c5d283
KB
72 tty.ty_status = 0;
73 tty.ty_window = NULL;
74
75#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
76#define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
77 for (; *p; p = skip(p)) {
78 if (scmp(_TTYS_OFF))
79 tty.ty_status &= ~TTY_ON;
80 else if (scmp(_TTYS_ON))
81 tty.ty_status |= TTY_ON;
82 else if (scmp(_TTYS_SECURE))
83 tty.ty_status |= TTY_SECURE;
84 else if (vcmp(_TTYS_WINDOW))
85 tty.ty_window = value(p);
86 else
87 break;
88 }
89
90 if (zapchar == '#' || *p == '#')
91 while ((c = *++p) == ' ' || c == '\t')
92 ;
93 tty.ty_comment = p;
94 if (*p == 0)
95 tty.ty_comment = 0;
96 if (p = index(p, '\n'))
97 *p = '\0';
98 return(&tty);
d011ae29
RC
99}
100
06c5d283 101#define QUOTED 1
18971fdd
RC
102
103/*
06c5d283
KB
104 * Skip over the current field, removing quotes, and return a pointer to
105 * the next field.
18971fdd 106 */
d011ae29
RC
107static char *
108skip(p)
18971fdd 109 register char *p;
d011ae29 110{
06c5d283
KB
111 register char *t;
112 register int c, q;
d011ae29 113
06c5d283 114 for (q = 0, t = p; (c = *p) != '\0'; p++) {
18971fdd
RC
115 if (c == '"') {
116 q ^= QUOTED; /* obscure, but nice */
117 continue;
118 }
9c787b65
MK
119 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
120 p++;
121 *t++ = *p;
18971fdd
RC
122 if (q == QUOTED)
123 continue;
d011ae29 124 if (c == '#') {
9c787b65
MK
125 zapchar = c;
126 *p = 0;
d011ae29
RC
127 break;
128 }
129 if (c == '\t' || c == ' ' || c == '\n') {
9c787b65
MK
130 zapchar = c;
131 *p++ = 0;
d011ae29
RC
132 while ((c = *p) == '\t' || c == ' ' || c == '\n')
133 p++;
134 break;
135 }
d011ae29 136 }
9c787b65 137 *--t = '\0';
06c5d283 138 return(p);
d011ae29
RC
139}
140
18971fdd
RC
141static char *
142value(p)
143 register char *p;
144{
06c5d283 145 return((p = index(p, '=')) ? ++p : NULL);
18971fdd
RC
146}
147
c5980113 148int
06c5d283 149setttyent()
d011ae29 150{
06c5d283
KB
151 if (tf) {
152 (void)rewind(tf);
153 return(1);
154 } else if (tf = fopen(_PATH_TTYS, "r"))
155 return(1);
156 return(0);
157}
d011ae29 158
c5980113 159int
06c5d283
KB
160endttyent()
161{
162 int rval;
163
164 if (tf) {
165 rval = !(fclose(tf) == EOF);
166 tf = NULL;
167 return(rval);
d011ae29 168 }
06c5d283 169 return(1);
d011ae29 170}