Skip to contentSkip to conversation

i2cdetect: Sort the bus list by number

PATCH In review Jean Delvare 2 participants 2 messages May 27, 2021 – May 28, 2021 I2C subsystem host drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2cdetect: Sort the bus list by number

The bus list (option -l) will be easier to read once sorted, as typically this will group the buses by driver.

---
Why did I not do that earlier... I don't know.

 tools/i2cbusses.c |   11 +++++++++++
 1 file changed, 11 insertions(+)
tools/i2cbusses.c +11 -0
@@ -129,6 +129,14 @@ static struct i2c_adap *more_adapters(st
 	return new_adapters;
 }
 
+static int sort_i2c_busses(const void *a, const void *b)
+{
+	const struct i2c_adap *adap1 = a;
+	const struct i2c_adap *adap2 = b;
+
+	return adap1->nr - adap2->nr;
+}
+
 struct i2c_adap *gather_i2c_busses(void)
 {
 	char s[120];
@@ -314,6 +322,9 @@ struct i2c_adap *gather_i2c_busses(void)
 	closedir(dir);
 
 done:
+	/* Sort by bus number for convenience */
+	qsort(adapters, count, sizeof(struct i2c_adap), sort_i2c_busses);
+
 	return adapters;
 }
 
Signature · 2 lines
Jean Delvare
SUSE L3 Support