diff mbox series

[i2c-tools,2/2] i2cset: printing help is not an error

Message ID 20240811093411.5694-2-wsa+renesas@sang-engineering.com
State Accepted
Headers show
Series [i2c-tools,1/2] i2cget: printing help is not an error | expand

Commit Message

Wolfram Sang Aug. 11, 2024, 9:34 a.m. UTC
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(-)

Comments

Wolfram Sang Aug. 14, 2024, 6:04 p.m. UTC | #1
On Sun, Aug 11, 2024 at 11:34:11AM +0200, Wolfram Sang wrote:
> 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>

Pushed to master.
diff mbox series

Patch

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);
 		}
 	}