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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libc/] [stdio/] [getdelim.c] - Blame information for rev 309

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/* Copyright 2002, Red Hat Inc. - all rights reserved */
2
/*
3
FUNCTION
4
<<getdelim>>---read a line up to a specified line delimiter
5
 
6
INDEX
7
        getdelim
8
 
9
ANSI_SYNOPSIS
10
        #include <stdio.h>
11
        int getdelim(char **<[bufptr]>, size_t *<[n]>,
12
                     int <[delim]>, FILE *<[fp]>);
13
 
14
TRAD_SYNOPSIS
15
        #include <stdio.h>
16
        int getdelim(<[bufptr]>, <[n]>, <[delim]>, <[fp]>)
17
        char **<[bufptr]>;
18
        size_t *<[n]>;
19
        int <[delim]>;
20
        FILE *<[fp]>;
21
 
22
DESCRIPTION
23
<<getdelim>> reads a file <[fp]> up to and possibly including a specified
24
delimiter <[delim]>.  The line is read into a buffer pointed to
25
by <[bufptr]> and designated with size *<[n]>.  If the buffer is
26
not large enough, it will be dynamically grown by <<getdelim>>.
27
As the buffer is grown, the pointer to the size <[n]> will be
28
updated.
29
 
30
RETURNS
31
<<getdelim>> returns <<-1>> if no characters were successfully read;
32
otherwise, it returns the number of bytes successfully read.
33
At end of file, the result is nonzero.
34
 
35
PORTABILITY
36
<<getdelim>> is a glibc extension.
37
 
38
No supporting OS subroutines are directly required.
39
*/
40
 
41
#include <_ansi.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <errno.h>
45
#include "local.h"
46
 
47
#define MIN_LINE_SIZE 4
48
#define DEFAULT_LINE_SIZE 128
49
 
50
ssize_t
51
_DEFUN(__getdelim, (bufptr, n, delim, fp),
52
       char **bufptr _AND
53
       size_t *n     _AND
54
       int delim     _AND
55
       FILE *fp)
56
{
57
  char *buf;
58
  char *ptr;
59
  size_t newsize, numbytes;
60
  int pos;
61
  int ch;
62
  int cont;
63
 
64
  if (fp == NULL || bufptr == NULL || n == NULL)
65
    {
66
      errno = EINVAL;
67
      return -1;
68
    }
69
 
70
  buf = *bufptr;
71
  if (buf == NULL || *n < MIN_LINE_SIZE)
72
    {
73
      buf = (char *)realloc (*bufptr, DEFAULT_LINE_SIZE);
74
      if (buf == NULL)
75
        {
76
          return -1;
77
        }
78
      *bufptr = buf;
79
      *n = DEFAULT_LINE_SIZE;
80
    }
81
 
82
  CHECK_INIT (_REENT, fp);
83
 
84
  __sfp_lock_acquire ();
85
  _flockfile (fp);
86
 
87
  numbytes = *n;
88
  ptr = buf;
89
 
90
  cont = 1;
91
 
92
  while (cont)
93
    {
94
      /* fill buffer - leaving room for nul-terminator */
95
      while (--numbytes > 0)
96
        {
97
          if ((ch = getc_unlocked (fp)) == EOF)
98
            {
99
              cont = 0;
100
              break;
101
            }
102
          else
103
            {
104
              *ptr++ = ch;
105
              if (ch == delim)
106
                {
107
                  cont = 0;
108
                  break;
109
                }
110
            }
111
        }
112
 
113
      if (cont)
114
        {
115
          /* Buffer is too small so reallocate a larger buffer.  */
116
          pos = ptr - buf;
117
          newsize = (*n << 1);
118
          buf = realloc (buf, newsize);
119
          if (buf == NULL)
120
            {
121
              cont = 0;
122
              break;
123
            }
124
 
125
          /* After reallocating, continue in new buffer */
126
          *bufptr = buf;
127
          *n = newsize;
128
          ptr = buf + pos;
129
          numbytes = newsize - pos;
130
        }
131
    }
132
 
133
  _funlockfile (fp);
134
  __sfp_lock_release ();
135
 
136
  /* if no input data, return failure */
137
  if (ptr == buf)
138
    return -1;
139
 
140
  /* otherwise, nul-terminate and return number of bytes read */
141
  *ptr = '\0';
142
  return (ssize_t)(ptr - buf);
143
}
144
 

powered by: WebSVN 2.1.0

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