From: Michael Tremer Date: Fri, 9 Jun 2023 12:40:55 +0000 (+0000) Subject: networkctl: Move describe into an own function X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8dc40ed861b5ee93f176caf792fc818f681fd9b4;p=network.git networkctl: Move describe into an own function Signed-off-by: Michael Tremer --- diff --git a/src/networkctl/port.c b/src/networkctl/port.c index 67bc0037..b6a95c55 100644 --- a/src/networkctl/port.c +++ b/src/networkctl/port.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include @@ -80,22 +81,19 @@ ERROR: return r; } -// Dump - -static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) { +static int networkctl_port_describe(sd_bus* bus, const char* name, char** text) { sd_bus_message* reply = NULL; sd_bus_error error = SD_BUS_ERROR_NULL; char path[PATH_MAX]; - const char* text = NULL; + const char* t = NULL; int r; - if (argc < 1) { - fprintf(stderr, "Port required\n"); + // Check input + if (!name || !text) return -EINVAL; - } // Make port path - r = nw_string_format(path, "/org/ipfire/network1/port/%s", argv[0]); + r = nw_string_format(path, "/org/ipfire/network1/port/%s", name); if (r < 0) goto ERROR; @@ -108,15 +106,16 @@ static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) { } // Read the text - r = sd_bus_message_read(reply, "s", &text); + r = sd_bus_message_read(reply, "s", &t); if (r < 0) { fprintf(stderr, "Could not parse bus message: %s\n", strerror(-r)); goto ERROR; } - // Print the text - if (text) - printf("%s\n", text); + // Copy text to heap + *text = strdup(t); + if (!*text) + r = -errno; ERROR: if (reply) @@ -125,6 +124,31 @@ ERROR: return r; } +// Dump + +static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) { + char* text = NULL; + int r; + + if (argc < 1) { + fprintf(stderr, "Port required\n"); + return -EINVAL; + } + + // Describe the port + r = networkctl_port_describe(bus, argv[0], &text); + if (r < 0) + return r; + + // Print the text + printf("%s\n", text); + + if (text) + free(text); + + return 0; +} + // List static int __networkctl_port_list(sd_bus* bus, const char* path, const char* name, void* data) {