diff --git a/lib/libc/Makefile b/lib/libc/Makefile index 1f424c1..29a660c 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -6,7 +6,7 @@ # directories: # gen, gno, locale, stdio, stdlib, string, sys # -# $Id: Makefile,v 1.1 1997/02/28 05:12:39 gdr Exp $ +# $Id: Makefile,v 1.2 1997/05/03 19:40:37 gdr Exp $ # .INCLUDE: ../const.mk @@ -45,6 +45,7 @@ LIBC_OBJ = \ gen/tty.o \ gen/utime.o \ locale/table.o \ + stdio/fgetln.o \ stdio/mktemp.o \ stdio/perror.o \ stdio/tempnam.o \ diff --git a/lib/libc/stdio/Makefile b/lib/libc/stdio/Makefile index c1e4cf7..2a89fc0 100644 --- a/lib/libc/stdio/Makefile +++ b/lib/libc/stdio/Makefile @@ -1,10 +1,10 @@ # -# $Id: Makefile,v 1.1 1997/02/28 05:12:49 gdr Exp $ +# $Id: Makefile,v 1.2 1997/05/03 19:40:45 gdr Exp $ # .INCLUDE: ../../const.mk -OBJS = mktemp.o perror.o tempnam.o +OBJS = fgetln.o mktemp.o perror.o tempnam.o default: $(OBJS) diff --git a/lib/libc/stdio/fgetln.c b/lib/libc/stdio/fgetln.c new file mode 100644 index 0000000..259efe0 --- /dev/null +++ b/lib/libc/stdio/fgetln.c @@ -0,0 +1,72 @@ +#ifdef __ORCAC__ +segment "libc_stdio"; +#endif + +#pragma databank 1 +#pragma optimize 0 +#pragma debug 0 +#pragma memorymodel 0 + +#include +#include +#include + +#define LINESIZE 20 + +char * +fgetln (FILE *stream, size_t *len) +{ + static char *buffer = NULL; + static size_t currentBufferSize = 0; + char *p, *q; + + if (buffer == NULL) { + currentBufferSize = LINESIZE; + if ((buffer = malloc (currentBufferSize)) == NULL) { + return NULL; + } + } + + + p = fgets(buffer, currentBufferSize, stream); + if (p == NULL) { + return NULL; + } + + for (;;) { + if ((p = strchr(buffer, '\n')) != NULL) { + break; + } + p = buffer + strlen(buffer); + if ((q = realloc(buffer, currentBufferSize + LINESIZE)) == NULL) { + return NULL; + } + buffer = q; + currentBufferSize += LINESIZE; + if (fgets(p, LINESIZE, stream) == NULL) { + if (ferror(stream)) { + return NULL; + } else { + break; + } + } + } + *len = strlen(buffer); + return buffer; +} + + +#if 0 /* for testing fgetln */ + +int +main(int argc, char **argv) { + char *p; + size_t len; + + while ((p = fgetln(stdin, &len)) != NULL) { + printf("%d:%s", len, p); + } + return 0; +} + +#endif