[BACK]Return to next_line.c CVS log [TXT][DIR] Up to [Development] / xfs-cmds / attr / libmisc

File: [Development] / xfs-cmds / attr / libmisc / next_line.c (download)

Revision 1.2, Tue Feb 21 14:32:36 2006 UTC (11 years, 8 months ago) by nathans.longdrop.melbourne.sgi.com
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +4 -1 lines

Fix builds for Debian folks using GNU/Hurd, thanks to Barry deFreese.
Merge of master-melb:xfs-cmds:25256a by kenmcd.

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include "misc.h"

#define LINE_SIZE getpagesize()

char *next_line(FILE *file)
{
	static char *line;
	static size_t line_size;
	char *c;
	int eol = 0;

	if (!line) {
		if (high_water_alloc((void **)&line, &line_size, LINE_SIZE))
			return NULL;
	}
	c = line;
	do {
		if (!fgets(c, line_size - (c - line), file))
			return NULL;
		c = strrchr(c, '\0');
		while (c > line && (*(c-1) == '\n' || *(c-1) == '\r')) {
			c--;
			*c = '\0';
			eol = 1;
		}
		if (feof(file))
			break;
		if (!eol) {
			if (high_water_alloc((void **)&line, &line_size,
					     2 * line_size))
				return NULL;
			c = strrchr(line, '\0');
		}
	} while (!eol);
	return line;
}