diff mbox

[v3,2/6] Define some utility functions for using the fwts_architecture enum

Message ID 1453163175-5801-3-git-send-email-al.stone@linaro.org
State New
Headers show

Commit Message

Al Stone Jan. 19, 2016, 12:26 a.m. UTC
Add in some helper functions to make it easier to use the enum for
fwts_architecture.

Signed-off-by: Al Stone <al.stone@linaro.org>

---
 src/lib/include/fwts_arch.h |  4 +++
 src/lib/src/Makefile.am     |  1 +
 src/lib/src/fwts_arch.c     | 88 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+)
 create mode 100644 src/lib/src/fwts_arch.c

-- 
2.5.0
diff mbox

Patch

diff --git a/src/lib/include/fwts_arch.h b/src/lib/include/fwts_arch.h
index 3fc03fc..a950ea7 100644
--- a/src/lib/include/fwts_arch.h
+++ b/src/lib/include/fwts_arch.h
@@ -34,4 +34,8 @@  typedef enum {
 	FWTS_ARCH_OTHER
 } fwts_architecture;
 
+extern fwts_architecture fwts_arch_get_host(void);
+extern fwts_architecture fwts_arch_get_arch(const char *name);
+extern const char *fwts_arch_names(void);
+
 #endif
diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am
index c16f129..5d63804 100644
--- a/src/lib/src/Makefile.am
+++ b/src/lib/src/Makefile.am
@@ -28,6 +28,7 @@  libfwts_la_SOURCES = 		\
 	fwts_acpi.c 		\
 	fwts_acpid.c 		\
 	fwts_alloc.c 		\
+	fwts_arch.c 		\
 	fwts_args.c 		\
 	fwts_backtrace.c	\
 	fwts_battery.c 		\
diff --git a/src/lib/src/fwts_arch.c b/src/lib/src/fwts_arch.c
new file mode 100644
index 0000000..558458c
--- /dev/null
+++ b/src/lib/src/fwts_arch.c
@@ -0,0 +1,84 @@ 
+/*
+ * Copyright (C) 2016, Al Stone <ahs3@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <stdlib.h>
+#include <sys/utsname.h>
+
+#include "fwts.h"
+
+struct fwts_arch_info {
+	fwts_architecture arch;
+	char *name;
+};
+
+static const struct fwts_arch_info arch_info[] = {
+	{ FWTS_ARCH_X86, "x86" },
+	{ FWTS_ARCH_X86, "x86_32" },
+	{ FWTS_ARCH_X86, "x86_64" },
+	{ FWTS_ARCH_IA64, "ia64" },
+	{ FWTS_ARCH_ARM64, "arm64" },
+	{ FWTS_ARCH_ARM64, "aarch64" },
+	{ FWTS_ARCH_OTHER, "other" }
+};
+
+static char *arch_names;
+
+static fwts_architecture __fwts_arch_get_arch(const char *name)
+{
+	const struct fwts_arch_info *ptr;
+
+	for (ptr = arch_info; ptr->arch != FWTS_ARCH_OTHER; ptr++)
+		if (!strcmp(ptr->name, name))
+			return ptr->arch;
+
+	return FWTS_ARCH_OTHER;
+}
+
+fwts_architecture fwts_arch_get_host(void)
+{
+	struct utsname buf;
+
+	if (uname(&buf))
+		return FWTS_ARCH_OTHER;
+
+	return __fwts_arch_get_arch(buf.machine);
+}
+
+fwts_architecture fwts_arch_get_arch(const char *name)
+{
+	return __fwts_arch_get_arch(name);
+}
+
+const char *fwts_arch_names(void)
+{
+	const struct fwts_arch_info *ptr;
+	size_t len;
+
+	if (arch_names)
+		return arch_names;
+
+	for (ptr = arch_info, len = 0; ptr->arch != FWTS_ARCH_OTHER; ptr++)
+		len += strlen(ptr->name) + 1;
+
+	arch_names = calloc(len, 1);
+	if (arch_names) {
+		for (ptr = arch_info; ptr->arch != FWTS_ARCH_OTHER; ptr++) {
+			strcat(arch_names, ptr->name);
+			strcat(arch_names, " ");
+		}
+	}
+
+	return arch_names;
+}