diff options
author | Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> | 2010-04-22 16:52:29 +0200 |
---|---|---|
committer | Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> | 2010-04-22 16:52:29 +0200 |
commit | 8e0608d31d988333ff04f3faaa6e851c0ecdbc6e (patch) | |
tree | b042fc732d7c784d298ed42496f88a2f164f413c /lib/utils.c | |
parent | 1e6e8bd9a62aa7cd72e13db9355badc96df18ee8 (diff) |
Fourth stage to ipset-5
Add new userspace files: include/, lib/ and plus new files in src/.
Diffstat (limited to 'lib/utils.c')
-rw-r--r-- | lib/utils.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/utils.c b/lib/utils.c new file mode 100644 index 0000000..bddeb87 --- /dev/null +++ b/lib/utils.c @@ -0,0 +1,103 @@ +/* Copyright 2007-20010 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ +#include <assert.h> /* assert */ +#include <stdbool.h> /* bool */ +#include <stdlib.h> /* malloc, free */ +#include <string.h> /* memset, str* */ + +#include <libipset/session.h> /* ipset_err */ +#include <libipset/utils.h> /* prototypes */ + +/** + * ipset_strchr - locate character(s) in string + * @str: string to locate the character(s) in + * @sep: string of characters to locate + * + * Return a pointer to the first occurence of any of the + * characters to be located in the string. NULL is returned + * if no character is found. + */ +char * +ipset_strchr(const char *str, const char *sep) +{ + char *match; + + assert(str); + assert(sep); + + for (; *sep != '\0'; sep++) + if ((match = strchr(str, (int)sep[0])) != NULL) + return match; + + return NULL; +} + +/** + * ipset_name_match - match a string against an array of strings + * @arg: string + * @name: array of strings, last one is a NULL pointer + * + * Return true if arg matches any of the strings in the array. + */ +bool +ipset_name_match(const char *arg, const char * const name[]) +{ + int i = 0; + + assert(arg); + assert(name); + + while (name[i]) { + if (STREQ(arg, name[i])) + return true; + i++; + } + + return false; +} + +/** + * ipset_shift_argv - shift off an argument + * @arc: argument count + * @argv: array of argument strings + * @from: from where shift off an argument + * + * Shift off the argument at "from" from the array of + * arguments argv of size argc. + */ +void +ipset_shift_argv(int *argc, char *argv[], int from) +{ + int i; + + assert(*argc >= from + 1); + + for (i = from + 1; i <= *argc; i++) { + argv[i-1] = argv[i]; + } + (*argc)--; + return; +} + +/** + * ipset_strncpy - copy the string from src to dst + * @dst: the target string buffer + * @src: the source string buffer + * @len: the length of bytes to copy, including the terminating null byte. + * + * Copy the string from src to destination, but at most len bytes are + * copied. The target is unconditionally terminated by the null byte. + */ +void +ipset_strncpy(char *dst, const char *src, size_t len) +{ + assert(dst); + assert(src); + + strncpy(dst, src, len); + dst[len - 1] = '\0'; +} |