From d427d09204c97df14607149e94a1da9beddd76ab Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Fri, 21 Jun 2002 18:29:23 +0000 Subject: 2002-06-21 Jeff Johnston * libc/include/stdio.h (__getline, __getdelim): New prototypes. * libc/include/time.h [HAVE_GETDATE](getdate, getdate_r): Ditto. [HAVE_GETDATE](getdate_err): New error code. * libc/stdio/Makefile.am: Add support for getline.c and getdelim.c. * libc/stdio/Makefile.in: Regenerated. * libc/stdio/getdelim.c: New file. * libc/stdio/getline.c: Ditto. * libc/sys/linux/Makefile.am: Add support for getdate.c, getdate_err.c and ntp_gettime.c. Also add AM_CFLAGS to point to libc/stdio. * libc/sys/linux/Makefile.in: Regenerated. * libc/sys/linux/getdate.c: New file. * libc/sys/linux/getdate_err.c: Ditto. * libc/sys/linux/ntp_gettime.c: Ditto. * libc/sys/linux/time.c (adjtimex, ntp_adjtime): New functions. * libc/sys/linux/sys/stdio.h (getline, getdelim): New macros. --- newlib/libc/stdio/Makefile.am | 4 ++ newlib/libc/stdio/Makefile.in | 25 +++++--- newlib/libc/stdio/getdelim.c | 139 ++++++++++++++++++++++++++++++++++++++++++ newlib/libc/stdio/getline.c | 54 ++++++++++++++++ 4 files changed, 212 insertions(+), 10 deletions(-) create mode 100644 newlib/libc/stdio/getdelim.c create mode 100644 newlib/libc/stdio/getline.c (limited to 'newlib/libc/stdio') diff --git a/newlib/libc/stdio/Makefile.am b/newlib/libc/stdio/Makefile.am index e7d4fea21..b7eaab8e4 100644 --- a/newlib/libc/stdio/Makefile.am +++ b/newlib/libc/stdio/Makefile.am @@ -35,6 +35,8 @@ LIB_SOURCES = \ getchar.c \ getc_u.c \ getchar_u.c \ + getdelim.c \ + getline.c \ gets.c \ getw.c \ iprintf.c \ @@ -129,6 +131,8 @@ CHEWOUT_FILES = \ getchar.def \ getc_u.def \ getchar_u.def \ + getdelim.def \ + getline.def \ gets.def \ getw.def \ iprintf.def \ diff --git a/newlib/libc/stdio/Makefile.in b/newlib/libc/stdio/Makefile.in index b8d348f90..7210cb771 100644 --- a/newlib/libc/stdio/Makefile.in +++ b/newlib/libc/stdio/Makefile.in @@ -139,6 +139,8 @@ LIB_SOURCES = \ getchar.c \ getc_u.c \ getchar_u.c \ + getdelim.c \ + getline.c \ gets.c \ getw.c \ iprintf.c \ @@ -220,6 +222,8 @@ CHEWOUT_FILES = \ getchar.def \ getc_u.def \ getchar_u.def \ + getdelim.def \ + getline.def \ gets.def \ getw.def \ iprintf.def \ @@ -274,7 +278,8 @@ LIBS = @LIBS@ @USE_LIBTOOL_FALSE@fseek.$(OBJEXT) fsetpos.$(OBJEXT) ftell.$(OBJEXT) \ @USE_LIBTOOL_FALSE@fvwrite.$(OBJEXT) fwalk.$(OBJEXT) fwrite.$(OBJEXT) \ @USE_LIBTOOL_FALSE@getc.$(OBJEXT) getchar.$(OBJEXT) getc_u.$(OBJEXT) \ -@USE_LIBTOOL_FALSE@getchar_u.$(OBJEXT) gets.$(OBJEXT) getw.$(OBJEXT) \ +@USE_LIBTOOL_FALSE@getchar_u.$(OBJEXT) getdelim.$(OBJEXT) \ +@USE_LIBTOOL_FALSE@getline.$(OBJEXT) gets.$(OBJEXT) getw.$(OBJEXT) \ @USE_LIBTOOL_FALSE@iprintf.$(OBJEXT) makebuf.$(OBJEXT) mktemp.$(OBJEXT) \ @USE_LIBTOOL_FALSE@perror.$(OBJEXT) printf.$(OBJEXT) putc.$(OBJEXT) \ @USE_LIBTOOL_FALSE@putchar.$(OBJEXT) putc_u.$(OBJEXT) \ @@ -299,15 +304,15 @@ LTLIBRARIES = $(noinst_LTLIBRARIES) @USE_LIBTOOL_TRUE@fopen.lo fprintf.lo fputc.lo fputs.lo fread.lo \ @USE_LIBTOOL_TRUE@freopen.lo fscanf.lo fseek.lo fsetpos.lo ftell.lo \ @USE_LIBTOOL_TRUE@fvwrite.lo fwalk.lo fwrite.lo getc.lo getchar.lo \ -@USE_LIBTOOL_TRUE@getc_u.lo getchar_u.lo gets.lo getw.lo iprintf.lo \ -@USE_LIBTOOL_TRUE@makebuf.lo mktemp.lo perror.lo printf.lo putc.lo \ -@USE_LIBTOOL_TRUE@putchar.lo putc_u.lo putchar_u.lo puts.lo putw.lo \ -@USE_LIBTOOL_TRUE@refill.lo remove.lo rename.lo rewind.lo rget.lo \ -@USE_LIBTOOL_TRUE@scanf.lo setbuf.lo setbuffer.lo setlinebuf.lo \ -@USE_LIBTOOL_TRUE@setvbuf.lo siprintf.lo snprintf.lo sprintf.lo \ -@USE_LIBTOOL_TRUE@sscanf.lo stdio.lo tmpfile.lo tmpnam.lo ungetc.lo \ -@USE_LIBTOOL_TRUE@vfscanf.lo vprintf.lo vscanf.lo vsnprintf.lo \ -@USE_LIBTOOL_TRUE@vsprintf.lo vsscanf.lo wbuf.lo wsetup.lo +@USE_LIBTOOL_TRUE@getc_u.lo getchar_u.lo getdelim.lo getline.lo gets.lo \ +@USE_LIBTOOL_TRUE@getw.lo iprintf.lo makebuf.lo mktemp.lo perror.lo \ +@USE_LIBTOOL_TRUE@printf.lo putc.lo putchar.lo putc_u.lo putchar_u.lo \ +@USE_LIBTOOL_TRUE@puts.lo putw.lo refill.lo remove.lo rename.lo \ +@USE_LIBTOOL_TRUE@rewind.lo rget.lo scanf.lo setbuf.lo setbuffer.lo \ +@USE_LIBTOOL_TRUE@setlinebuf.lo setvbuf.lo siprintf.lo snprintf.lo \ +@USE_LIBTOOL_TRUE@sprintf.lo sscanf.lo stdio.lo tmpfile.lo tmpnam.lo \ +@USE_LIBTOOL_TRUE@ungetc.lo vfscanf.lo vprintf.lo vscanf.lo \ +@USE_LIBTOOL_TRUE@vsnprintf.lo vsprintf.lo vsscanf.lo wbuf.lo wsetup.lo CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) diff --git a/newlib/libc/stdio/getdelim.c b/newlib/libc/stdio/getdelim.c new file mode 100644 index 000000000..8ec9d40c8 --- /dev/null +++ b/newlib/libc/stdio/getdelim.c @@ -0,0 +1,139 @@ +/* +FUNCTION +<>---read a line up to a specified line delimeter + +INDEX + getdelim + +ANSI_SYNOPSIS + #include + int getdelim(char **<[bufptr]>, size_t *<[n]>, + int <[delim]>, FILE *<[fp]>); + +TRAD_SYNOPSIS + #include + int getdelim(<[bufptr]>, <[n]>, <[delim]>, <[fp]>) + char **<[bufptr]>; + size_t *<[n]>; + int <[delim]>; + FILE *<[fp]>; + +DESCRIPTION +<> reads a file <[fp]> up to and possibly including a specified +delimeter <[delim]>. The line is read into a buffer pointed to +by <[bufptr]> and designated with size *<[n]>. If the buffer is +not large enough, it will be dynamically grown by <>. +As the buffer is grown, the pointer to the size <[n]> will be +updated. + +RETURNS +<> returns <<-1>> if no characters were successfully read, +otherwise, it returns the number of bytes successfully read. +at end of file, the result is nonzero. + +PORTABILITY +<> is a glibc extension. + +No supporting OS subroutines are directly required. +*/ + +/* Copyright 2002, Red Hat Inc. - all rights reserved */ + +#include +#include +#include +#include "local.h" + +#define MIN_LINE_SIZE 4 +#define DEFAULT_LINE_SIZE 128 + +ssize_t +__getdelim (bufptr, n, delim, fp) + char **bufptr; + size_t *n; + int delim; + FILE *fp; +{ + char *buf; + char *ptr; + size_t newsize, numbytes; + int pos; + int ch; + int cont; + + if (fp == NULL || bufptr == NULL || n == NULL) + { + errno = EINVAL; + return -1; + } + + buf = *bufptr; + if (buf == NULL || *n < MIN_LINE_SIZE) + { + buf = (char *)realloc (*bufptr, DEFAULT_LINE_SIZE); + if (buf == NULL) + { + return -1; + } + *bufptr = buf; + *n = DEFAULT_LINE_SIZE; + } + + _flockfile(fp); + + CHECK_INIT(fp); + + numbytes = *n; + ptr = buf; + + cont = 1; + + while (cont) + { + /* fill buffer - leaving room for nul-terminator */ + while (--numbytes > 0) + { + if ((ch = getc_unlocked (fp)) == EOF) + { + cont = 0; + break; + } + else + { + *ptr++ = ch; + if (ch == delim) + { + cont = 0; + break; + } + } + } + + /* Buffer is too small so reallocate a larger buffer. */ + pos = ptr - buf; + newsize = (*n << 1); + buf = realloc (buf, newsize); + if (buf == NULL) + { + cont = 0; + break; + } + + /* After reallocating, continue in new buffer */ + *bufptr = buf; + *n = newsize; + ptr = buf + pos; + numbytes = newsize - pos; + } + + _funlockfile (fp); + + /* if no input data, return failure */ + if (ptr == buf) + return -1; + + /* otherwise, nul-terminate and return number of bytes read */ + *ptr = '\0'; + return (ssize_t)(ptr - buf); +} + diff --git a/newlib/libc/stdio/getline.c b/newlib/libc/stdio/getline.c new file mode 100644 index 000000000..741a7dc77 --- /dev/null +++ b/newlib/libc/stdio/getline.c @@ -0,0 +1,54 @@ +/* +FUNCTION +<>---read a line from a file + +INDEX + getline + +ANSI_SYNOPSIS + #include + ssize_t getline(char **<[bufptr]>, size_t *<[n]>, FILE *<[fp]>); + +TRAD_SYNOPSIS + #include + ssize_t getline(<[bufptr]>, <[n]>, <[fp]>) + char **<[bufptr]>; + size_t *<[n]>; + FILE *<[fp]>; + +DESCRIPTION +<> reads a file <[fp]> up to and possibly including the +newline character. The line is read into a buffer pointed to +by <[bufptr]> and designated with size *<[n]>. If the buffer is +not large enough, it will be dynamically grown by <>. +As the buffer is grown, the pointer to the size <[n]> will be +updated. + +<> is equivalent to getdelim(bufptr, n, '\n', fp); + +RETURNS +<> returns <<-1>> if no characters were successfully read, +otherwise, it returns the number of bytes successfully read. +at end of file, the result is nonzero. + +PORTABILITY +<> is a glibc extension. + +No supporting OS subroutines are directly required. +*/ + +/* Copyright 2002, Red Hat Inc. - all rights reserved */ + +#include + +extern ssize_t __getdelim (char **, size_t *, int, FILE *); + +ssize_t +__getline (lptr, n, fp) + char **lptr; + size_t *n; + FILE *fp; +{ + return __getdelim (lptr, n, '\n', fp); +} + -- cgit v1.2.3