From: Arvin Schnell Date: Mon, 16 Feb 2026 15:34:04 +0000 (+0100) Subject: avoid repetitive code X-Git-Tag: v2.43-devel~76^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4bac783c9a6eac149023a22a09f73ae3ef63f286;p=thirdparty%2Futil-linux.git avoid repetitive code --- diff --git a/misc-utils/blkid.c b/misc-utils/blkid.c index 185a7afea..8d9148985 100644 --- a/misc-utils/blkid.c +++ b/misc-utils/blkid.c @@ -914,6 +914,12 @@ int main(int argc, char **argv) pretty_print_dev(NULL); } + if (!ctl.eval && ctl.output & OUTPUT_JSON) { + ul_jsonwrt_init(ctl.json_fmt, stdout, 0); + ul_jsonwrt_root_open(ctl.json_fmt); + ul_jsonwrt_array_open(ctl.json_fmt, "blkid"); + } + if (ctl.lowprobe) { /* * Low-level API @@ -929,12 +935,6 @@ int main(int argc, char **argv) if (!ctl.output && ctl.lowprobe_topology) ctl.output = OUTPUT_EXPORT_LIST; - if (ctl.output & OUTPUT_JSON) { - ul_jsonwrt_init(ctl.json_fmt, stdout, 0); - ul_jsonwrt_root_open(ctl.json_fmt); - ul_jsonwrt_array_open(ctl.json_fmt, "blkid"); - } - pr = blkid_new_probe(); if (!pr) goto exit; @@ -973,11 +973,6 @@ int main(int argc, char **argv) } blkid_free_probe(pr); - if (ctl.output & OUTPUT_JSON) { - ul_jsonwrt_array_close(ctl.json_fmt); - ul_jsonwrt_root_close(ctl.json_fmt); - } - } else if (ctl.eval) { /* * Evaluate API @@ -993,12 +988,6 @@ int main(int argc, char **argv) */ blkid_dev dev; - if (ctl.output & OUTPUT_JSON) { - ul_jsonwrt_init(ctl.json_fmt, stdout, 0); - ul_jsonwrt_root_open(ctl.json_fmt); - ul_jsonwrt_array_open(ctl.json_fmt, "blkid"); - } - if (!search_type) errx(BLKID_EXIT_OTHER, _("The lookup option requires a " @@ -1013,22 +1002,11 @@ int main(int argc, char **argv) err = 0; } - if (ctl.output & OUTPUT_JSON) { - ul_jsonwrt_array_close(ctl.json_fmt); - ul_jsonwrt_root_close(ctl.json_fmt); - } - /* If we didn't specify a single device, show all available devices */ } else if (!numdev) { blkid_dev_iterate iter; blkid_dev dev; - if (ctl.output & OUTPUT_JSON) { - ul_jsonwrt_init(ctl.json_fmt, stdout, 0); - ul_jsonwrt_root_open(ctl.json_fmt); - ul_jsonwrt_array_open(ctl.json_fmt, "blkid"); - } - blkid_probe_all(cache); iter = blkid_dev_iterate_begin(cache); @@ -1042,20 +1020,9 @@ int main(int argc, char **argv) } blkid_dev_iterate_end(iter); - if (ctl.output & OUTPUT_JSON) { - ul_jsonwrt_array_close(ctl.json_fmt); - ul_jsonwrt_root_close(ctl.json_fmt); - } - /* Add all specified devices to cache (optionally display tags) */ } else { - if (ctl.output & OUTPUT_JSON) { - ul_jsonwrt_init(ctl.json_fmt, stdout, 0); - ul_jsonwrt_root_open(ctl.json_fmt); - ul_jsonwrt_array_open(ctl.json_fmt, "blkid"); - } - for (i = 0; i < numdev; i++) { blkid_dev dev = blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL); @@ -1070,10 +1037,11 @@ int main(int argc, char **argv) } } - if (ctl.output & OUTPUT_JSON) { - ul_jsonwrt_array_close(ctl.json_fmt); - ul_jsonwrt_root_close(ctl.json_fmt); - } + } + + if (!ctl.eval && ctl.output & OUTPUT_JSON) { + ul_jsonwrt_array_close(ctl.json_fmt); + ul_jsonwrt_root_close(ctl.json_fmt); } exit: