diff options
author | Daniel Borkmann <dborkman@redhat.com> | 2013-06-04 00:04:15 +0200 |
---|---|---|
committer | Daniel Borkmann <dborkman@redhat.com> | 2013-06-04 00:04:15 +0200 |
commit | 9e4cf71371187927a6eb7cc4fe32b67d58a93010 (patch) | |
tree | 063ebd26cfc7b50212a2c8069cf702fb9b4b8708 /str.c | |
parent | 8bb67e4e60d1c0d9fba66c1b28e62e4d92d8d4ce (diff) |
xutils: break out string handling and locking
Break out all string handling functions and lockme stuff in order
to further eliminate the big code blob in xutils, so that it can
be easier maintained.
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Diffstat (limited to 'str.c')
-rw-r--r-- | str.c | 88 |
1 files changed, 88 insertions, 0 deletions
@@ -0,0 +1,88 @@ +/* + * netsniff-ng - the packet sniffing beast + * Copyright 2009, 2010 Daniel Borkmann. + * Subject to the GPL, version 2. + */ + +#include <stdio.h> +#include <string.h> +#include <stdarg.h> + +#include "str.h" + +size_t strlcpy(char *dest, const char *src, size_t size) +{ + size_t ret = strlen(src); + + if (size) { + size_t len = (ret >= size) ? size - 1 : ret; + + memcpy(dest, src, len); + dest[len] = '\0'; + } + + return ret; +} + +static inline int vslprintf(char *dst, size_t size, const char *fmt, va_list ap) +{ + int ret; + + ret = vsnprintf(dst, size, fmt, ap); + dst[size - 1] = '\0'; + + return ret; +} + +int slprintf(char *dst, size_t size, const char *fmt, ...) +{ + int ret; + va_list ap; + + va_start(ap, fmt); + ret = vslprintf(dst, size, fmt, ap); + va_end(ap); + + return ret; +} + +int slprintf_nocheck(char *dst, size_t size, const char *fmt, ...) +{ + int ret; + va_list ap; + + va_start(ap, fmt); + ret = vslprintf(dst, size, fmt, ap); + va_end(ap); + + return ret; +} + +noinline void *xmemset(void *s, int c, size_t n) +{ + size_t i; + uint8_t *ptr = s; + + for (i = 0; i < n; ++i) + ptr[i] = (uint8_t) c; + + return ptr; +} + +char *strtrim_right(char *p, char c) +{ + char *end; + size_t len; + + len = strlen(p); + while (*p && len) { + end = p + len - 1; + if (c == *end) + *end = 0; + else + break; + len = strlen(p); + } + + return p; +} |