From f3c8225a35115b1a2d4419bde9d4b763233e59c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20H=C3=B6glund?= <jonas.hoglund@embeddednation.com> Date: Thu, 1 Apr 2021 13:01:22 +0200 Subject: [PATCH] Read input file into buffer. --- dumpimage.c | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/dumpimage.c b/dumpimage.c index 6fdc4cd..9f42d20 100644 --- a/dumpimage.c +++ b/dumpimage.c @@ -23,18 +23,57 @@ #include <stdio.h> #include <stdint.h> #include <stdlib.h> +#include <stdarg.h> #include <string.h> #include <stdbool.h> +#include <sys/stat.h> #include <libfdt.h> #include <fdt.h> +#define PRINTF(i, j) __attribute__((format (printf, i, j))) +#define NORETURN __attribute__((noreturn)) + +static inline void NORETURN PRINTF(1, 2) die(const char *str, ...) +{ + va_list ap; + + va_start(ap, str); + fprintf(stderr, "ERROR: "); + vfprintf(stderr, str, ap); + va_end(ap); + exit(1); +} + int main(int argc, char *argv[]) { - printf("dumpimage\n"); + const char *file; + FILE *f; + struct stat st; + int size, ret; + char *buf; + + + if (argc < 2) + die("missing filename.\n"); + + file = argv[1]; + + f = fopen(file, "r"); + if (!f) + die("could not open file\n"); + + stat(file, &st); + size = st.st_size; + + buf = malloc(size); + if (!buf) + die("Could not allocate buffer.\n"); + + ret = fread(buf, 1, size, f); + if (ret < size) + die("Could not read file.\n"); - if (argc < 2) { - fprintf(stderr, "Error: missing filename"); - return -1; - } + free(buf); + fclose(f); } -- GitLab