added fgetln.c to build

This commit is contained in:
gdr 1997-05-03 19:40:45 +00:00
parent c6828d9ec1
commit da9cc85bc1
3 changed files with 76 additions and 3 deletions

View File

@ -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 \

View File

@ -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)

72
lib/libc/stdio/fgetln.c Normal file
View File

@ -0,0 +1,72 @@
#ifdef __ORCAC__
segment "libc_stdio";
#endif
#pragma databank 1
#pragma optimize 0
#pragma debug 0
#pragma memorymodel 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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