update changelog
[platform/upstream/acl.git] / 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 <stdio.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <unistd.h>
22 #include "misc.h"
23
24 #define LINE_SIZE getpagesize()
25
26 char *next_line(FILE *file)
27 {
28         static char *line;
29         static size_t line_size;
30         char *c;
31         int eol = 0;
32
33         if (!line) {
34                 if (high_water_alloc((void **)&line, &line_size, LINE_SIZE))
35                         return NULL;
36         }
37         c = line;
38         do {
39                 if (!fgets(c, line_size - (c - line), file))
40                         return NULL;
41                 c = strrchr(c, '\0');
42                 while (c > line && (*(c-1) == '\n' || *(c-1) == '\r')) {
43                         c--;
44                         *c = '\0';
45                         eol = 1;
46                 }
47                 if (feof(file))
48                         break;
49                 if (!eol) {
50                         if (high_water_alloc((void **)&line, &line_size,
51                                              2 * line_size))
52                                 return NULL;
53                         c = strrchr(line, '\0');
54                 }
55         } while (!eol);
56         return line;
57 }