lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 1989, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 4. Neither the name of the University nor the names of its contributors |
| 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <features.h> |
| 31 | #include <ttyent.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdio_ext.h> |
| 34 | #include <ctype.h> |
| 35 | #include <string.h> |
| 36 | #include <stdlib.h> |
| 37 | #ifdef __UCLIBC_HAS_THREADS__ |
| 38 | # include <pthread.h> |
| 39 | #endif |
| 40 | |
| 41 | static char zapchar; |
| 42 | static FILE *tf; |
| 43 | static struct ttyent tty; |
| 44 | |
| 45 | |
| 46 | /* Skip over the current field, removing quotes, and return |
| 47 | * a pointer to the next field. |
| 48 | */ |
| 49 | #define QUOTED 1 |
| 50 | static char * skip(register char *p) |
| 51 | { |
| 52 | register char *t; |
| 53 | register int c, q; |
| 54 | |
| 55 | for (q = 0, t = p; (c = *p) != '\0'; p++) { |
| 56 | if (c == '"') { |
| 57 | q ^= QUOTED; /* obscure, but nice */ |
| 58 | continue; |
| 59 | } |
| 60 | if (q == QUOTED && *p == '\\' && *(p+1) == '"') |
| 61 | p++; |
| 62 | *t++ = *p; |
| 63 | if (q == QUOTED) |
| 64 | continue; |
| 65 | if (c == '#') { |
| 66 | zapchar = c; |
| 67 | *p = 0; |
| 68 | break; |
| 69 | } |
| 70 | if (c == '\t' || c == ' ' || c == '\n') { |
| 71 | zapchar = c; |
| 72 | *p++ = 0; |
| 73 | while ((c = *p) == '\t' || c == ' ' || c == '\n') |
| 74 | p++; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | *--t = '\0'; |
| 79 | return (p); |
| 80 | } |
| 81 | |
| 82 | static char * value(register char *p) |
| 83 | { |
| 84 | |
| 85 | return ((p = strchr(p, '=')) ? ++p : NULL); |
| 86 | } |
| 87 | |
| 88 | int setttyent(void) |
| 89 | { |
| 90 | |
| 91 | if (tf) { |
| 92 | rewind(tf); |
| 93 | return (1); |
| 94 | } else if ((tf = fopen(_PATH_TTYS, "r"))) { |
| 95 | /* We do the locking ourselves. */ |
| 96 | #ifdef __UCLIBC_HAS_THREADS__ |
| 97 | __fsetlocking (tf, FSETLOCKING_BYCALLER); |
| 98 | #endif |
| 99 | return (1); |
| 100 | } |
| 101 | return (0); |
| 102 | } |
| 103 | libc_hidden_def(setttyent) |
| 104 | |
| 105 | struct ttyent * getttyent(void) |
| 106 | { |
| 107 | register int c; |
| 108 | register char *p; |
| 109 | static char *line = NULL; |
| 110 | struct ttyent *retval = NULL; |
| 111 | |
| 112 | if (!tf && !setttyent()) |
| 113 | return (NULL); |
| 114 | |
| 115 | if (!line) { |
| 116 | line = malloc(BUFSIZ); |
| 117 | if (!line) |
| 118 | abort(); |
| 119 | } |
| 120 | |
| 121 | __STDIO_ALWAYS_THREADLOCK(tf); |
| 122 | |
| 123 | for (;;) { |
| 124 | if (!fgets_unlocked(p = line, BUFSIZ, tf)) { |
| 125 | goto DONE; |
| 126 | } |
| 127 | /* skip lines that are too big */ |
| 128 | if (!strchr(p, '\n')) { |
| 129 | while ((c = getc_unlocked(tf)) != '\n' && c != EOF) |
| 130 | ; |
| 131 | continue; |
| 132 | } |
| 133 | while (isspace(*p)) |
| 134 | ++p; |
| 135 | if (*p && *p != '#') |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | zapchar = 0; |
| 140 | tty.ty_name = p; |
| 141 | p = skip(p); |
| 142 | if (!*(tty.ty_getty = p)) |
| 143 | tty.ty_getty = tty.ty_type = NULL; |
| 144 | else { |
| 145 | p = skip(p); |
| 146 | if (!*(tty.ty_type = p)) |
| 147 | tty.ty_type = NULL; |
| 148 | else |
| 149 | p = skip(p); |
| 150 | } |
| 151 | tty.ty_status = 0; |
| 152 | tty.ty_window = NULL; |
| 153 | |
| 154 | #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) |
| 155 | #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' |
| 156 | for (; *p; p = skip(p)) { |
| 157 | if (scmp(_TTYS_OFF)) |
| 158 | tty.ty_status &= ~TTY_ON; |
| 159 | else if (scmp(_TTYS_ON)) |
| 160 | tty.ty_status |= TTY_ON; |
| 161 | else if (scmp(_TTYS_SECURE)) |
| 162 | tty.ty_status |= TTY_SECURE; |
| 163 | else if (vcmp(_TTYS_WINDOW)) |
| 164 | tty.ty_window = value(p); |
| 165 | else |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | if (zapchar == '#' || *p == '#') |
| 170 | while ((c = *++p) == ' ' || c == '\t') |
| 171 | ; |
| 172 | tty.ty_comment = p; |
| 173 | if (*p == 0) |
| 174 | tty.ty_comment = 0; |
| 175 | if ((p = strchr(p, '\n'))) |
| 176 | *p = '\0'; |
| 177 | retval = &tty; |
| 178 | |
| 179 | DONE: |
| 180 | __STDIO_ALWAYS_THREADUNLOCK(tf); |
| 181 | return retval; |
| 182 | } |
| 183 | libc_hidden_def(getttyent) |
| 184 | |
| 185 | int endttyent(void) |
| 186 | { |
| 187 | int rval; |
| 188 | |
| 189 | if (tf) { |
| 190 | rval = !(fclose(tf) == EOF); |
| 191 | tf = NULL; |
| 192 | return (rval); |
| 193 | } |
| 194 | return (1); |
| 195 | } |
| 196 | libc_hidden_def(endttyent) |
| 197 | |
| 198 | struct ttyent * getttynam(const char *_tty) |
| 199 | { |
| 200 | register struct ttyent *t; |
| 201 | |
| 202 | setttyent(); |
| 203 | while ((t = getttyent())) |
| 204 | if (!strcmp(_tty, t->ty_name)) |
| 205 | break; |
| 206 | endttyent(); |
| 207 | return (t); |
| 208 | } |