[PATCH v3 RESEND 2/2] usbip: apply list options regardless of order

Jason Colapietro <[email protected]>
Newsgroups org.kernel.vger.linux-usb,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
usbip_list() acts on -r, -l and -d as soon as getopt_long() returns them,
so any option parsed afterwards is never seen. -p therefore only takes
effect when it precedes the mode selector: "usbip list -p -r <host>" is
parsable while "usbip list -r <host> -p" is not, and -l and -d behave the
same way. Nothing in the usage text suggests the order matters.

Record the requested mode during the option loop and run it once parsing
has finished, so -p applies wherever it appears on the command line. When
several mode selectors are given the first one still wins, as before.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Jason Colapietro <[email protected]>
---
 tools/usb/usbip/src/usbip_list.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/tools/usb/usbip/src/usbip_list.c b/tools/usb/usbip/src/usbip_list.c
index b9d60b87e..632a968a0 100644
--- a/tools/usb/usbip/src/usbip_list.c
+++ b/tools/usb/usbip/src/usbip_list.c
@@ -343,6 +343,8 @@ int usbip_list(int argc, char *argv[])
 	};
 
 	bool parsable = false;
+	char *remote_host = NULL;
+	int action = 0;
 	int opt;
 	int ret = -1;
 
@@ -360,19 +362,36 @@ int usbip_list(int argc, char *argv[])
 			parsable = true;
 			break;
 		case 'r':
-			ret = list_exported_devices(optarg, parsable);
-			goto out;
+			if (!action) {
+				action = opt;
+				remote_host = optarg;
+			}
+			break;
 		case 'l':
-			ret = list_devices(parsable);
-			goto out;
 		case 'd':
-			ret = list_gadget_devices(parsable);
-			goto out;
+			if (!action)
+				action = opt;
+			break;
 		default:
 			goto err_out;
 		}
 	}
 
+	switch (action) {
+	case 'r':
+		ret = list_exported_devices(remote_host, parsable);
+		break;
+	case 'l':
+		ret = list_devices(parsable);
+		break;
+	case 'd':
+		ret = list_gadget_devices(parsable);
+		break;
+	default:
+		goto err_out;
+	}
+	goto out;
+
 err_out:
 	usbip_list_usage();
 out:
-- 
2.50.1 (Apple Git-155)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.