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 | 60 ++++++++++++++++++++++++++++++++++++++++ src/abstraction-common.h | 44 +++++++++++++++++++++++++++++ 3 files changed, 105 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 9de482231d78..b2e8f02f0146 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,7 +15,7 @@ CLEANFILES = lex.c parse.c parse.h INCLUDES = -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..8c57f1a07d8d --- /dev/null +++ b/src/abstraction-common.c @@ -0,0 +1,60 @@ +/** + * 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; + int ret = 0; + + *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..03275412a9e8 --- /dev/null +++ b/src/abstraction-common.h @@ -0,0 +1,44 @@ +/** + * 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 + +__BEGIN_DECLS + +#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 + * + * @return 0 on success, ECGFAIL if the conversion to long failed + */ +int cgroup_strtol(const char * const in_str, int base, + long int * const out_value); + +__END_DECLS + +#endif /* __ABSTRACTION_COMMON */ -- 2.26.2 _______________________________________________ Libcg-devel mailing list Libcg-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/libcg-devel