* libio/fileops.c (_IO_new_file_underflow): Set error indicator
[glibc/pb-stable.git] / libio / iogetwline.c
blob402158a4efa89a827345b5d32d2afff31708e99f
1 /* Copyright (C) 1993, 1997, 1998, 1999 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>
28 #include <wchar.h>
30 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
32 _IO_size_t
33 _IO_getwline (fp, buf, n, delim, extract_delim)
34 _IO_FILE *fp;
35 wchar_t *buf;
36 _IO_size_t n;
37 wint_t delim;
38 int extract_delim;
40 return _IO_getwline_info (fp, buf, n, delim, extract_delim, (wint_t *) 0);
43 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
45 Read chars into buf (of size n), until delim is seen.
46 Return number of chars read (at most n).
47 Does not put a terminating '\0' in buf.
48 If extract_delim < 0, leave delimiter unread.
49 If extract_delim > 0, insert delim in output. */
51 _IO_size_t
52 _IO_getwline_info (fp, buf, n, delim, extract_delim, eof)
53 _IO_FILE *fp;
54 wchar_t *buf;
55 _IO_size_t n;
56 wint_t delim;
57 int extract_delim;
58 wint_t *eof;
60 wchar_t *ptr = buf;
61 if (eof != NULL)
62 *eof = 0;
63 while (n != 0)
65 _IO_ssize_t len = (fp->_wide_data->_IO_read_end
66 - fp->_wide_data->_IO_read_ptr);
67 if (len <= 0)
69 wint_t wc = __wuflow (fp);
70 if (wc == WEOF)
72 if (eof)
73 *eof = wc;
74 break;
76 if (wc == delim)
78 if (extract_delim > 0)
79 *ptr++ = wc;
80 else if (extract_delim < 0)
81 _IO_sputbackc (fp, wc);
82 return ptr - buf;
83 if (extract_delim > 0)
84 ++len;
86 *ptr++ = wc;
87 n--;
89 else
91 wchar_t *t;
92 if ((_IO_size_t) len >= n)
93 len = n;
94 t = (wchar_t *) memchr ((void *) fp->_wide_data->_IO_read_ptr,
95 delim, len);
96 if (t != NULL)
98 _IO_size_t old_len = ptr - buf;
99 len = t - fp->_wide_data->_IO_read_ptr;
100 if (extract_delim >= 0)
102 ++t;
103 if (extract_delim > 0)
104 ++len;
106 memcpy ((void *) ptr, (void *) fp->_wide_data->_IO_read_ptr,
107 len);
108 fp->_wide_data->_IO_read_ptr = t;
109 return old_len + len;
111 memcpy ((void *) ptr, (void *) fp->_wide_data->_IO_read_ptr, len);
112 fp->_wide_data->_IO_read_ptr += len;
113 ptr += len;
114 n -= len;
117 return ptr - buf;
120 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */