From: Wolfram Sang <wsa+renesas@sang-engineering.com>
To: linux-i2c@vger.kernel.org
Cc: Wolfram Sang <wsa+renesas@sang-engineering.com>
Subject: [PATCH i2c-tools 2/2] i2cset: printing help is not an error
Date: Sun, 11 Aug 2024 11:34:11 +0200 [thread overview]
Message-ID: <20240811093411.5694-2-wsa+renesas@sang-engineering.com> (raw)
In-Reply-To: <20240811093411.5694-1-wsa+renesas@sang-engineering.com>
The check distinguishing the 'h' and '?' parameters is dead code. Found
by cppcheck:
i2cset.c:177:4: warning: Statements following noreturn function 'help()' will never be executed. [unreachableCode]
Add a parameter to help saying how it should exit. Remove a forward
declaration while here.
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
tools/i2cset.c | 41 +++++++++++++++++++----------------------
1 file changed, 19 insertions(+), 22 deletions(-)
diff --git a/tools/i2cset.c b/tools/i2cset.c
index dd4fdc2..aaf7faf 100644
--- a/tools/i2cset.c
+++ b/tools/i2cset.c
@@ -28,9 +28,7 @@
#include "util.h"
#include "../version.h"
-static void help(void) __attribute__ ((noreturn));
-
-static void help(void)
+static void __attribute__ ((noreturn)) help(int status)
{
fprintf(stderr,
"Usage: i2cset [-f] [-y] [-m MASK] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]\n"
@@ -43,7 +41,7 @@ static void help(void)
" i (I2C block data)\n"
" s (SMBus block data)\n"
" Append p for SMBus PEC\n");
- exit(1);
+ exit(status);
}
static int check_funcs(int file, int size, int pec)
@@ -173,8 +171,7 @@ int main(int argc, char *argv[])
case 'a': all_addrs = 1; break;
case 'h':
case '?':
- help();
- exit(opt == '?');
+ help(opt == '?');
}
}
@@ -184,20 +181,20 @@ int main(int argc, char *argv[])
}
if (argc < optind + 3)
- help();
+ help(1);
i2cbus = lookup_i2c_bus(argv[optind]);
if (i2cbus < 0)
- help();
+ help(1);
address = parse_i2c_address(argv[optind+1], all_addrs);
if (address < 0)
- help();
+ help(1);
daddress = strtol(argv[optind+2], &end, 0);
if (*end || daddress < 0 || daddress > 0xff) {
fprintf(stderr, "Error: Data address invalid!\n");
- help();
+ help(1);
}
/* check for command/mode */
@@ -218,7 +215,7 @@ int main(int argc, char *argv[])
if (strlen(argv[argc-1]) > 2
|| (strlen(argv[argc-1]) == 2 && argv[argc-1][1] != 'p')) {
fprintf(stderr, "Error: Invalid mode '%s'!\n", argv[argc-1]);
- help();
+ help(1);
}
switch (argv[argc-1][0]) {
case 'b': size = I2C_SMBUS_BYTE_DATA; break;
@@ -227,25 +224,25 @@ int main(int argc, char *argv[])
case 'i': size = I2C_SMBUS_I2C_BLOCK_DATA; break;
default:
fprintf(stderr, "Error: Invalid mode '%s'!\n", argv[argc-1]);
- help();
+ help(1);
}
pec = argv[argc-1][1] == 'p';
if (size == I2C_SMBUS_BLOCK_DATA || size == I2C_SMBUS_I2C_BLOCK_DATA) {
if (pec && size == I2C_SMBUS_I2C_BLOCK_DATA) {
fprintf(stderr, "Error: PEC not supported for I2C block writes!\n");
- help();
+ help(1);
}
if (maskp) {
fprintf(stderr, "Error: Mask not supported for block writes!\n");
- help();
+ help(1);
}
if (argc > (int)sizeof(block) + optind + 4) {
fprintf(stderr, "Error: Too many arguments!\n");
- help();
+ help(1);
}
} else if (argc != optind + 5) {
fprintf(stderr, "Error: Too many arguments!\n");
- help();
+ help(1);
}
}
@@ -262,12 +259,12 @@ int main(int argc, char *argv[])
value = strtol(argv[optind+3], &end, 0);
if (*end || value < 0) {
fprintf(stderr, "Error: Data value invalid!\n");
- help();
+ help(1);
}
if ((size == I2C_SMBUS_BYTE_DATA && value > 0xff)
|| (size == I2C_SMBUS_WORD_DATA && value > 0xffff)) {
fprintf(stderr, "Error: Data value out of range!\n");
- help();
+ help(1);
}
break;
case I2C_SMBUS_BLOCK_DATA:
@@ -276,11 +273,11 @@ int main(int argc, char *argv[])
value = strtol(argv[optind + len + 3], &end, 0);
if (*end || value < 0) {
fprintf(stderr, "Error: Data value invalid!\n");
- help();
+ help(1);
}
if (value > 0xff) {
fprintf(stderr, "Error: Data value out of range!\n");
- help();
+ help(1);
}
block[len] = value;
}
@@ -295,12 +292,12 @@ int main(int argc, char *argv[])
vmask = strtol(maskp, &end, 0);
if (*end || vmask == 0) {
fprintf(stderr, "Error: Data value mask invalid!\n");
- help();
+ help(1);
}
if (((size == I2C_SMBUS_BYTE || size == I2C_SMBUS_BYTE_DATA)
&& vmask > 0xff) || vmask > 0xffff) {
fprintf(stderr, "Error: Data value mask out of range!\n");
- help();
+ help(1);
}
}
--
2.43.0
next prev parent reply other threads:[~2024-08-11 9:34 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-11 9:34 [PATCH i2c-tools 1/2] i2cget: printing help is not an error Wolfram Sang
2024-08-11 9:34 ` Wolfram Sang [this message]
2024-08-14 18:04 ` [PATCH i2c-tools 2/2] i2cset: " Wolfram Sang
2024-08-14 18:03 ` [PATCH i2c-tools 1/2] i2cget: " Wolfram Sang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240811093411.5694-2-wsa+renesas@sang-engineering.com \
--to=wsa+renesas@sang-engineering.com \
--cc=linux-i2c@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox