Update.
[glibc.git] / libio / iogetline.c
blob9a5f6910315cc0976e19aaa7ceb91efd02b3e4d5
1 /* Copyright (C) 1993, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
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.
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.
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.
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. */
26 #include "libioP.h"
27 #include <string.h>
29 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
31 _IO_size_t
32 _IO_getline (fp, buf, n, delim, extract_delim)
33 _IO_FILE *fp;
34 char *buf;
35 _IO_size_t n;
36 int delim;
37 int extract_delim;
39 return _IO_getline_info (fp, buf, n, delim, extract_delim, (int *) 0);
42 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
44 Read chars into buf (of size n), until delim is seen.
45 Return number of chars read (at most n).
46 Does not put a terminating '\0' in buf.
47 If extract_delim < 0, leave delimiter unread.
48 If extract_delim > 0, insert delim in output. */
50 _IO_size_t
51 _IO_getline_info (fp, buf, n, delim, extract_delim, eof)
52 _IO_FILE *fp;
53 char *buf;
54 _IO_size_t n;
55 int delim;
56 int extract_delim;
57 int *eof;
59 char *ptr = buf;
60 if (eof != NULL)
61 *eof = 0;
62 while (n != 0)
64 _IO_ssize_t len = fp->_IO_read_end - fp->_IO_read_ptr;
65 if (len <= 0)
67 int c = __uflow (fp);
68 if (c == EOF)
70 if (eof) *eof = c;
71 break;
73 if (c == delim)
75 if (extract_delim > 0)
76 *ptr++ = c;
77 else if (extract_delim < 0)
78 _IO_sputbackc (fp, c);
79 return ptr - buf;
80 if (extract_delim > 0)
81 ++len;
83 *ptr++ = c;
84 n--;
86 else
88 char *t;
89 if ((_IO_size_t) len >= n)
90 len = n;
91 t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
92 if (t != NULL)
94 _IO_size_t old_len = ptr-buf;
95 len = t - fp->_IO_read_ptr;
96 if (extract_delim >= 0)
98 ++t;
99 if (extract_delim > 0)
100 ++len;
102 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
103 fp->_IO_read_ptr = t;
104 return old_len + len;
106 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
107 fp->_IO_read_ptr += len;
108 ptr += len;
109 n -= len;
112 return ptr - buf;
115 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */