diff mbox series

[firmware-utils,2/2] oseama: support extracting entity to stdout

Message ID 20211126161708.9306-2-zajec5@gmail.com
State Accepted
Delegated to: Rafał Miłecki
Headers show
Series [firmware-utils,1/2] oseama: allow reading from stdin | expand

Commit Message

Rafał Miłecki Nov. 26, 2021, 4:17 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

This allows using oseama in a pipe. It's very useful for writing data to
flash using e.g.
oseama extract firmware.bin -e 0 | mtd write - firmware

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 src/oseama.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/src/oseama.c b/src/oseama.c
index d3eb9b9..f68d7d0 100644
--- a/src/oseama.c
+++ b/src/oseama.c
@@ -525,10 +525,6 @@  static int oseama_extract(int argc, char **argv) {
 		fprintf(stderr, "No entity specified\n");
 		err = -EINVAL;
 		goto out;
-	} else if (!out_path) {
-		fprintf(stderr, "No output file specified\n");
-		err = -EINVAL;
-		goto out;
 	}
 
 	seama = oseama_open(seama_path, "r");
@@ -538,11 +534,15 @@  static int oseama_extract(int argc, char **argv) {
 		goto out;
 	}
 
-	out = fopen(out_path, "w");
-	if (!out) {
-		fprintf(stderr, "Couldn't open %s\n", out_path);
-		err = -EACCES;
-		goto err_close_seama;
+	if (out_path) {
+		out = fopen(out_path, "w");
+		if (!out) {
+			fprintf(stderr, "Couldn't open %s\n", out_path);
+			err = -EACCES;
+			goto err_close_seama;
+		}
+	} else {
+		out = stdout;
 	}
 
 	bytes = fread(&hdr, 1, sizeof(hdr), seama);
@@ -558,7 +558,8 @@  static int oseama_extract(int argc, char **argv) {
 	oseama_extract_entity(seama, out);
 
 err_close_out:
-	fclose(out);
+	if (out != stdout)
+		fclose(out);
 err_close_seama:
 	oseama_close(seama);
 out: