Annotation of GNUtools/libg++/libio/iogetline.c, revision 1.1.1.1

1.1       root        1: /* 
                      2: Copyright (C) 1993 Free Software Foundation
                      3: 
                      4: This file is part of the GNU IO Library.  This library is free
                      5: software; you can redistribute it and/or modify it under the
                      6: terms of the GNU General Public License as published by the
                      7: Free Software Foundation; either version 2, or (at your option)
                      8: any later version.
                      9: 
                     10: This 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
                     13: GNU General Public License for more details.
                     14: 
                     15: You should have received a copy of the GNU General Public License
                     16: along with GNU CC; see the file COPYING.  If not, write to
                     17: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
                     18: 
                     19: As a special exception, if you link this library with files
                     20: compiled with a GNU compiler to produce an executable, this does not cause
                     21: the resulting executable to be covered by the GNU General Public License.
                     22: This exception does not however invalidate any other reasons why
                     23: the executable file might be covered by the GNU General Public License. */
                     24: 
                     25: #include "libioP.h"
                     26: #include <string.h>
                     27: 
                     28: /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
                     29: 
                     30:    Read chars into buf (of size n), until delim is seen.
                     31:    Return number of chars read (at most n-1).
                     32:    If extract_delim < 0, leave delimiter unread.
                     33:    If extract_delim > 0, insert delim in output. */
                     34: 
                     35: _IO_size_t
                     36: _IO_getline(fp, buf, n, delim, extract_delim)
                     37:      _IO_FILE *fp;
                     38:      char* buf;
                     39:      _IO_size_t n;
                     40:      int delim;
                     41:      int extract_delim;
                     42: {
                     43:   register char *ptr = buf;
                     44:   if (n <= 0)
                     45:     return EOF;
                     46:   n--; /* Leave space for final '\0'. */
                     47:   do
                     48:     {
                     49:       _IO_size_t len = fp->_IO_read_end - fp->_IO_read_ptr;
                     50:       char *t;
                     51:       if (len <= 0)
                     52:        if (__underflow(fp) == EOF)
                     53:          break;
                     54:        else
                     55:          len = fp->_IO_read_end - fp->_IO_read_ptr;
                     56:       if (len >= n)
                     57:        len = n;
                     58:       t = (char*)memchr((void*)fp->_IO_read_ptr, delim, len);
                     59:       if (t != NULL)
                     60:        {
                     61:          _IO_size_t old_len = ptr-buf;
                     62:          len = t - fp->_IO_read_ptr;
                     63:          if (extract_delim >= 0)
                     64:            {
                     65:              t++;
                     66:              old_len++;
                     67:              if (extract_delim > 0)
                     68:                len++;
                     69:            }
                     70:          memcpy((void*)ptr, (void*)fp->_IO_read_ptr, len);
                     71:          ptr[len] = 0;
                     72:          fp->_IO_read_ptr = t;
                     73:          return old_len + len;
                     74:        }
                     75:       memcpy((void*)ptr, (void*)fp->_IO_read_ptr, len);
                     76:       fp->_IO_read_ptr += len;
                     77:       ptr += len;
                     78:       n -= len;
                     79:     } while (n != 0);
                     80:   *ptr = 0;
                     81:   return ptr - buf;
                     82: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.