OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [stdio/] [getdelim.c] - Diff between revs 148 and 158

Only display areas with differences | Details | Blame | View Log

Rev 148 Rev 158
/* Copyright 2002, Red Hat Inc. - all rights reserved */
/* Copyright 2002, Red Hat Inc. - all rights reserved */
/*
/*
FUNCTION
FUNCTION
<<getdelim>>---read a line up to a specified line delimiter
<<getdelim>>---read a line up to a specified line delimiter
 
 
INDEX
INDEX
        getdelim
        getdelim
 
 
ANSI_SYNOPSIS
ANSI_SYNOPSIS
        #include <stdio.h>
        #include <stdio.h>
        int getdelim(char **<[bufptr]>, size_t *<[n]>,
        int getdelim(char **<[bufptr]>, size_t *<[n]>,
                     int <[delim]>, FILE *<[fp]>);
                     int <[delim]>, FILE *<[fp]>);
 
 
TRAD_SYNOPSIS
TRAD_SYNOPSIS
        #include <stdio.h>
        #include <stdio.h>
        int getdelim(<[bufptr]>, <[n]>, <[delim]>, <[fp]>)
        int getdelim(<[bufptr]>, <[n]>, <[delim]>, <[fp]>)
        char **<[bufptr]>;
        char **<[bufptr]>;
        size_t *<[n]>;
        size_t *<[n]>;
        int <[delim]>;
        int <[delim]>;
        FILE *<[fp]>;
        FILE *<[fp]>;
 
 
DESCRIPTION
DESCRIPTION
<<getdelim>> reads a file <[fp]> up to and possibly including a specified
<<getdelim>> reads a file <[fp]> up to and possibly including a specified
delimiter <[delim]>.  The line is read into a buffer pointed to
delimiter <[delim]>.  The line is read into a buffer pointed to
by <[bufptr]> and designated with size *<[n]>.  If the buffer is
by <[bufptr]> and designated with size *<[n]>.  If the buffer is
not large enough, it will be dynamically grown by <<getdelim>>.
not large enough, it will be dynamically grown by <<getdelim>>.
As the buffer is grown, the pointer to the size <[n]> will be
As the buffer is grown, the pointer to the size <[n]> will be
updated.
updated.
 
 
RETURNS
RETURNS
<<getdelim>> returns <<-1>> if no characters were successfully read;
<<getdelim>> returns <<-1>> if no characters were successfully read;
otherwise, it returns the number of bytes successfully read.
otherwise, it returns the number of bytes successfully read.
At end of file, the result is nonzero.
At end of file, the result is nonzero.
 
 
PORTABILITY
PORTABILITY
<<getdelim>> is a glibc extension.
<<getdelim>> is a glibc extension.
 
 
No supporting OS subroutines are directly required.
No supporting OS subroutines are directly required.
*/
*/
 
 
#include <_ansi.h>
#include <_ansi.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <errno.h>
#include <errno.h>
#include "local.h"
#include "local.h"
 
 
#define MIN_LINE_SIZE 4
#define MIN_LINE_SIZE 4
#define DEFAULT_LINE_SIZE 128
#define DEFAULT_LINE_SIZE 128
 
 
ssize_t
ssize_t
_DEFUN(__getdelim, (bufptr, n, delim, fp),
_DEFUN(__getdelim, (bufptr, n, delim, fp),
       char **bufptr _AND
       char **bufptr _AND
       size_t *n     _AND
       size_t *n     _AND
       int delim     _AND
       int delim     _AND
       FILE *fp)
       FILE *fp)
{
{
  char *buf;
  char *buf;
  char *ptr;
  char *ptr;
  size_t newsize, numbytes;
  size_t newsize, numbytes;
  int pos;
  int pos;
  int ch;
  int ch;
  int cont;
  int cont;
 
 
  if (fp == NULL || bufptr == NULL || n == NULL)
  if (fp == NULL || bufptr == NULL || n == NULL)
    {
    {
      errno = EINVAL;
      errno = EINVAL;
      return -1;
      return -1;
    }
    }
 
 
  buf = *bufptr;
  buf = *bufptr;
  if (buf == NULL || *n < MIN_LINE_SIZE)
  if (buf == NULL || *n < MIN_LINE_SIZE)
    {
    {
      buf = (char *)realloc (*bufptr, DEFAULT_LINE_SIZE);
      buf = (char *)realloc (*bufptr, DEFAULT_LINE_SIZE);
      if (buf == NULL)
      if (buf == NULL)
        {
        {
          return -1;
          return -1;
        }
        }
      *bufptr = buf;
      *bufptr = buf;
      *n = DEFAULT_LINE_SIZE;
      *n = DEFAULT_LINE_SIZE;
    }
    }
 
 
  CHECK_INIT (_REENT, fp);
  CHECK_INIT (_REENT, fp);
 
 
  _flockfile (fp);
  _flockfile (fp);
 
 
  numbytes = *n;
  numbytes = *n;
  ptr = buf;
  ptr = buf;
 
 
  cont = 1;
  cont = 1;
 
 
  while (cont)
  while (cont)
    {
    {
      /* fill buffer - leaving room for nul-terminator */
      /* fill buffer - leaving room for nul-terminator */
      while (--numbytes > 0)
      while (--numbytes > 0)
        {
        {
          if ((ch = getc_unlocked (fp)) == EOF)
          if ((ch = getc_unlocked (fp)) == EOF)
            {
            {
              cont = 0;
              cont = 0;
              break;
              break;
            }
            }
          else
          else
            {
            {
              *ptr++ = ch;
              *ptr++ = ch;
              if (ch == delim)
              if (ch == delim)
                {
                {
                  cont = 0;
                  cont = 0;
                  break;
                  break;
                }
                }
            }
            }
        }
        }
 
 
      if (cont)
      if (cont)
        {
        {
          /* Buffer is too small so reallocate a larger buffer.  */
          /* Buffer is too small so reallocate a larger buffer.  */
          pos = ptr - buf;
          pos = ptr - buf;
          newsize = (*n << 1);
          newsize = (*n << 1);
          buf = realloc (buf, newsize);
          buf = realloc (buf, newsize);
          if (buf == NULL)
          if (buf == NULL)
            {
            {
              cont = 0;
              cont = 0;
              break;
              break;
            }
            }
 
 
          /* After reallocating, continue in new buffer */
          /* After reallocating, continue in new buffer */
          *bufptr = buf;
          *bufptr = buf;
          *n = newsize;
          *n = newsize;
          ptr = buf + pos;
          ptr = buf + pos;
          numbytes = newsize - pos;
          numbytes = newsize - pos;
        }
        }
    }
    }
 
 
  _funlockfile (fp);
  _funlockfile (fp);
 
 
  /* if no input data, return failure */
  /* if no input data, return failure */
  if (ptr == buf)
  if (ptr == buf)
    return -1;
    return -1;
 
 
  /* otherwise, nul-terminate and return number of bytes read */
  /* otherwise, nul-terminate and return number of bytes read */
  *ptr = '\0';
  *ptr = '\0';
  return (ssize_t)(ptr - buf);
  return (ssize_t)(ptr - buf);
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.