* sysdeps/m68k/dl-machine.h (_dl_start_user): Pass correct
[glibc.git] / libio / iogetwline.c
blob2559cd8eb67a543d9537182d613cfdab83fad00a
1 /* Copyright (C) 1993, 1997, 1998, 1999, 2000 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 #ifdef _LIBC
31 # define wmemcpy __wmemcpy
32 #endif
34 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
36 _IO_size_t
37 _IO_getwline (fp, buf, n, delim, extract_delim)
38 _IO_FILE *fp;
39 wchar_t *buf;
40 _IO_size_t n;
41 wint_t delim;
42 int extract_delim;
44 return _IO_getwline_info (fp, buf, n, delim, extract_delim, (wint_t *) 0);
47 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
49 Read chars into buf (of size n), until delim is seen.
50 Return number of chars read (at most n).
51 Does not put a terminating '\0' in buf.
52 If extract_delim < 0, leave delimiter unread.
53 If extract_delim > 0, insert delim in output. */
55 _IO_size_t
56 _IO_getwline_info (fp, buf, n, delim, extract_delim, eof)
57 _IO_FILE *fp;
58 wchar_t *buf;
59 _IO_size_t n;
60 wint_t delim;
61 int extract_delim;
62 wint_t *eof;
64 wchar_t *ptr = buf;
65 if (eof != NULL)
66 *eof = 0;
67 if (__builtin_expect (fp->_mode, 1) == 0)
68 _IO_fwide (fp, 1);
69 while (n != 0)
71 _IO_ssize_t len = (fp->_wide_data->_IO_read_end
72 - fp->_wide_data->_IO_read_ptr);
73 if (len <= 0)
75 wint_t wc = __wuflow (fp);
76 if (wc == WEOF)
78 if (eof)
79 *eof = wc;
80 break;
82 if (wc == delim)
84 if (extract_delim > 0)
85 *ptr++ = wc;
86 else if (extract_delim < 0)
87 _IO_sputbackc (fp, wc);
88 return ptr - buf;
89 if (extract_delim > 0)
90 ++len;
92 *ptr++ = wc;
93 n--;
95 else
97 wchar_t *t;
98 if ((_IO_size_t) len >= n)
99 len = n;
100 t = wmemchr ((void *) fp->_wide_data->_IO_read_ptr, delim, len);
101 if (t != NULL)
103 _IO_size_t old_len = ptr - buf;
104 len = t - fp->_wide_data->_IO_read_ptr;
105 if (extract_delim >= 0)
107 ++t;
108 if (extract_delim > 0)
109 ++len;
111 wmemcpy ((void *) ptr, (void *) fp->_wide_data->_IO_read_ptr,
112 len);
113 fp->_wide_data->_IO_read_ptr = t;
114 return old_len + len;
116 wmemcpy ((void *) ptr, (void *) fp->_wide_data->_IO_read_ptr, len);
117 fp->_wide_data->_IO_read_ptr += len;
118 ptr += len;
119 n -= len;
122 return ptr - buf;
125 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */