* ggc-common.c (ggc_print_statistics): Make arguments to fprintf
[official-gcc.git] / libio / iogetline.c
blob5aac9b56387e9aa9aedab20f06baf9ea6b1b3639
1 /* Copyright (C) 1993, 1997, 1998 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>
29 #if defined(_LIBC) || !_G_HAVE_IO_GETLINE_INFO
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;
39 return _IO_getline_info (fp, buf, n, delim, extract_delim, (int *) 0);
42 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
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. */
50 _IO_size_t
51 _IO_getline_info (fp, buf, n, delim, extract_delim, eof)
52 _IO_FILE *fp;
53 char *buf;
54 _IO_size_t n;
55 int delim;
56 int extract_delim;
57 int *eof;
59 char *ptr = buf;
60 if (eof) *eof = 0;
61 while (n != 0)
63 _IO_ssize_t len = fp->_IO_read_end - fp->_IO_read_ptr;
64 if (len <= 0)
66 int c = __uflow (fp);
67 if (c == EOF)
69 if (eof) *eof = c;
70 break;
72 if (c == delim)
74 if (extract_delim > 0)
75 *ptr++ = c;
76 else if (extract_delim < 0)
77 _IO_sputbackc (fp, c);
78 return ptr - buf;
80 *ptr++ = c;
81 n--;
83 else
85 char *t;
86 if ((_IO_size_t) len >= n)
87 len = n;
88 t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
89 if (t != NULL)
91 _IO_size_t old_len = ptr-buf;
92 len = t - fp->_IO_read_ptr;
93 if (extract_delim >= 0)
95 ++t;
96 if (extract_delim > 0)
97 ++len;
99 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
100 fp->_IO_read_ptr = t;
101 return old_len + len;
103 memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
104 fp->_IO_read_ptr += len;
105 ptr += len;
106 n -= len;
109 return ptr - buf;
112 #endif /* Defined(_LIBC) || !_G_HAVE_IO_GETLINE_INFO */