summaryrefslogtreecommitdiff
path: root/contrib/global/lib/mgets.c
diff options
context:
space:
mode:
authorHidetoshi Shimokawa <simokawa@FreeBSD.org>1999-01-18 06:59:18 +0000
committerHidetoshi Shimokawa <simokawa@FreeBSD.org>1999-01-18 06:59:18 +0000
commit3ba3e2cc4b63fa16707f51e735e751e62dd9526e (patch)
tree2d073c94248fff7dcaa8a3b1356933e1dcd784db /contrib/global/lib/mgets.c
parent0823b5bf088ea8520ea23a0c424e4ecab62491d5 (diff)
Notes
Diffstat (limited to 'contrib/global/lib/mgets.c')
-rw-r--r--contrib/global/lib/mgets.c55
1 files changed, 38 insertions, 17 deletions
diff --git a/contrib/global/lib/mgets.c b/contrib/global/lib/mgets.c
index 2bc6099517c2..7f0cc7221c0e 100644
--- a/contrib/global/lib/mgets.c
+++ b/contrib/global/lib/mgets.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1996, 1997 Shigio Yamaguchi. All rights reserved.
+ * Copyright (c) 1996, 1997, 1998 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,16 +28,19 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * mgets.c 8-Nov-97
+ * mgets.c 29-Aug-98
*
*/
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
-#include "mgets.h"
#include "die.h"
+#include "mgets.h"
+
+#define EXPANDSIZE 127
+#define MINSIZE 16
-#define EXPANDSIZE 512
static int mbufsize = EXPANDSIZE;
static char *mbuf;
@@ -45,20 +48,21 @@ static char *mbuf;
* mgets: read whole record into allocated buffer
*
* i) ip input stream
+ * o) length record length
* i) flags flags
- * MGETS_CONT \\ + \n -> \n
+ * MGETS_CONT \\ + \n -> ''
* MGETS_SKIPCOM skip line which start with '#'.
- * o) length length of record
+ * MGETS_TAILCUT remove following blanks
* 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, flags, length)
+mgets(ip, length, flags)
FILE *ip;
-int flags;
int *length;
+int flags;
{
char *p;
@@ -72,27 +76,33 @@ int *length;
* read whole record.
*/
if (!fgets(mbuf, mbufsize, ip))
- return (char *)0;
+ return NULL;
if (flags & MGETS_SKIPCOM)
while (*mbuf == '#')
if (!fgets(mbuf, mbufsize, ip))
- return (char *)0;
+ return NULL;
p = mbuf + strlen(mbuf);
+
for (;;) {
/*
* get a line.
*/
while (*(p - 1) != '\n') {
/*
- * expand and read additionally.
+ * expand buffer and read additionally.
*/
int count = p - mbuf;
- 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.");
+
+ 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;
+ }
p += strlen(p);
}
/*
@@ -109,6 +119,17 @@ int *length;
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;