summaryrefslogtreecommitdiff
path: root/src/creole_util_main.c
blob: 7d29c68c99d9e94d7cc3f295c5fb06662325cc56 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "creole.h"

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define CHUNK_SIZE 128

int read_file(const char *file_path, char **out_buffer, size_t *out_length) {
	assert(out_buffer != NULL && *out_buffer == NULL);
	assert(file_path != NULL);

	FILE *fp = fopen(file_path, "r");
	if (fp == NULL) {
		return -1;
	}

	char *buffer = NULL;
	size_t allocated = 0;
	size_t used = 0;
	while (true) {
		// Grow buffer, if needed.
		if (used + CHUNK_SIZE > allocated) {
			// Grow exponentially to guarantee O(log(n)) performance.
			allocated = (allocated == 0) ? CHUNK_SIZE : allocated * 2;

			// Overflow check. Some ANSI C compilers may optimize this away, though.
			if (allocated <= used) {
				free(buffer);
				fclose(fp);
				errno = EOVERFLOW;
				return -1;
			}

			char *temp = realloc(buffer, allocated);
			if (temp == NULL) {
				int old_errno = errno;
				free(buffer); // free() may not set errno
				fclose(fp);   // fclose() may set errno
				errno = old_errno;
				return -1;
			}
			buffer = temp;
		}

		size_t nread = fread(buffer + used, 1, CHUNK_SIZE, fp);
		if (nread == 0) {
			// End-of-file or errnor has occured.
			// FIXME: Should we be checking (nread < CHUNK_SIZE)?
			//        https://stackoverflow.com/a/39322170
			break;
		}
		used += nread;
	}

	if (ferror(fp)) {
		int old_errno = errno;
		free(buffer); // free() may not set errno
		fclose(fp);   // fclose() may set errno
		errno = old_errno;
		return -1;
	}

	// Reallocate to optimal size.
	char *temp = realloc(buffer, used + 1);
	if (temp == NULL) {
		int old_errno = errno;
		free(buffer); // free() may not set errno
		fclose(fp);   // fclose() may set errno
		errno = old_errno;
		return -1;
	}
	buffer = temp;

        // Null-terminate the buffer. Note that buffers may still contain \0,
	// so strlen(buffer) == length may not be true.
        buffer[used] = '\0';

	// Return buffer.
	*out_buffer = buffer;
	if (out_length != NULL) {
		*out_length = used;
	}
	fclose(fp);
	return 0;
}

int main(void) {
	size_t buffer_length = 0;
	char *buffer = NULL;
	if (read_file("/dev/stdin", &buffer, &buffer_length) < 0) {
		perror("Failed to read stdin");
		return EXIT_FAILURE;
	}

	render_creole(stdout, buffer, buffer_length);

        // The lack of return value makes it painfully obvious that we aren't
        // handling errors at all. This represents my half-hearted attempt to fix that.
	if (ferror(stdout)) {
		perror("Failed to write to stdout");
		return EXIT_FAILURE;
	}

        return EXIT_SUCCESS;
}