(linenum→info "unix/slp.c:2238")

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