(Hash Search Function): Describe that memory must be cleared before calling hcreate_r.
[glibc.git] / libio / iogetline.c
blobeca38cf7735c183d301680812d6c11c953a40e73
1 /* Copyright (C) 1993,1997,1998,2000,2001,2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA.
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
28 #include "libioP.h"
29 #include <string.h>
31 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
33 _IO_size_t
34 _IO_getline (fp, buf, n, delim, extract_delim)
35 _IO_FILE *fp;
36 char *buf;
37 _IO_size_t n;
38 int delim;
39 int extract_delim;
41 return INTUSE(_IO_getline_info) (fp, buf, n, delim, extract_delim,
42 (int *) 0);
44 INTDEF(_IO_getline)
46 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
48 Read chars into buf (of size n), until delim is seen.
49 Return number of chars read (at most n).
50 Does not put a terminating '\0' in buf.
51 If extract_delim < 0, leave delimiter unread.
52 If extract_delim > 0, insert delim in output. */
54 _IO_size_t
55 _IO_getline_info (fp, buf, n, delim, extract_delim, eof)
56 _IO_FILE *fp;
57 char *buf;
58 _IO_size_t n;
59 int delim;
60 int extract_delim;
61 int *eof;
63 char *ptr = buf;
64 if (eof != NULL)
65 *eof = 0;
66 if (__builtin_expect (fp->_mode, -1) == 0)
67 _IO_fwide (fp, -1);
68 while (n != 0)
70 _IO_ssize_t len = fp->_IO_read_end - fp->_IO_read_ptr;
71 if (len <= 0)
73 int c = __uflow (fp);
74 if (c == EOF)
76 if (eof) *eof = c;
77 break;
79 if (c == delim)
81 if (extract_delim > 0)
82 *ptr++ = c;
83 else if (extract_delim < 0)
84 INTUSE(_IO_sputbackc) (fp, c);
85 if (extract_delim > 0)
86 ++len;
87 return ptr - buf;
89 *ptr++ = c;
90 n--;
92 else
94 char *t;
95 if ((_IO_size_t) len >= n)
96 len = n;
97 t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
98 if (t != NULL)
100 _IO_size_t old_len = ptr-buf;
101 len = t - fp->_IO_read_ptr;
102 if (extract_delim >= 0)
104 ++t;
105 if (extract_delim > 0)
106 ++len;
108 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
109 fp->_IO_read_ptr = t;
110 return old_len + len;
112 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
113 fp->_IO_read_ptr += len;
114 ptr += len;
115 n -= len;
118 return ptr - buf;
120 INTDEF(_IO_getline_info)
122 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */