]> git.ipfire.org Git - network.git/commitdiff
networkctl: Move describe into an own function
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 12:40:55 +0000 (12:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 12:40:55 +0000 (12:40 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkctl/port.c

index 67bc003730b08c34e863032530453660796b03fe..b6a95c55265c148c65dd525e2e98984576036558 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <limits.h>
+#include <stdlib.h>
 
 #include <systemd/sd-bus.h>
 
@@ -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) {