[OpenWrt-Devel] libubox, jshn: add option to write output to a file
diff mbox series

Message ID 20190913222248.30764-1-roman@advem.lv
State Accepted
Delegated to: John Crispin
Headers show
Series
  • [OpenWrt-Devel] libubox, jshn: add option to write output to a file
Related show

Commit Message

Roman Yeryomin Sept. 13, 2019, 10:22 p.m. UTC
This would allow board_config_flush to run one command instead
of two and would be faster and safer than redirecting output
and moving a file between filesystems.

Originally discussed here:
http://lists.openwrt.org/pipermail/openwrt-devel/2017-December/010127.html

Signed-off-by: Roman Yeryomin <roman@advem.lv>
---
 jshn.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

Comments

Roman Yeryomin Sept. 26, 2019, 2:09 p.m. UTC | #1
On 2019-09-14 01:22, Roman Yeryomin wrote:
> This would allow board_config_flush to run one command instead
> of two and would be faster and safer than redirecting output
> and moving a file between filesystems.
> 
> Originally discussed here:
> http://lists.openwrt.org/pipermail/openwrt-devel/2017-December/010127.html
> 

Anybody?
I would like to submit a patch to base files which depends on this.

Regards,
Roman

Patch
diff mbox series

diff --git a/jshn.c b/jshn.c
index 24e3265..0aa120c 100644
--- a/jshn.c
+++ b/jshn.c
@@ -275,7 +275,7 @@  out:
 	return obj;
 }
 
-static int jshn_format(bool no_newline, bool indent)
+static int jshn_format(bool no_newline, bool indent, FILE *stream)
 {
 	json_object *obj;
 	const char *output;
@@ -297,7 +297,7 @@  static int jshn_format(bool no_newline, bool indent)
 			goto out;
 		output = blobmsg_output;
 	}
-	fprintf(stdout, "%s%s", output, no_newline ? "" : "\n");
+	fprintf(stream, "%s%s", output, no_newline ? "" : "\n");
 	free(blobmsg_output);
 	ret = 0;
 
@@ -342,6 +342,7 @@  int main(int argc, char **argv)
 	int i;
 	int ch;
 	int fd;
+	FILE *fp = NULL;
 	struct stat sb;
 	char *fbuf;
 	int ret;
@@ -366,7 +367,7 @@  int main(int argc, char **argv)
 		avl_insert(&env_vars, &vars[i].avl);
 	}
 
-	while ((ch = getopt(argc, argv, "p:nir:R:w")) != -1) {
+	while ((ch = getopt(argc, argv, "p:nir:R:o:w")) != -1) {
 		switch(ch) {
 		case 'p':
 			var_prefix = optarg;
@@ -400,7 +401,16 @@  int main(int argc, char **argv)
 			close(fd);
 			return ret;
 		case 'w':
-			return jshn_format(no_newline, indent);
+			return jshn_format(no_newline, indent, stdout);
+		case 'o':
+			fp = fopen(optarg, "w");
+			if (!fp) {
+				fprintf(stderr, "Error opening %s\n", optarg);
+				return 3;
+			}
+			jshn_format(no_newline, indent, fp);
+			fclose(fp);
+			return 0;
 		case 'n':
 			no_newline = true;
 			break;