Files
sunnypilot/selfdrive/common/util.c
Vehicle Researcher 5de48a7668 openpilot v0.3.0-devel release
old-commit-hash: 4653a9aef0
2017-05-11 12:41:17 -07:00

28 lines
485 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void* read_file(const char* path, size_t* out_len) {
FILE* f = fopen(path, "r");
if (!f) {
return NULL;
}
fseek(f, 0, SEEK_END);
long f_len = ftell(f);
rewind(f);
char* buf = malloc(f_len + 1);
assert(buf);
memset(buf, 0, f_len + 1);
size_t num_read = fread(buf, f_len, 1, f);
assert(num_read == 1);
fclose(f);
if (out_len) {
*out_len = f_len + 1;
}
return buf;
}