[PATCH 1/5] mmc-utils: lsmmc: Reuse do_read_reg

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



All 3 register parsing: CID, CSD, and CSR follow the same structure -
reuse it instead of duplicating the code 3 times. No functional change.

Signed-off-by: Avri Altman <avri.altman@xxxxxxx>
---
 lsmmc.c | 59 +++++++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 40 deletions(-)

diff --git a/lsmmc.c b/lsmmc.c
index 54c3167..85779bb 100644
--- a/lsmmc.c
+++ b/lsmmc.c
@@ -2373,7 +2373,6 @@ int lsmmc_main(struct config *config, int argc, char **argv)
 {
 	int ret;
 
-	memset(config, 0, sizeof(*config));
 	config->mmc_ids = calloc(IDS_MAX, sizeof(char *));
 	config->sd_ids = calloc(IDS_MAX, sizeof(char *));
 	if (!config->mmc_ids || !config->sd_ids) {
@@ -2395,71 +2394,51 @@ void lsmmc_free(struct config *config)
 	free(config->dir);
 }
 
-int do_read_csd(int argc, char **argv)
+static int do_read_reg(int argc, char **argv, enum REG_TYPE reg)
 {
-	struct config config;
+	struct config cfg = {};
 	int ret;
 
-	if (argc != 2 && argc != 3) {
-		fprintf(stderr, "Usage: Print CSD data from <device path>.\n");
-		exit(1);
-	}
-
-	ret = lsmmc_main(&config, argc, argv);
+	ret = lsmmc_main(&cfg, argc, argv);
 	if (ret)
 		goto out;
 
-	if (config.dir)
-		ret = process_dir(&config, CSD);
+	if (cfg.dir)
+		ret = process_dir(&cfg, reg);
 
 out:
-	lsmmc_free(&config);
+	lsmmc_free(&cfg);
 
 	return ret;
+
 }
 
-int do_read_cid(int argc, char **argv)
+int do_read_csd(int argc, char **argv)
 {
-	struct config config;
-	int ret;
-
 	if (argc != 2 && argc != 3) {
-		fprintf(stderr, "Usage: Print CID data from <device path>.\n");
+		fprintf(stderr, "Usage: Print CSD data from <device path>.\n");
 		exit(1);
 	}
 
-	ret = lsmmc_main(&config, argc, argv);
-	if (ret)
-		goto out;
-
-	if (config.dir)
-		ret = process_dir(&config, CID);
+	return do_read_reg(argc, argv, CSD);
+}
 
-out:
-	lsmmc_free(&config);
+int do_read_cid(int argc, char **argv)
+{
+	if (argc != 2 && argc != 3) {
+		fprintf(stderr, "Usage: Print CID data from <device path>.\n");
+		exit(1);
+	}
 
-	return ret;
+	return do_read_reg(argc, argv, CID);
 }
 
 int do_read_scr(int argc, char **argv)
 {
-	struct config config;
-	int ret;
-
 	if (argc != 2 && argc != 3) {
 		fprintf(stderr, "Usage: Print SCR data from <device path>.\n");
 		exit(1);
 	}
 
-	ret = lsmmc_main(&config, argc, argv);
-	if (ret)
-		goto out;
-
-	if (config.dir)
-		ret = process_dir(&config, SCR);
-
-out:
-	lsmmc_free(&config);
-
-	return ret;
+	return do_read_reg(argc, argv, SCR);
 }
-- 
2.42.0




[Index of Archives]     [Linux Memonry Technology]     [Linux USB Devel]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux