diff options
Diffstat (limited to 'lib/libc/gen/getttyent.c')
-rw-r--r-- | lib/libc/gen/getttyent.c | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/lib/libc/gen/getttyent.c b/lib/libc/gen/getttyent.c index f9b1e0d..ca0413c 100644 --- a/lib/libc/gen/getttyent.c +++ b/lib/libc/gen/getttyent.c @@ -37,11 +37,19 @@ static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; #include <ttyent.h> #include <stdio.h> +#include <stdlib.h> #include <ctype.h> #include <string.h> static char zapchar; static FILE *tf; +static size_t lbsize; +static char *line; + +#define MALLOCCHUNK 100 + +static char *skip __P((char *)); +static char *value __P((char *)); struct ttyent * getttynam(tty) @@ -50,7 +58,7 @@ getttynam(tty) register struct ttyent *t; setttyent(); - while (t = getttyent()) + while ( (t = getttyent()) ) if (!strcmp(tty, t->ty_name)) break; endttyent(); @@ -61,22 +69,26 @@ struct ttyent * getttyent() { static struct ttyent tty; - register int c; register char *p; -#define MAXLINELENGTH 100 - static char line[MAXLINELENGTH]; - static char *skip(), *value(); + register int c; + size_t i; if (!tf && !setttyent()) return (NULL); for (;;) { - if (!fgets(p = line, sizeof(line), tf)) + if (!fgets(p = line, lbsize, tf)) return (NULL); - /* skip lines that are too big */ - if (!index(p, '\n')) { - while ((c = getc(tf)) != '\n' && c != EOF) - ; - continue; + /* extend buffer if line was too big, and retry */ + while (!index(p, '\n')) { + i = strlen(p); + lbsize += MALLOCCHUNK; + if ((p = realloc(line, lbsize)) == NULL) { + (void)endttyent(); + return (NULL); + } + line = p; + if (!fgets(&line[i], lbsize - i, tf)) + return (NULL); } while (isspace(*p)) ++p; @@ -98,6 +110,7 @@ getttyent() } tty.ty_status = 0; tty.ty_window = NULL; + tty.ty_group = _TTYS_NOGROUP; #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1]) #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' @@ -108,8 +121,12 @@ getttyent() tty.ty_status |= TTY_ON; else if (scmp(_TTYS_SECURE)) tty.ty_status |= TTY_SECURE; + else if (scmp(_TTYS_INSECURE)) + tty.ty_status &= ~TTY_SECURE; else if (vcmp(_TTYS_WINDOW)) tty.ty_window = value(p); + else if (vcmp(_TTYS_GROUP)) + tty.ty_group = value(p); else break; } @@ -120,7 +137,7 @@ getttyent() tty.ty_comment = p; if (*p == 0) tty.ty_comment = 0; - if (p = index(p, '\n')) + if ( (p = index(p, '\n')) ) *p = '\0'; return (&tty); } @@ -177,10 +194,15 @@ int setttyent() { + if (line == NULL) { + if ((line = malloc(MALLOCCHUNK)) == NULL) + return (0); + lbsize = MALLOCCHUNK; + } if (tf) { - (void)rewind(tf); + rewind(tf); return (1); - } else if (tf = fopen(_PATH_TTYS, "r")) + } else if ( (tf = fopen(_PATH_TTYS, "r")) ) return (1); return (0); } @@ -190,8 +212,12 @@ endttyent() { int rval; + /* + * NB: Don't free `line' because getttynam() + * may still be referencing it + */ if (tf) { - rval = !(fclose(tf) == EOF); + rval = (fclose(tf) != EOF); tf = NULL; return (rval); } |