2020-08-06 15:01:08 +01:00
|
|
|
/*
|
|
|
|
|
* Read file into memory
|
|
|
|
|
*
|
|
|
|
|
* Copyright Johan Malm 2020
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
#include "common/grab-file.h"
|
|
|
|
|
#include "common/buf.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
char *
|
|
|
|
|
grab_file(const char *filename)
|
2020-08-06 15:01:08 +01:00
|
|
|
{
|
|
|
|
|
char *line = NULL;
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
FILE *stream = fopen(filename, "r");
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!stream) {
|
2020-08-06 15:01:08 +01:00
|
|
|
return NULL;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-08-06 15:01:08 +01:00
|
|
|
struct buf buffer;
|
|
|
|
|
buf_init(&buffer);
|
|
|
|
|
while ((getline(&line, &len, stream) != -1)) {
|
|
|
|
|
char *p = strrchr(line, '\n');
|
2020-09-28 20:41:41 +01:00
|
|
|
if (p) {
|
2020-08-06 15:01:08 +01:00
|
|
|
*p = '\0';
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-08-06 15:01:08 +01:00
|
|
|
buf_add(&buffer, line);
|
|
|
|
|
}
|
|
|
|
|
free(line);
|
|
|
|
|
fclose(stream);
|
|
|
|
|
return (buffer.buf);
|
|
|
|
|
}
|