]> sourceware.org Git - glibc.git/blame - libio/iogetline.c
Update.
[glibc.git] / libio / iogetline.c
CommitLineData
076bfcf6 1/* Copyright (C) 1993, 1997, 1998, 2000 Free Software Foundation, Inc.
40a55d20 2 This file is part of the GNU IO Library.
96aa2d94 3
40a55d20
UD
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.
96aa2d94 8
40a55d20
UD
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.
96aa2d94 13
40a55d20
UD
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.
96aa2d94 18
40a55d20
UD
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. */
96aa2d94
RM
25
26#include "libioP.h"
27#include <string.h>
28
0a614877
UD
29#if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
30
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;
38{
39 return _IO_getline_info (fp, buf, n, delim, extract_delim, (int *) 0);
40}
41
96aa2d94
RM
42/* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
43
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. */
49
50_IO_size_t
0a614877 51_IO_getline_info (fp, buf, n, delim, extract_delim, eof)
40a55d20
UD
52 _IO_FILE *fp;
53 char *buf;
54 _IO_size_t n;
55 int delim;
56 int extract_delim;
0a614877 57 int *eof;
96aa2d94 58{
40a55d20 59 char *ptr = buf;
0a614877
UD
60 if (eof != NULL)
61 *eof = 0;
076bfcf6
UD
62 if (__builtin_expect (fp->_mode, -1) == 0)
63 _IO_fwide (fp, -1);
00a2f9aa 64 while (n != 0)
96aa2d94
RM
65 {
66 _IO_ssize_t len = fp->_IO_read_end - fp->_IO_read_ptr;
96aa2d94 67 if (len <= 0)
96aa2d94 68 {
0a614877
UD
69 int c = __uflow (fp);
70 if (c == EOF)
96aa2d94 71 {
0a614877
UD
72 if (eof) *eof = c;
73 break;
74 }
75 if (c == delim)
76 {
77 if (extract_delim > 0)
78 *ptr++ = c;
79 else if (extract_delim < 0)
80 _IO_sputbackc (fp, c);
81 return ptr - buf;
96aa2d94 82 if (extract_delim > 0)
40a55d20 83 ++len;
96aa2d94 84 }
0a614877
UD
85 *ptr++ = c;
86 n--;
96aa2d94 87 }
0a614877
UD
88 else
89 {
90 char *t;
91 if ((_IO_size_t) len >= n)
92 len = n;
93 t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
94 if (t != NULL)
95 {
96 _IO_size_t old_len = ptr-buf;
97 len = t - fp->_IO_read_ptr;
98 if (extract_delim >= 0)
99 {
100 ++t;
101 if (extract_delim > 0)
102 ++len;
103 }
104 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
105 fp->_IO_read_ptr = t;
106 return old_len + len;
107 }
108 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
109 fp->_IO_read_ptr += len;
110 ptr += len;
111 n -= len;
112 }
08cac4ac 113 }
96aa2d94
RM
114 return ptr - buf;
115}
0a614877
UD
116
117#endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */
This page took 0.126482 seconds and 5 git commands to generate.