Thread (9 messages) flat view 9 messages, 2 authors, 4d ago
COOLING4d

[PATCH iproute2-next 3/7] misc: put help to stdout and usage to stderr

From: Stephen Hemminger <stephen@networkplumber.org>
Date: 2026-08-07 15:43:13
Subsystem: the rest · Maintainer: Linus Torvalds

Similar change to other iproute2 commands.
Syntax error should print to stderr and exit with non-zero.
Help command should print to stdout and exit with zero status.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 misc/arpd.c   | 12 +++++++-----
 misc/ifstat.c | 44 ++++++++++++++++++++++----------------------
 misc/nstat.c  | 11 ++++++-----
 misc/rtacct.c | 13 ++++++-------
 4 files changed, 41 insertions(+), 39 deletions(-)
diff --git a/misc/arpd.c b/misc/arpd.c
index a42603f6..230d3580 100644
--- a/misc/arpd.c
+++ b/misc/arpd.c
@@ -6,6 +6,7 @@
  */
 
 #include <stdio.h>
+#include <stdbool.h>
 #include <syslog.h>
 #include <malloc.h>
 #include <string.h>
@@ -88,11 +89,12 @@ int broadcast_rate = 1000;
 int broadcast_burst = 3000;
 int poll_timeout = 30000;
 
-static void usage(void)
+static void usage(bool help)
 {
-	fprintf(stderr,
+	fprintf(help ? stdout : stderr,
 		"Usage: arpd [ -lkh? ] [ -a N ] [ -b dbase ] [ -B number ] [ -f file ] [ -n time ] [-p interval ] [ -R rate ] [ interfaces ]\n");
-	exit(1);
+
+	exit(help ? 0 : 1);
 }
 
 static int handle_if(int ifindex)
@@ -594,7 +596,7 @@ int main(int argc, char **argv)
 		case 'f':
 			if (do_load) {
 				fprintf(stderr, "Duplicate option -f\n");
-				usage();
+				usage(true);
 			}
 			do_load = optarg;
 			break;
@@ -633,7 +635,7 @@ int main(int argc, char **argv)
 		case 'h':
 		case '?':
 		default:
-			usage();
+			usage(opt == 'h');
 		}
 	}
 	argc -= optind;
diff --git a/misc/ifstat.c b/misc/ifstat.c
index ec59a9eb..c2ea5942 100644
--- a/misc/ifstat.c
+++ b/misc/ifstat.c
@@ -756,8 +756,8 @@ static int verify_forging(int fd)
 static void xstat_usage(void)
 {
 	fprintf(stderr,
-"Usage: ifstat supported xstats:\n"
-"       cpu_hits       Counts only packets that went via the CPU.\n");
+		"Usage: ifstat supported xstats:\n"
+		"       cpu_hits       Counts only packets that went via the CPU.\n");
 }
 
 struct extended_stats_options_t {
@@ -796,27 +796,27 @@ static const char *get_filter_type(const char *name)
 	return NULL;
 }
 
-static void usage(void) __attribute__((noreturn));
+static void usage(bool help) __attribute__((noreturn));
 
-static void usage(void)
+static void usage(bool help)
 {
-	fprintf(stderr,
-"Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
-"   -h, --help           this message\n"
-"   -a, --ignore         ignore history\n"
-"   -d, --scan=SECS      sample every statistics every SECS\n"
-"   -e, --errors         show errors\n"
-"   -j, --json           format output in JSON\n"
-"   -n, --nooutput       do history only\n"
-"   -p, --pretty         pretty print\n"
-"   -r, --reset          reset history\n"
-"   -s, --noupdate       don't update history\n"
-"   -t, --interval=SECS  report average over the last SECS\n"
-"   -V, --version        output version information\n"
-"   -z, --zeros          show entries with zero activity\n"
-"   -x, --extended=TYPE  show extended stats of TYPE\n");
-
-	exit(-1);
+	fprintf(help ? stdout : stderr,
+		"Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
+		"   -h, --help           this message\n"
+		"   -a, --ignore         ignore history\n"
+		"   -d, --scan=SECS      sample every statistics every SECS\n"
+		"   -e, --errors         show errors\n"
+		"   -j, --json           format output in JSON\n"
+		"   -n, --nooutput       do history only\n"
+		"   -p, --pretty         pretty print\n"
+		"   -r, --reset          reset history\n"
+		"   -s, --noupdate       don't update history\n"
+		"   -t, --interval=SECS  report average over the last SECS\n"
+		"   -V, --version        output version information\n"
+		"   -z, --zeros          show entries with zero activity\n"
+		"   -x, --extended=TYPE  show extended stats of TYPE\n");
+
+	exit(help ? 0 : 1);
 }
 
 static const struct option longopts[] = {
@@ -898,7 +898,7 @@ int main(int argc, char *argv[])
 		case 'h':
 		case '?':
 		default:
-			usage();
+			usage(ch == 'h');
 		}
 	}
 
diff --git a/misc/nstat.c b/misc/nstat.c
index 4a9f3326..cd822ba2 100644
--- a/misc/nstat.c
+++ b/misc/nstat.c
@@ -7,6 +7,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <string.h>
@@ -528,11 +529,11 @@ static int verify_forging(int fd)
 	return -1;
 }
 
-static void usage(void) __attribute__((noreturn));
+static void usage(bool help) __attribute__((noreturn));
 
-static void usage(void)
+static void usage(bool help)
 {
-	fprintf(stderr,
+	fprintf(help ? stdout : stderr,
 		"Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
 		"   -h, --help          this message\n"
 		"   -a, --ignore        ignore history\n"
@@ -545,7 +546,7 @@ static void usage(void)
 		"   -t, --interval=SECS report average over the last SECS\n"
 		"   -V, --version       output version information\n"
 		"   -z, --zeros         show entries with zero activity\n");
-	exit(-1);
+	exit(help ? 0 : 1);
 }
 
 static const struct option longopts[] = {
@@ -612,7 +613,7 @@ int main(int argc, char *argv[])
 		case 'h':
 		case '?':
 		default:
-			usage();
+			usage(ch == 'h');
 		}
 	}
 
diff --git a/misc/rtacct.c b/misc/rtacct.c
index cd84b7f0..0e2c03af 100644
--- a/misc/rtacct.c
+++ b/misc/rtacct.c
@@ -412,14 +412,13 @@ static int verify_forging(int fd)
 	return -1;
 }
 
-static void usage(void) __attribute__((noreturn));
+static void usage(bool help) __attribute__((noreturn));
 
-static void usage(void)
+static void usage(bool help)
 {
-	fprintf(stderr,
-"Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
-		);
-	exit(-1);
+	fprintf(help ? stdout : stderr,
+		"Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n");
+	exit(help ? 0 : 1);
 }
 
 int main(int argc, char *argv[])
@@ -469,7 +468,7 @@ int main(int argc, char *argv[])
 		case 'h':
 		case '?':
 		default:
-			usage();
+			usage(ch == 'h');
 		}
 	}
 
-- 
2.53.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help