Blame libmisc/next_line.c
|
rpm-build |
0a0c83 |
/*
|
|
rpm-build |
0a0c83 |
Copyright (C) 2009 Andreas Gruenbacher <agruen@suse.de>
|
|
rpm-build |
0a0c83 |
|
|
rpm-build |
0a0c83 |
This program is free software: you can redistribute it and/or modify it
|
|
rpm-build |
0a0c83 |
under the terms of the GNU Lesser General Public License as published by
|
|
rpm-build |
0a0c83 |
the Free Software Foundation, either version 2.1 of the License, or
|
|
rpm-build |
0a0c83 |
(at your option) any later version.
|
|
rpm-build |
0a0c83 |
|
|
rpm-build |
0a0c83 |
This program is distributed in the hope that it will be useful,
|
|
rpm-build |
0a0c83 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
rpm-build |
0a0c83 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
rpm-build |
0a0c83 |
GNU General Public License for more details.
|
|
rpm-build |
0a0c83 |
|
|
rpm-build |
0a0c83 |
You should have received a copy of the GNU General Public License
|
|
rpm-build |
0a0c83 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
rpm-build |
0a0c83 |
*/
|
|
rpm-build |
0a0c83 |
|
|
rpm-build |
0a0c83 |
#include "config.h"
|
|
rpm-build |
0a0c83 |
#include <stdio.h>
|
|
rpm-build |
0a0c83 |
#include <string.h>
|
|
rpm-build |
0a0c83 |
#include <limits.h>
|
|
rpm-build |
0a0c83 |
#include <unistd.h>
|
|
rpm-build |
0a0c83 |
#include "misc.h"
|
|
rpm-build |
0a0c83 |
|
|
rpm-build |
0a0c83 |
#define LINE_SIZE getpagesize()
|
|
rpm-build |
0a0c83 |
|
|
rpm-build |
0a0c83 |
char *__acl_next_line(FILE *file)
|
|
rpm-build |
0a0c83 |
{
|
|
rpm-build |
0a0c83 |
static char *line;
|
|
rpm-build |
0a0c83 |
static size_t line_size;
|
|
rpm-build |
0a0c83 |
char *c;
|
|
rpm-build |
0a0c83 |
int eol = 0;
|
|
rpm-build |
0a0c83 |
|
|
rpm-build |
0a0c83 |
if (!line) {
|
|
rpm-build |
0a0c83 |
if (__acl_high_water_alloc((void **)&line, &line_size, LINE_SIZE))
|
|
rpm-build |
0a0c83 |
return NULL;
|
|
rpm-build |
0a0c83 |
}
|
|
rpm-build |
0a0c83 |
c = line;
|
|
rpm-build |
0a0c83 |
do {
|
|
rpm-build |
0a0c83 |
if (!fgets(c, line_size - (c - line), file))
|
|
rpm-build |
0a0c83 |
return NULL;
|
|
rpm-build |
0a0c83 |
c = strrchr(c, '\0');
|
|
rpm-build |
0a0c83 |
while (c > line && (*(c-1) == '\n' || *(c-1) == '\r')) {
|
|
rpm-build |
0a0c83 |
c--;
|
|
rpm-build |
0a0c83 |
*c = '\0';
|
|
rpm-build |
0a0c83 |
eol = 1;
|
|
rpm-build |
0a0c83 |
}
|
|
rpm-build |
0a0c83 |
if (feof(file))
|
|
rpm-build |
0a0c83 |
break;
|
|
rpm-build |
0a0c83 |
if (!eol) {
|
|
rpm-build |
0a0c83 |
if (__acl_high_water_alloc((void **)&line, &line_size,
|
|
rpm-build |
0a0c83 |
2 * line_size))
|
|
rpm-build |
0a0c83 |
return NULL;
|
|
rpm-build |
0a0c83 |
c = strrchr(line, '\0');
|
|
rpm-build |
0a0c83 |
}
|
|
rpm-build |
0a0c83 |
} while (!eol);
|
|
rpm-build |
0a0c83 |
return line;
|
|
rpm-build |
0a0c83 |
}
|