diff options
Diffstat (limited to 'contrib/global/lib/mgets.c')
| -rw-r--r-- | contrib/global/lib/mgets.c | 55 |
1 files changed, 17 insertions, 38 deletions
diff --git a/contrib/global/lib/mgets.c b/contrib/global/lib/mgets.c index 6b9bb731f70c4..b30cae643936b 100644 --- a/contrib/global/lib/mgets.c +++ b/contrib/global/lib/mgets.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 1997, 1998 Shigio Yamaguchi. All rights reserved. + * Copyright (c) 1996, 1997 Shigio Yamaguchi. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -28,20 +28,17 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * mgets.c 29-Aug-98 + * mgets.c 8-Nov-97 * */ -#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "die.h" #include "mgets.h" +#include "die.h" -#define EXPANDSIZE 127 -#define MINSIZE 16 - +#define EXPANDSIZE 512 static int mbufsize = EXPANDSIZE; static char *mbuf; @@ -49,21 +46,20 @@ static char *mbuf; * mgets: read whole record into allocated buffer * * i) ip input stream - * o) length record length * i) flags flags - * MGETS_CONT \\ + \n -> '' + * MGETS_CONT \\ + \n -> \n * MGETS_SKIPCOM skip line which start with '#'. - * MGETS_TAILCUT remove following blanks + * o) length length of record * r) record buffer (NULL at end of file) * * Returned buffer has whole record. * The buffer end with '\0' and doesn't include '\r' and '\n'. */ char * -mgets(ip, length, flags) +mgets(ip, flags, length) FILE *ip; -int *length; int flags; +int *length; { char *p; @@ -77,33 +73,27 @@ int flags; * read whole record. */ if (!fgets(mbuf, mbufsize, ip)) - return NULL; + return (char *)0; if (flags & MGETS_SKIPCOM) while (*mbuf == '#') if (!fgets(mbuf, mbufsize, ip)) - return NULL; + return (char *)0; p = mbuf + strlen(mbuf); - for (;;) { /* * get a line. */ while (*(p - 1) != '\n') { /* - * expand buffer and read additionally. + * expand and read additionally. */ int count = p - mbuf; - - if (mbufsize - count < MINSIZE) { - mbufsize += EXPANDSIZE; - if (!(mbuf = (char *)realloc(mbuf, mbufsize + 1))) - die("short of memory."); - p = mbuf + count; - } - if (!fgets(p, mbufsize - count, ip)) { - *p++ = '\n'; - break; - } + mbufsize += EXPANDSIZE; + if (!(mbuf = (char *)realloc(mbuf, mbufsize + 1))) + die("short of memory."); + p = mbuf + count; + if (!fgets(p, mbufsize - count, ip)) + die("illegal end of file."); p += strlen(p); } /* @@ -120,17 +110,6 @@ int flags; else break; } -/* - if (flags & MGETS_SKIPCOM) - for (p = mbuf; *p; p++) - if (*p == '#') { - *p = 0; - break; - } -*/ - if (flags & MGETS_TAILCUT) - while (isspace(*(--p))) - *p = 0; if (length) *length = p - mbuf; return mbuf; |
