Avoid dup3 PLT usage.
[platform/upstream/glibc.git] / libio / iogetline.c
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.
4
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.
9
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.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.
18
19    As a special exception, if you link the code in this file with
20    files compiled with a GNU compiler to produce an executable,
21    that does not cause the resulting executable to be covered by
22    the GNU Lesser General Public License.  This exception does not
23    however invalidate any other reasons why the executable file
24    might be covered by the GNU Lesser General Public License.
25    This exception applies to code released by its copyright holders
26    in files containing the exception.  */
27
28 #include "libioP.h"
29 #include <string.h>
30
31 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
32
33 _IO_size_t
34 _IO_getline (fp, buf, n, delim, extract_delim)
35      _IO_FILE *fp;
36      char *buf;
37      _IO_size_t n;
38      int delim;
39      int extract_delim;
40 {
41   return INTUSE(_IO_getline_info) (fp, buf, n, delim, extract_delim,
42                                    (int *) 0);
43 }
44 INTDEF(_IO_getline)
45
46 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
47
48    Read chars into buf (of size n), until delim is seen.
49    Return number of chars read (at most n).
50    Does not put a terminating '\0' in buf.
51    If extract_delim < 0, leave delimiter unread.
52    If extract_delim > 0, insert delim in output. */
53
54 _IO_size_t
55 _IO_getline_info (fp, buf, n, delim, extract_delim, eof)
56      _IO_FILE *fp;
57      char *buf;
58      _IO_size_t n;
59      int delim;
60      int extract_delim;
61      int *eof;
62 {
63   char *ptr = buf;
64   if (eof != NULL)
65     *eof = 0;
66   if (__builtin_expect (fp->_mode, -1) == 0)
67     _IO_fwide (fp, -1);
68   while (n != 0)
69     {
70       _IO_ssize_t len = fp->_IO_read_end - fp->_IO_read_ptr;
71       if (len <= 0)
72         {
73           int c = __uflow (fp);
74           if (c == EOF)
75             {
76               if (eof)
77                 *eof = c;
78               break;
79             }
80           if (c == delim)
81             {
82               if (extract_delim > 0)
83                 *ptr++ = c;
84               else if (extract_delim < 0)
85                 INTUSE(_IO_sputbackc) (fp, c);
86               if (extract_delim > 0)
87                 ++len;
88               return ptr - buf;
89             }
90           *ptr++ = c;
91           n--;
92         }
93       else
94         {
95           char *t;
96           if ((_IO_size_t) len >= n)
97             len = n;
98           t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
99           if (t != NULL)
100             {
101               _IO_size_t old_len = ptr-buf;
102               len = t - fp->_IO_read_ptr;
103               if (extract_delim >= 0)
104                 {
105                   ++t;
106                   if (extract_delim > 0)
107                     ++len;
108                 }
109               memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
110               fp->_IO_read_ptr = t;
111               return old_len + len;
112             }
113           memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
114           fp->_IO_read_ptr += len;
115           ptr += len;
116           n -= len;
117         }
118     }
119   return ptr - buf;
120 }
121 INTDEF(_IO_getline_info)
122
123 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */