linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [i2c-tools PATCH] tools: i2cbusses: Handle bus names like /dev/i2c-0
@ 2021-05-25  9:06 Chris Packham
  2021-05-26  7:39 ` Jean Delvare
  0 siblings, 1 reply; 5+ messages in thread
From: Chris Packham @ 2021-05-25  9:06 UTC (permalink / raw)
  To: jdelvare, wsa; +Cc: linux-i2c, Chris Packham

File based tab completion means it's easy to do something like
i2cdump /dev/i2c-0 0x52. Accept this method of specifying the i2c bus
device.

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
 tools/i2cbusses.c | 12 ++++++++++--
 tools/i2cbusses.h |  1 +
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/tools/i2cbusses.c b/tools/i2cbusses.c
index b4f00ae..5cb6e93 100644
--- a/tools/i2cbusses.c
+++ b/tools/i2cbusses.c
@@ -25,6 +25,7 @@
 /* For strdup and snprintf */
 #define _BSD_SOURCE 1 /* for glibc <= 2.19 */
 #define _DEFAULT_SOURCE 1 /* for glibc >= 2.19 */
+#define _GNU_SOURCE 1 /* for asprintf */
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -104,8 +105,10 @@ void free_adapters(struct i2c_adap *adapters)
 {
 	int i;
 
-	for (i = 0; adapters[i].name; i++)
+	for (i = 0; adapters[i].name; i++) {
 		free(adapters[i].name);
+		free(adapters[i].devname);
+	}
 	free(adapters);
 }
 
@@ -306,6 +309,10 @@ found:
 				free_adapters(adapters);
 				return NULL;
 			}
+			if (asprintf(&adapters[count].devname, "/dev/i2c-%d", i2cbus) < 0) {
+				free_adapters(adapters);
+				return NULL;
+			}
 			adapters[count].funcs = adap_types[type].funcs;
 			adapters[count].algo = adap_types[type].algo;
 			count++;
@@ -331,7 +338,8 @@ static int lookup_i2c_bus_by_name(const char *bus_name)
 	/* Walk the list of i2c busses, looking for the one with the
 	   right name */
 	for (i = 0; adapters[i].name; i++) {
-		if (strcmp(adapters[i].name, bus_name) == 0) {
+		if (strcmp(adapters[i].name, bus_name) == 0 ||
+		    strcmp(adapters[i].devname, bus_name) == 0) {
 			if (i2cbus >= 0) {
 				fprintf(stderr,
 					"Error: I2C bus name is not unique!\n");
diff --git a/tools/i2cbusses.h b/tools/i2cbusses.h
index a192c7f..77e1b8e 100644
--- a/tools/i2cbusses.h
+++ b/tools/i2cbusses.h
@@ -27,6 +27,7 @@
 struct i2c_adap {
 	int nr;
 	char *name;
+	char *devname;
 	const char *funcs;
 	const char *algo;
 };
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-06-03 22:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-25  9:06 [i2c-tools PATCH] tools: i2cbusses: Handle bus names like /dev/i2c-0 Chris Packham
2021-05-26  7:39 ` Jean Delvare
2021-05-26 21:23   ` Chris Packham
2021-06-02  7:25     ` Jean Delvare
2021-06-03 22:57       ` Chris Packham

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).