]> git.ipfire.org Git - pakfire.git/commitdiff
arch: Dynamically create an array with the supported arches
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 9 Oct 2024 17:32:59 +0000 (17:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 9 Oct 2024 17:35:42 +0000 (17:35 +0000)
Clang did not like the previous solution.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/arch.c

index 03b1cf9ecc82ed0d87185d476de1b1fce3b21b72..5ea79271ddc8ff5b1e7bba2cad332993f645501a 100644 (file)
@@ -91,22 +91,35 @@ PAKFIRE_EXPORT int pakfire_arch_supported(const char* name) {
        return 0;
 }
 
+static size_t pakfire_num_arches(void) {
+       size_t i = 0;
+
+       for (const struct pakfire_arch* arch = PAKFIRE_ARCHES; arch->name; arch++)
+               i++;
+
+       return i;
+}
+
 PAKFIRE_EXPORT const char** pakfire_supported_arches(void) {
-       static const char* supported_arches[] = {
-               // x86_64
-               PAKFIRE_ARCHES[0].name,
+       const struct pakfire_arch* arch = NULL;
+       static const char** arches = NULL;
 
-               // aarch64
-               PAKFIRE_ARCHES[1].name,
+       if (!arches) {
+               // Count how many architectures we have
+               const size_t num_arches = pakfire_num_arches();
+               unsigned int i = 0;
 
-               // riscv64
-               PAKFIRE_ARCHES[2].name,
+               // Allocate a new array
+               arches = calloc(num_arches + 1, sizeof(*arches));
+               if (!arches)
+                       return NULL;
 
-               // Sentinel
-               NULL,
-       };
+               // Copy all architectures
+               for (arch = PAKFIRE_ARCHES; arch->name; arch++)
+                       arches[i++] = arch->name;
+       }
 
-       return supported_arches;
+       return arches;
 }
 
 const char* pakfire_arch_platform(const char* name) {