* include/arpa/nameser.h: Also optimize NS_PUT16 and NS_PUT32.
[glibc.git] / libio / iogetline.c
blob022a444f7776b26aaaec7c78dbdbdda5259a3e57
1 /* Copyright (C) 1993,1997,1998,2000,2001,2002,2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA.
20 As a special exception, if you link the code in this file with
21 files compiled with a GNU compiler to produce an executable,
22 that does not cause the resulting executable to be covered by
23 the GNU Lesser General Public License. This exception does not
24 however invalidate any other reasons why the executable file
25 might be covered by the GNU Lesser General Public License.
26 This exception applies to code released by its copyright holders
27 in files containing the exception. */
29 #include "libioP.h"
30 #include <string.h>
32 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
34 _IO_size_t
35 _IO_getline (fp, buf, n, delim, extract_delim)
36 _IO_FILE *fp;
37 char *buf;
38 _IO_size_t n;
39 int delim;
40 int extract_delim;
42 return INTUSE(_IO_getline_info) (fp, buf, n, delim, extract_delim,
43 (int *) 0);
45 INTDEF(_IO_getline)
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_getline_info (fp, buf, n, delim, extract_delim, eof)
57 _IO_FILE *fp;
58 char *buf;
59 _IO_size_t n;
60 int delim;
61 int extract_delim;
62 int *eof;
64 char *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->_IO_read_end - fp->_IO_read_ptr;
72 if (len <= 0)
74 int c = __uflow (fp);
75 if (c == EOF)
77 if (eof)
78 *eof = c;
79 break;
81 if (c == delim)
83 if (extract_delim > 0)
84 *ptr++ = c;
85 else if (extract_delim < 0)
86 INTUSE(_IO_sputbackc) (fp, c);
87 if (extract_delim > 0)
88 ++len;
89 return ptr - buf;
91 *ptr++ = c;
92 n--;
94 else
96 char *t;
97 if ((_IO_size_t) len >= n)
98 len = n;
99 t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
100 if (t != NULL)
102 _IO_size_t old_len = ptr-buf;
103 len = t - fp->_IO_read_ptr;
104 if (extract_delim >= 0)
106 ++t;
107 if (extract_delim > 0)
108 ++len;
110 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
111 fp->_IO_read_ptr = t;
112 return old_len + len;
114 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
115 fp->_IO_read_ptr += len;
116 ptr += len;
117 n -= len;
120 return ptr - buf;
122 INTDEF(_IO_getline_info)
124 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */