From eda5918d0f2554b38d526746cf77ed21e31f6fc7 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 20 Apr 2008 17:27:34 +0000 Subject: [PATCH] Refactor the handling of the I2CBUS parameter. This ensures more consistency accross the i2c tools. git-svn-id: http://lm-sensors.org/svn/i2c-tools/trunk@5193 7894878c-1315-0410-8ee3-d5d059ff63e0 --- tools/i2cbusses.c | 24 ++++++++++++++++++++++++ tools/i2cbusses.h | 1 + tools/i2cdetect.c | 11 ++--------- tools/i2cdump.c | 10 ++-------- tools/i2cget.c | 6 ++---- tools/i2cset.c | 6 ++---- 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/tools/i2cbusses.c b/tools/i2cbusses.c index 1c96b46..6222787 100644 --- a/tools/i2cbusses.c +++ b/tools/i2cbusses.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -217,6 +218,29 @@ done: "and also modprobed your i2c bus drivers\n"); } +/* + * Parse an I2CBUS command line argument and return the corresponding + * bus number, or a negative value if the bus is invalid. + */ +int lookup_i2c_bus(const char *i2cbus_arg) +{ + long i2cbus; + char *end; + + i2cbus = strtol(i2cbus_arg, &end, 0); + if (*end || !*i2cbus_arg) { + fprintf(stderr, "Error: I2CBUS argument not a number!\n"); + return -1; + } + if (i2cbus < 0 || i2cbus > 0xff) { + fprintf(stderr, "Error: I2CBUS argument out of range " + "(0-255)!\n"); + return -2; + } + + return i2cbus; +} + int open_i2c_dev(const int i2cbus, char *filename, const int quiet) { int file; diff --git a/tools/i2cbusses.h b/tools/i2cbusses.h index 932ce9a..625f930 100644 --- a/tools/i2cbusses.h +++ b/tools/i2cbusses.h @@ -24,6 +24,7 @@ void print_i2c_busses(int procfmt); +int lookup_i2c_bus(const char *i2cbus_arg); int open_i2c_dev(const int i2cbus, char *filename, const int quiet); int set_slave_addr(int file, int address, int force); diff --git a/tools/i2cdetect.c b/tools/i2cdetect.c index cf19c27..c039b20 100644 --- a/tools/i2cdetect.c +++ b/tools/i2cdetect.c @@ -235,15 +235,8 @@ int main(int argc, char *argv[]) help(); exit(1); } - i2cbus = strtol(argv[flags+1], &end, 0); - if (*end) { - fprintf(stderr, "Error: I2CBUS argument not a number!\n"); - help(); - exit(1); - } - if ((i2cbus < 0) || (i2cbus > 0xff)) { - fprintf(stderr, "Error: I2CBUS argument out of range " - "(0-255)!\n"); + i2cbus = lookup_i2c_bus(argv[flags+1]); + if (i2cbus < 0) { help(); exit(1); } diff --git a/tools/i2cdump.c b/tools/i2cdump.c index 5ece0af..90551b6 100644 --- a/tools/i2cdump.c +++ b/tools/i2cdump.c @@ -91,14 +91,8 @@ int main(int argc, char *argv[]) help(); exit(1); } - i2cbus = strtol(argv[flags+1], &end, 0); - if (*end) { - fprintf(stderr, "Error: First argument not a number!\n"); - help(); - exit(1); - } - if (i2cbus < 0 || i2cbus > 0xff) { - fprintf(stderr, "Error: I2CBUS argument out of range!\n"); + i2cbus = lookup_i2c_bus(argv[flags+1]); + if (i2cbus < 0) { help(); exit(1); } diff --git a/tools/i2cget.c b/tools/i2cget.c index 3a3ff67..3611ec7 100644 --- a/tools/i2cget.c +++ b/tools/i2cget.c @@ -187,11 +187,9 @@ int main(int argc, char *argv[]) if (argc - flags < 3) help(); - i2cbus = strtol(argv[flags+1], &end, 0); - if (*end || i2cbus < 0 || i2cbus > 0x3f) { - fprintf(stderr, "Error: I2CBUS argument invalid!\n"); + i2cbus = lookup_i2c_bus(argv[flags+1]); + if (i2cbus < 0) help(); - } address = strtol(argv[flags+2], &end, 0); if (*end || address < 3 || address > 0x77) { diff --git a/tools/i2cset.c b/tools/i2cset.c index 6a09b5d..cd8e4e6 100644 --- a/tools/i2cset.c +++ b/tools/i2cset.c @@ -78,11 +78,9 @@ int main(int argc, char *argv[]) if (argc < flags + 5) help(); - i2cbus = strtol(argv[flags+1], &end, 0); - if (*end || i2cbus < 0 || i2cbus > 0x3f) { - fprintf(stderr, "Error: I2CBUS argument invalid!\n"); + i2cbus = lookup_i2c_bus(argv[flags+1]); + if (i2cbus < 0) help(); - } address = strtol(argv[flags+2], &end, 0); if (*end || address < 0 || address > 0x7f) {