[PATCH 1/3] Filter device name in hciconfig.

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

 



No anymore work someting like "hciconfig tty1", "hciconfig qfg1" or
"hciconfig hci1hg" as "hci1".
---
 lib/hci.c         |   12 +++++++++++-
 tools/hciconfig.c |   30 +++++++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/lib/hci.c b/lib/hci.c
index 3304daa..64c7dad 100644
--- a/lib/hci.c
+++ b/lib/hci.c
@@ -54,6 +54,16 @@ typedef struct {
 	unsigned int val;
 } hci_map;
 
+static int is_number(const char *c)
+{
+	while(*c) {
+		if (! isdigit(*c))
+			return 0;
+		++c;
+	}
+	return 1;
+}
+
 static char *hci_bit2str(hci_map *m, unsigned int val)
 {
 	char *str = malloc(120);
@@ -889,7 +899,7 @@ int hci_devid(const char *str)
 	bdaddr_t ba;
 	int id = -1;
 
-	if (!strncmp(str, "hci", 3) && strlen(str) >= 4) {
+	if (!strncmp(str, "hci", 3) && strlen(str) >= 4 && is_number(str+3)) {
 		id = atoi(str + 3);
 		if (hci_devba(id, &ba) < 0)
 			return -1;
diff --git a/tools/hciconfig.c b/tools/hciconfig.c
index 3627b7c..e8b0c69 100644
--- a/tools/hciconfig.c
+++ b/tools/hciconfig.c
@@ -1721,6 +1721,33 @@ static void print_dev_info(int ctl, struct hci_dev_info *di)
 	printf("\n");
 }
 
+static int is_number(const char *c)
+{
+	while(*c) {
+		if (! isdigit(*c))
+			return 0;
+		++c;
+	}
+	return 1;
+}
+
+static int dev_name_filter(char *dev_name)
+{
+	int ret;
+
+	if ( (strlen(dev_name) >= 4) && (!strncmp(dev_name, "hci", 3)) &&
+			(is_number(dev_name+3)) )
+		ret = atoi(dev_name+3);
+	else if (is_number(dev_name))
+		ret = atoi(dev_name);
+	else {
+		fprintf(stderr, "No valid device name\n");
+		exit(1);
+	}
+
+	return ret;
+}
+
 static struct {
 	char *cmd;
 	void (*func)(int ctl, int hdev, char *opt);
@@ -1824,7 +1851,8 @@ int main(int argc, char *argv[])
 		exit(0);
 	}
 
-	di.dev_id = atoi(argv[0] + 3);
+	di.dev_id = dev_name_filter(argv[0]);
+
 	argc--; argv++;
 
 	if (ioctl(ctl, HCIGETDEVINFO, (void *) &di)) {
-- 
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux