]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/getttyent.c
zfs: merge openzfs/zfs@797f55ef1
[FreeBSD/FreeBSD.git] / lib / libc / gen / getttyent.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)getttyent.c       8.1 (Berkeley) 6/4/93");
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/sysctl.h>
37
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ttyent.h>
43
44 static char zapchar;
45 static FILE *tf;
46 static size_t lbsize;
47 static char *line;
48
49 #define MALLOCCHUNK     100
50
51 static char *skip(char *);
52 static char *value(char *);
53
54 struct ttyent *
55 getttynam(const char *tty)
56 {
57         struct ttyent *t;
58
59         if (strncmp(tty, "/dev/", 5) == 0)
60                 tty += 5;
61         setttyent();
62         while ( (t = getttyent()) )
63                 if (!strcmp(tty, t->ty_name))
64                         break;
65         endttyent();
66         return (t);
67 }
68
69 static int
70 auto_tty_status(const char *ty_name)
71 {
72         size_t len;
73         char *buf, *cons, *nextcons;
74         int rv;
75
76         rv = TTY_IFCONSOLE;
77
78         /* Check if this is an enabled kernel console line */
79         buf = NULL;
80         if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
81                 return (rv); /* Errors mean don't enable */
82         buf = malloc(len);
83         if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
84                 goto done;
85
86         if ((cons = strchr(buf, '/')) == NULL)
87                 goto done;
88         *cons = '\0';
89         nextcons = buf;
90         while ((cons = strsep(&nextcons, ",")) != NULL && strlen(cons) != 0) {
91                 if (strcmp(cons, ty_name) == 0) {
92                         rv |= TTY_ON;
93                         break;
94                 }
95         }
96
97 done:
98         free(buf);
99         return (rv);
100 }
101
102 static int
103 auto_exists_status(const char *ty_name)
104 {
105         struct stat sb;
106         char *dev;
107         int rv;
108
109         rv = TTY_IFEXISTS;
110         if (*ty_name == '/')
111                 asprintf(&dev, "%s", ty_name);
112         else
113                 asprintf(&dev, "/dev/%s", ty_name);
114         if (dev != NULL && stat(dev, &sb) == 0)
115                 rv |= TTY_ON;
116         free(dev);
117         return (rv);
118 }
119
120 struct ttyent *
121 getttyent(void)
122 {
123         static struct ttyent tty;
124         char *p;
125         int c;
126         size_t i;
127
128         if (!tf && !setttyent())
129                 return (NULL);
130         for (;;) {
131                 if (!fgets(p = line, lbsize, tf))
132                         return (NULL);
133                 /* extend buffer if line was too big, and retry */
134                 while (!strchr(p, '\n') && !feof(tf)) {
135                         i = strlen(p);
136                         lbsize += MALLOCCHUNK;
137                         if ((p = realloc(line, lbsize)) == NULL) {
138                                 (void)endttyent();
139                                 return (NULL);
140                         }
141                         line = p;
142                         if (!fgets(&line[i], lbsize - i, tf))
143                                 return (NULL);
144                 }
145                 while (isspace((unsigned char)*p))
146                         ++p;
147                 if (*p && *p != '#')
148                         break;
149         }
150
151 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1])
152 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
153
154         zapchar = 0;
155         tty.ty_name = p;
156         tty.ty_status = 0;
157         tty.ty_window = NULL;
158         tty.ty_group  = _TTYS_NOGROUP;
159
160         p = skip(p);
161         if (!*(tty.ty_getty = p))
162                 tty.ty_getty = tty.ty_type = NULL;
163         else {
164                 p = skip(p);
165                 if (!*(tty.ty_type = p))
166                         tty.ty_type = NULL;
167                 else {
168                         /* compatibility kludge: handle network/dialup specially */
169                         if (scmp(_TTYS_DIALUP))
170                                 tty.ty_status |= TTY_DIALUP;
171                         else if (scmp(_TTYS_NETWORK))
172                                 tty.ty_status |= TTY_NETWORK;
173                         p = skip(p);
174                 }
175         }
176
177         for (; *p; p = skip(p)) {
178                 if (scmp(_TTYS_OFF))
179                         tty.ty_status &= ~TTY_ON;
180                 else if (scmp(_TTYS_ON))
181                         tty.ty_status |= TTY_ON;
182                 else if (scmp(_TTYS_ONIFCONSOLE))
183                         tty.ty_status |= auto_tty_status(tty.ty_name);
184                 else if (scmp(_TTYS_ONIFEXISTS))
185                         tty.ty_status |= auto_exists_status(tty.ty_name);
186                 else if (scmp(_TTYS_SECURE))
187                         tty.ty_status |= TTY_SECURE;
188                 else if (scmp(_TTYS_INSECURE))
189                         tty.ty_status &= ~TTY_SECURE;
190                 else if (scmp(_TTYS_DIALUP))
191                         tty.ty_status |= TTY_DIALUP;
192                 else if (scmp(_TTYS_NETWORK))
193                         tty.ty_status |= TTY_NETWORK;
194                 else if (vcmp(_TTYS_WINDOW))
195                         tty.ty_window = value(p);
196                 else if (vcmp(_TTYS_GROUP))
197                         tty.ty_group = value(p);
198                 else
199                         break;
200         }
201
202         if (zapchar == '#' || *p == '#')
203                 while ((c = *++p) == ' ' || c == '\t')
204                         ;
205         tty.ty_comment = p;
206         if (*p == 0)
207                 tty.ty_comment = 0;
208         if ((p = strchr(p, '\n')))
209                 *p = '\0';
210         return (&tty);
211 }
212
213 #define QUOTED  1
214
215 /*
216  * Skip over the current field, removing quotes, and return a pointer to
217  * the next field.
218  */
219 static char *
220 skip(char *p)
221 {
222         char *t;
223         int c, q;
224
225         for (q = 0, t = p; (c = *p) != '\0'; p++) {
226                 if (c == '"') {
227                         q ^= QUOTED;    /* obscure, but nice */
228                         continue;
229                 }
230                 if (q == QUOTED && *p == '\\' && *(p+1) == '"')
231                         p++;
232                 *t++ = *p;
233                 if (q == QUOTED)
234                         continue;
235                 if (c == '#') {
236                         zapchar = c;
237                         *p = 0;
238                         break;
239                 }
240                 if (c == '\t' || c == ' ' || c == '\n') {
241                         zapchar = c;
242                         *p++ = 0;
243                         while ((c = *p) == '\t' || c == ' ' || c == '\n')
244                                 p++;
245                         break;
246                 }
247         }
248         *--t = '\0';
249         return (p);
250 }
251
252 static char *
253 value(char *p)
254 {
255
256         return ((p = strchr(p, '=')) ? ++p : NULL);
257 }
258
259 int
260 setttyent(void)
261 {
262
263         if (line == NULL) {
264                 if ((line = malloc(MALLOCCHUNK)) == NULL)
265                         return (0);
266                 lbsize = MALLOCCHUNK;
267         }
268         if (tf) {
269                 rewind(tf);
270                 return (1);
271         } else if ( (tf = fopen(_PATH_TTYS, "re")) )
272                 return (1);
273         return (0);
274 }
275
276 int
277 endttyent(void)
278 {
279         int rval;
280
281         /*
282          * NB: Don't free `line' because getttynam()
283          * may still be referencing it
284          */
285         if (tf) {
286                 rval = (fclose(tf) != EOF);
287                 tf = NULL;
288                 return (rval);
289         }
290         return (1);
291 }
292
293 static int
294 isttystat(const char *tty, int flag)
295 {
296         struct ttyent *t;
297
298         return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag);
299 }
300
301
302 int
303 isdialuptty(const char *tty)
304 {
305
306         return isttystat(tty, TTY_DIALUP);
307 }
308
309 int
310 isnettty(const char *tty)
311 {
312
313         return isttystat(tty, TTY_NETWORK);
314 }