diff options
| author | Joerg Wunsch <joerg@FreeBSD.org> | 1996-10-19 16:29:10 +0000 |
|---|---|---|
| committer | Joerg Wunsch <joerg@FreeBSD.org> | 1996-10-19 16:29:10 +0000 |
| commit | 5fae0297dbe3fbc4d141f76df6f6c8c39e83d02c (patch) | |
| tree | 0be608856c87d5ebc4c2bec217873f598ef01140 /lib/libc/gen/getttyent.c | |
| parent | b0dac7029f347c2afba9d20941f16c2a7f1bdbbd (diff) | |
Notes
Diffstat (limited to 'lib/libc/gen/getttyent.c')
| -rw-r--r-- | lib/libc/gen/getttyent.c | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/lib/libc/gen/getttyent.c b/lib/libc/gen/getttyent.c index 267101505638..a4b31aa54f7d 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) @@ -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 ((line = realloc(line, lbsize)) == NULL) { + (void)endttyent(); + return (NULL); + } + p = line; + if (!fgets(&line[i], lbsize - i, tf)) + return (NULL); } while (isspace(*p)) ++p; @@ -177,6 +189,11 @@ int setttyent() { + if (line == NULL) { + if ((line = malloc(MALLOCCHUNK)) == NULL) + return (0); + lbsize = MALLOCCHUNK; + } if (tf) { rewind(tf); return (1); @@ -190,8 +207,13 @@ endttyent() { int rval; + if (line) { + free(line); + line = NULL; + lbsize = 0; + } if (tf) { - rval = !(fclose(tf) == EOF); + rval = (fclose(tf) != EOF); tf = NULL; return (rval); } |
