]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/iogetline.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / libio / iogetline.c
CommitLineData
581c785b 1/* Copyright (C) 1993-2022 Free Software Foundation, Inc.
41bdb6e2 2 This file is part of the GNU C Library.
96aa2d94 3
41bdb6e2
AJ
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.
96aa2d94 8
41bdb6e2
AJ
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
40a55d20 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
96aa2d94 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>.
96aa2d94 17
41bdb6e2
AJ
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
96aa2d94
RM
26
27#include "libioP.h"
28#include <string.h>
29
9964a145
ZW
30size_t
31_IO_getline (FILE *fp, char *buf, size_t n, int delim,
f63f2bfd 32 int extract_delim)
0a614877 33{
d18ea0c5 34 return _IO_getline_info (fp, buf, n, delim, extract_delim, (int *) 0);
0a614877 35}
d18ea0c5 36libc_hidden_def (_IO_getline)
0a614877 37
96aa2d94
RM
38/* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
39
40 Read chars into buf (of size n), until delim is seen.
41 Return number of chars read (at most n).
42 Does not put a terminating '\0' in buf.
43 If extract_delim < 0, leave delimiter unread.
44 If extract_delim > 0, insert delim in output. */
45
9964a145
ZW
46size_t
47_IO_getline_info (FILE *fp, char *buf, size_t n, int delim,
f63f2bfd 48 int extract_delim, int *eof)
96aa2d94 49{
40a55d20 50 char *ptr = buf;
0a614877
UD
51 if (eof != NULL)
52 *eof = 0;
076bfcf6
UD
53 if (__builtin_expect (fp->_mode, -1) == 0)
54 _IO_fwide (fp, -1);
00a2f9aa 55 while (n != 0)
96aa2d94 56 {
9964a145 57 ssize_t len = fp->_IO_read_end - fp->_IO_read_ptr;
96aa2d94 58 if (len <= 0)
96aa2d94 59 {
0a614877
UD
60 int c = __uflow (fp);
61 if (c == EOF)
96aa2d94 62 {
b5d839c9
UD
63 if (eof)
64 *eof = c;
0a614877
UD
65 break;
66 }
67 if (c == delim)
68 {
69 if (extract_delim > 0)
70 *ptr++ = c;
71 else if (extract_delim < 0)
d18ea0c5 72 _IO_sputbackc (fp, c);
96aa2d94 73 if (extract_delim > 0)
40a55d20 74 ++len;
8da2915d 75 return ptr - buf;
96aa2d94 76 }
0a614877
UD
77 *ptr++ = c;
78 n--;
96aa2d94 79 }
b5d839c9
UD
80 else
81 {
82 char *t;
9964a145 83 if ((size_t) len >= n)
b5d839c9
UD
84 len = n;
85 t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
86 if (t != NULL)
87 {
9964a145 88 size_t old_len = ptr-buf;
b5d839c9
UD
89 len = t - fp->_IO_read_ptr;
90 if (extract_delim >= 0)
91 {
92 ++t;
93 if (extract_delim > 0)
94 ++len;
95 }
96 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
97 fp->_IO_read_ptr = t;
98 return old_len + len;
99 }
100 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
101 fp->_IO_read_ptr += len;
102 ptr += len;
103 n -= len;
104 }
08cac4ac 105 }
96aa2d94
RM
106 return ptr - buf;
107}
d18ea0c5 108libc_hidden_def (_IO_getline_info)