aboutsummaryrefslogtreecommitdiff
path: root/getln.c
blob: 74c981b441d485c5aa256c7fc26fb2d829771ec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "getln.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static char *getln_held = NULL;

char *getln(FILE *stream)
{
	size_t size;
	char *buf;
	/* Recover held input from previous run. */
	if (getln_held == NULL) {
		size = 1024;
		buf = malloc(1024);
	} else {
		size = 1024 + strlen(getln_held);
		buf = realloc(getln_held, size);
	}
	if (buf == NULL) {
		perror("getln");
		return NULL;
	}
	memset(buf + size - 1024, 0, 1024);
	getln_held = NULL;
	/* Read until either end of file or end of line. */
	for (;;) {
		if (fgets(buf + size - 1024, 1023, stream) == NULL)
			break;
		if (strchr(buf, '\n') != NULL)
			break;
		/* Line is longer than 1024 bytes, realloc to make space. */
		size += 1024;
		char *new_buf = realloc(buf, size);
		if (new_buf == NULL) {
			perror("getln");
			free(buf);
			return NULL;
		}
		buf = new_buf;
		memset(buf + size - 1024, 0, 1024);
	}
	char *endl = strchr(buf, '\n');
	if (endl == NULL || endl == buf) {
		if (buf[0] == '\0') {
			free(buf);
			return NULL;
		}
		return buf;
	}
	size_t endl_size = strlen(endl + 1);
	if (endl_size > 0) {
		getln_held = malloc(endl_size + 1);
		if (getln_held == NULL) {
			perror("getln");
			free(buf);
			return NULL;
		}
		strcpy(getln_held, endl);
		endl[1] = '\0';
	}
	return buf;
}

void getln_cleanup(void)
{
	if (getln_held != NULL) {
		free(getln_held);
		getln_held = NULL;
	}
}