OSDN Git Service

* Makefile.in (local-distclean): Also remove fastjar.
[pf3gnuchains/gcc-fork.git] / libio / iogetdelim.c
1 /* Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU IO Library.
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2, or (at
7    your option) any later version.
8
9    This library is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this library; see the file COPYING.  If not, write to
16    the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17    MA 02111-1307, USA.
18
19    As a special exception, if you link this library with files
20    compiled with a GNU compiler to produce an executable, this does
21    not cause the resulting executable to be covered by the GNU General
22    Public License.  This exception does not however invalidate any
23    other reasons why the executable file might be covered by the GNU
24    General Public License.  */
25
26 #ifdef __STDC__
27 #include <stdlib.h>
28 #endif
29 #include "libioP.h"
30 #include <string.h>
31 #include <errno.h>
32
33 /* Read up to (and including) a TERMINATOR from FP into *LINEPTR
34    (and null-terminate it).  *LINEPTR is a pointer returned from malloc (or
35    NULL), pointing to *N characters of space.  It is realloc'ed as
36    necessary.  Returns the number of characters read (not including the
37    null terminator), or -1 on error or EOF.  */
38
39 _IO_ssize_t
40 _IO_getdelim (lineptr, n, delimiter, fp)
41      char **lineptr;
42      _IO_size_t *n;
43      int delimiter;
44      _IO_FILE *fp;
45 {
46   int result;
47   _IO_ssize_t cur_len = 0;
48   _IO_ssize_t len;
49
50   if (lineptr == NULL || n == NULL)
51     {
52       MAYBE_SET_EINVAL;
53       return -1;
54     }
55   CHECK_FILE (fp, -1);
56   _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
57   _IO_flockfile (fp);
58   if (_IO_ferror_unlocked (fp))
59     {
60       result = -1;
61       goto unlock_return;
62     }
63
64   if (*lineptr == NULL || *n == 0)
65     {
66       *n = 120;
67       *lineptr = (char *) malloc (*n);
68       if (*lineptr == NULL)
69         {
70           result = -1;
71           goto unlock_return;
72         }
73     }
74
75   len = fp->_IO_read_end - fp->_IO_read_ptr;
76   if (len <= 0)
77     {
78       if (__underflow (fp) == EOF)
79         {
80           result = -1;
81           goto unlock_return;
82         }
83       len = fp->_IO_read_end - fp->_IO_read_ptr;
84     }
85
86   for (;;)
87     {
88       _IO_size_t needed;
89       char *t;
90       t = (char *) memchr ((void *) fp->_IO_read_ptr, delimiter, len);
91       if (t != NULL)
92         len = (t - fp->_IO_read_ptr) + 1;
93       /* Make enough space for len+1 (for final NUL) bytes.  */
94       needed = cur_len + len + 1;
95       if (needed > *n)
96         {
97           if (needed < 2 * *n)
98             needed = 2 * *n;  /* Be generous. */
99           *n = needed;
100           *lineptr = (char *) realloc (*lineptr, needed);
101           if (*lineptr == NULL)
102             {
103               result = -1;
104               goto unlock_return;
105             }
106         }
107       memcpy (*lineptr + cur_len, (void *) fp->_IO_read_ptr, len);
108       fp->_IO_read_ptr += len;
109       cur_len += len;
110       if (t != NULL || __underflow (fp) == EOF)
111         break;
112       len = fp->_IO_read_end - fp->_IO_read_ptr;
113     }
114   (*lineptr)[cur_len] = '\0';
115   result = cur_len;
116
117 unlock_return:
118   _IO_cleanup_region_end (1);
119   return result;
120 }
121
122 #ifdef weak_alias
123 weak_alias (_IO_getdelim, __getdelim)
124 weak_alias (_IO_getdelim, getdelim)
125 #endif