Back to index

glibc  2.9
iogetline.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,1997,1998,2000,2001,2002,2005
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.
00019 
00020    As a special exception, if you link the code in this file with
00021    files compiled with a GNU compiler to produce an executable,
00022    that does not cause the resulting executable to be covered by
00023    the GNU Lesser General Public License.  This exception does not
00024    however invalidate any other reasons why the executable file
00025    might be covered by the GNU Lesser General Public License.
00026    This exception applies to code released by its copyright holders
00027    in files containing the exception.  */
00028 
00029 #include "libioP.h"
00030 #include <string.h>
00031 
00032 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
00033 
00034 _IO_size_t
00035 _IO_getline (fp, buf, n, delim, extract_delim)
00036      _IO_FILE *fp;
00037      char *buf;
00038      _IO_size_t n;
00039      int delim;
00040      int extract_delim;
00041 {
00042   return INTUSE(_IO_getline_info) (fp, buf, n, delim, extract_delim,
00043                                (int *) 0);
00044 }
00045 INTDEF(_IO_getline)
00046 
00047 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
00048 
00049    Read chars into buf (of size n), until delim is seen.
00050    Return number of chars read (at most n).
00051    Does not put a terminating '\0' in buf.
00052    If extract_delim < 0, leave delimiter unread.
00053    If extract_delim > 0, insert delim in output. */
00054 
00055 _IO_size_t
00056 _IO_getline_info (fp, buf, n, delim, extract_delim, eof)
00057      _IO_FILE *fp;
00058      char *buf;
00059      _IO_size_t n;
00060      int delim;
00061      int extract_delim;
00062      int *eof;
00063 {
00064   char *ptr = buf;
00065   if (eof != NULL)
00066     *eof = 0;
00067   if (__builtin_expect (fp->_mode, -1) == 0)
00068     _IO_fwide (fp, -1);
00069   while (n != 0)
00070     {
00071       _IO_ssize_t len = fp->_IO_read_end - fp->_IO_read_ptr;
00072       if (len <= 0)
00073        {
00074          int c = __uflow (fp);
00075          if (c == EOF)
00076            {
00077              if (eof)
00078               *eof = c;
00079              break;
00080            }
00081          if (c == delim)
00082            {
00083              if (extract_delim > 0)
00084               *ptr++ = c;
00085              else if (extract_delim < 0)
00086               INTUSE(_IO_sputbackc) (fp, c);
00087              if (extract_delim > 0)
00088               ++len;
00089              return ptr - buf;
00090            }
00091          *ptr++ = c;
00092          n--;
00093        }
00094       else
00095        {
00096          char *t;
00097          if ((_IO_size_t) len >= n)
00098            len = n;
00099          t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
00100          if (t != NULL)
00101            {
00102              _IO_size_t old_len = ptr-buf;
00103              len = t - fp->_IO_read_ptr;
00104              if (extract_delim >= 0)
00105               {
00106                 ++t;
00107                 if (extract_delim > 0)
00108                   ++len;
00109               }
00110              memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
00111              fp->_IO_read_ptr = t;
00112              return old_len + len;
00113            }
00114          memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
00115          fp->_IO_read_ptr += len;
00116          ptr += len;
00117          n -= len;
00118        }
00119     }
00120   return ptr - buf;
00121 }
00122 INTDEF(_IO_getline_info)
00123 
00124 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */