[stgt] [PATCH 3/3] str_to_val macro in tgtadm.c

Alexander Nezhinsky alexandern at mellanox.com
Thu Nov 17 17:33:42 CET 2011


Uses new macro str_to val() in tgtadm.c to check command line arguments
validity.

Signed-off-by: Alexander Nezhinsky <alexandern at mellanox.com>
---
 usr/tgtadm.c |   49 ++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/usr/tgtadm.c b/usr/tgtadm.c
index 398398b..1165fac 100644
--- a/usr/tgtadm.c
+++ b/usr/tgtadm.c
@@ -122,6 +122,23 @@ struct option const long_options[] = {
 
 static char *short_options = "dhVL:o:m:t:s:c:l:n:v:b:E:f:y:T:I:Q:u:p:H:F:P:B:Y:O:C:";
 
+static char *opt_long_name(int val)
+{
+	int i;
+	static char opt_name[64];
+
+	for (i = 0; i < ARRAY_SIZE(long_options); i++) {
+		if (long_options[i].val == val) {
+			snprintf(opt_name, sizeof(opt_name)-1,
+				 "option --%s (-%c)",
+				 long_options[i].name, val);
+			return opt_name;
+		}
+	}
+	sprintf(opt_name, "option -%c", val);
+	return opt_name;
+}
+
 static void usage(int status)
 {
 	if (status != 0)
@@ -485,20 +502,28 @@ int main(int argc, char **argv)
 			mode = str_to_mode(optarg);
 			break;
 		case 't':
-			tid = strtol(optarg, NULL, 10);
-			if (errno == EINVAL) {
-				eprintf("invalid tid '%s'\n", optarg);
+			rc = str_to_val(optarg, opt_long_name(ch),
+					tid, 0, INT_MAX);
+			if (rc)
 				exit(EINVAL);
-			}
 			break;
 		case 's':
-			sid = strtoull(optarg, NULL, 10);
+			rc = str_to_val(optarg, opt_long_name(ch),
+					sid, 0, ULLONG_MAX);
+			if (rc)
+				exit(EINVAL);
 			break;
 		case 'c':
-			cid = strtoul(optarg, NULL, 10);
+			rc = str_to_val(optarg, opt_long_name(ch),
+					cid, 0, UINT_MAX);
+			if (rc)
+				exit(EINVAL);
 			break;
 		case 'l':
-			lun = strtoull(optarg, NULL, 10);
+			rc = str_to_val(optarg, opt_long_name(ch),
+					lun, 0, ULLONG_MAX);
+			if (rc)
+				exit(EINVAL);
 			break;
 		case 'P':
 			if (mode == MODE_PORTAL)
@@ -534,7 +559,10 @@ int main(int argc, char **argv)
 			hostno = bus_to_host(optarg);
 			break;
 		case 'H':
-			hostno = strtol(optarg, NULL, 10);
+			rc = str_to_val(optarg, opt_long_name(ch),
+					hostno, 0, UINT_MAX);
+			if (rc)
+				exit(EINVAL);
 			break;
 		case 'F':
 			force = 1;
@@ -555,7 +583,10 @@ int main(int argc, char **argv)
 			ac_dir = ACCOUNT_TYPE_OUTGOING;
 			break;
 		case 'C':
-			control_port = strtol(optarg, NULL, 10);
+			rc = str_to_val(optarg, opt_long_name(ch),
+					control_port, 0, USHRT_MAX);
+			if (rc)
+				exit(EINVAL);
 			break;
 		case 'V':
 			version();
--
1.7.1
--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list