commit 03ba9c7da68c3fadb64ca32dbc9258919b7ac19a
parent f48499265ba2c6f39a1c3f35955aaa3b72ecaaa8
Author: Louis Burda <quent.burda@gmail.com>
Date: Wed, 15 Feb 2023 19:21:02 +0100
Rename and clean up
Diffstat:
M | .gitignore | | | 2 | +- |
M | Makefile | | | 9 | ++++++--- |
A | binpatch.c | | | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
D | fwpatch.c | | | 60 | ------------------------------------------------------------ |
4 files changed, 61 insertions(+), 64 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1 @@
-fwpatch
+binpatch
diff --git a/Makefile b/Makefile
@@ -1,7 +1,10 @@
CFLAGS = -g
-.PHONY: all
+all: binpatch
-all: fwpatch
+clean:
+ rm -f binpatch
-fwpatch: fwpatch.c
+binpatch: binpatch.c
+
+.PHONY: all clean
diff --git a/binpatch.c b/binpatch.c
@@ -0,0 +1,54 @@
+#include <err.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+int
+main(int argc, const char **argv)
+{
+ ssize_t addr, size;
+ char *end, *data;
+ FILE *file;
+ int ret;
+
+ if (argc != 4) {
+ printf("USAGE: fwpatch TARGET OFFSET FILE\n");
+ return 0;
+ }
+
+ addr = strtoull(argv[2], &end, 0);
+ if (*end) err(1, "strtoull %s", argv[2]);
+
+ file = fopen(argv[3], "rb");
+ if (!file) err(1, "fopen %s", argv[3]);
+
+ ret = fseek(file, 0, SEEK_END);
+ if (ret < 0) err(1, "fseek %s", argv[3]);
+
+ size = ftell(file);
+ if (size < 0) err(1, "ftell %s", argv[3]);
+
+ data = malloc(size);
+ if (!data) err(1, "malloc %lu", size);
+
+ ret = fseek(file, 0, SEEK_SET);
+ if (ret < 0) err(1, "freek %s", argv[3]);
+
+ if (fread(data, 1, size, file) != size)
+ errx(1, "fread %lu", size);
+
+ fclose(file);
+
+ file = fopen(argv[1], "r+b");
+ if (!file) err(1, "fopen %s", argv[1]);
+
+ ret = fseek(file, addr, SEEK_SET);
+ if (ret < 0) err(1, "fseek %s", argv[1]);
+
+ if (fwrite(data, 1, size, file) != size)
+ errx(1, "fwrite %lu", size);
+
+ fclose(file);
+
+ free(data);
+}
diff --git a/fwpatch.c b/fwpatch.c
@@ -1,60 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <stdint.h>
-
-const char usage[] = "USAGE: fwpatch TARGET OFFSET FILE";
-
-void
-die(const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
-
- exit(1);
-}
-
-int
-main(int argc, const char **argv)
-{
- size_t addr, size;
- FILE *file;
- char *end, *data;
-
- if (argc != 4)
- die(usage);
-
- addr = strtoull(argv[2], &end, 0);
- if (*end) die("Invalid offset: %s", argv[2]);
-
- file = fopen(argv[3], "rb");
- if (!file) die("Failed to open: %s", argv[3]);
-
- fseek(file, 0, SEEK_END);
- size = ftell(file);
-
- data = malloc(size);
- if (!data) die("Out of memory\n");
-
- fseek(file, 0, SEEK_SET);
- if (fread(data, 1, size, file) != size)
- die("Read failed\n");
-
- fclose(file);
-
- file = fopen(argv[1], "r+b");
- if (!file) die("Failed to open: %s", argv[1]);
-
- fseek(file, addr, SEEK_SET);
- if (ftell(file) != addr) die("Invalid offset\n");
-
- if (fwrite(data, 1, size, file) != size)
- die("Write failed\n");
-
- fclose(file);
-
- free(data);
-}