(root)/
acl-2.3.1/
libmisc/
next_line.c
       1  /*
       2    Copyright (C) 2009  Andreas Gruenbacher <agruen@suse.de>
       3  
       4    This program is free software: you can redistribute it and/or modify it
       5    under the terms of the GNU Lesser General Public License as published by
       6    the Free Software Foundation, either version 2.1 of the License, or
       7    (at your option) any later version.
       8  
       9    This program is distributed in the hope that it will be useful,
      10    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12    GNU General Public License for more details.
      13  
      14    You should have received a copy of the GNU General Public License
      15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16   */
      17  
      18  #include "config.h"
      19  #include <stdio.h>
      20  #include <string.h>
      21  #include <limits.h>
      22  #include <unistd.h>
      23  #include "misc.h"
      24  
      25  #define LINE_SIZE getpagesize()
      26  
      27  char *__acl_next_line(FILE *file)
      28  {
      29  	static char *line;
      30  	static size_t line_size;
      31  	char *c;
      32  	int eol = 0;
      33  
      34  	if (!line) {
      35  		if (__acl_high_water_alloc((void **)&line, &line_size, LINE_SIZE))
      36  			return NULL;
      37  	}
      38  	c = line;
      39  	do {
      40  		if (!fgets(c, line_size - (c - line), file))
      41  			return NULL;
      42  		c = strrchr(c, '\0');
      43  		while (c > line && (*(c-1) == '\n' || *(c-1) == '\r')) {
      44  			c--;
      45  			*c = '\0';
      46  			eol = 1;
      47  		}
      48  		if (feof(file))
      49  			break;
      50  		if (!eol) {
      51  			if (__acl_high_water_alloc((void **)&line, &line_size,
      52  					     2 * line_size))
      53  				return NULL;
      54  			c = strrchr(line, '\0');
      55  		}
      56  	} while (!eol);
      57  	return line;
      58  }