diff options
Diffstat (limited to 'host/lib/host_misc.c')
-rw-r--r-- | host/lib/host_misc.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/host/lib/host_misc.c b/host/lib/host_misc.c index 91eaea25..f8dd9b8f 100644 --- a/host/lib/host_misc.c +++ b/host/lib/host_misc.c @@ -9,6 +9,7 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include "host_common.h" @@ -18,6 +19,13 @@ #include "vboot_common.h" +char* StrCopy(char* dest, const char* src, int dest_size) { + strncpy(dest, src, dest_size); + dest[dest_size - 1] = '\0'; + return dest; +} + + uint8_t* ReadFile(const char* filename, uint64_t* size) { FILE* f; uint8_t* buf; @@ -50,6 +58,45 @@ uint8_t* ReadFile(const char* filename, uint64_t* size) { } +char* ReadFileString(char* dest, int size, const char* filename) { + char* got; + FILE* f; + + f = fopen(filename, "rt"); + if (!f) + return NULL; + + got = fgets(dest, size, f); + fclose(f); + return got; +} + + +int ReadFileInt(const char* filename) { + char buf[64]; + int value; + char* e = NULL; + + if (!ReadFileString(buf, sizeof(buf), filename)) + return -1; + + /* Convert to integer. Allow characters after the int ("123 blah"). */ + value = strtol(buf, &e, 0); + if (e == buf) + return -1; /* No characters consumed, so conversion failed */ + + return value; +} + + +int ReadFileBit(const char* filename, int bitmask) { + int value = ReadFileInt(filename); + if (value == -1) + return -1; + else return (value & bitmask ? 1 : 0); +} + + int WriteFile(const char* filename, const void *data, uint64_t size) { FILE *f = fopen(filename, "wb"); if (!f) { |