Add two new files - abstraction-common.[ch] - and within them add
a function - cgroup_strtol() - that can convert a string to a long.

Signed-off-by: Tom Hromatka <tom.hroma...@oracle.com>
---
 src/Makefile.am          |  2 +-
 src/abstraction-common.c | 66 ++++++++++++++++++++++++++++++++++++++++
 src/abstraction-common.h | 50 ++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100644 src/abstraction-common.c
 create mode 100644 src/abstraction-common.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 18398e2b3367..f1e671b5da59 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,7 +15,7 @@ CLEANFILES = lex.c parse.c parse.h
 
 AM_CPPFLAGS = -I$(top_srcdir)/include
 lib_LTLIBRARIES = libcgroup.la libcgroupfortesting.la
-libcgroup_la_SOURCES = parse.h parse.y lex.l api.c config.c 
libcgroup-internal.h libcgroup.map wrapper.c log.c
+libcgroup_la_SOURCES = parse.h parse.y lex.l api.c config.c 
libcgroup-internal.h libcgroup.map wrapper.c log.c abstraction-common.c 
abstraction-common.h
 libcgroup_la_LIBADD = -lpthread $(CODE_COVERAGE_LIBS)
 libcgroup_la_CFLAGS = $(CODE_COVERAGE_CFLAGS) -DSTATIC=static
 libcgroup_la_LDFLAGS = -Wl,--version-script,$(srcdir)/libcgroup.map \
diff --git a/src/abstraction-common.c b/src/abstraction-common.c
new file mode 100644
index 000000000000..b70a122b9f19
--- /dev/null
+++ b/src/abstraction-common.c
@@ -0,0 +1,66 @@
+/**
+ * Libcgroup abstraction layer
+ *
+ * Copyright (c) 2021 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hroma...@oracle.com>
+ */
+
+/*
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This library 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses>.
+ */
+
+#include <libcgroup.h>
+#include <libcgroup-internal.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "abstraction-common.h"
+
+int cgroup_strtol(const char * const in_str, int base,
+                 long int * const out_value)
+{
+       char *endptr = NULL;
+       int ret = 0;
+
+       if (out_value == NULL) {
+               cgroup_err("Error: Invalid parameter to %s\n", __func__);
+               ret = ECGINVAL;
+               goto out;
+       }
+
+       *out_value = strtol(in_str, &endptr, base);
+
+       /* taken directly from strtol's man page */
+       if ((errno == ERANGE &&
+            (*out_value == LONG_MAX || *out_value == LONG_MIN))
+           || (errno != 0 && *out_value == 0)) {
+               cgroup_err("Error: Failed to convert %s from strtol: %s\n",
+                          in_str);
+               ret = ECGFAIL;
+               goto out;
+       }
+
+       if (endptr == in_str) {
+               cgroup_err("Error: No long value found in %s\n",
+                          in_str);
+               ret = ECGFAIL;
+               goto out;
+       }
+
+out:
+       return ret;
+}
diff --git a/src/abstraction-common.h b/src/abstraction-common.h
new file mode 100644
index 000000000000..12807f4cddaf
--- /dev/null
+++ b/src/abstraction-common.h
@@ -0,0 +1,50 @@
+/**
+ * Libcgroup abstraction layer prototypes and structs
+ *
+ * Copyright (c) 2021 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hroma...@oracle.com>
+ */
+
+/*
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This library 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses>.
+ */
+#ifndef __ABSTRACTION_COMMON
+#define __ABSTRACTION_COMMON
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "config.h"
+#include <libcgroup.h>
+#include "libcgroup-internal.h"
+
+/**
+ * Convert a string to a long
+ *
+ * @param in_str String to be converted
+ * @param base Integer base
+ * @param out_value Pointer to hold the output long value
+ *
+ * @return 0 on success,
+ *        ECGFAIL if the conversion to long failed,
+ *        ECGINVAL upon an invalid parameter
+ */
+int cgroup_strtol(const char * const in_str, int base,
+                 long int * const out_value);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* __ABSTRACTION_COMMON */
-- 
2.25.1



_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to