build: move trafgen_stddef.h to stddef.h
[netsniff-ng.git] / str.c
blob7a5cb49f091690379c0ca3f21d1cd07d0b0877c7
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdarg.h>
11 #include "str.h"
13 size_t strlcpy(char *dest, const char *src, size_t size)
15 size_t ret = strlen(src);
17 if (size) {
18 size_t len = (ret >= size) ? size - 1 : ret;
20 memcpy(dest, src, len);
21 dest[len] = '\0';
24 return ret;
27 static inline int vslprintf(char *dst, size_t size, const char *fmt, va_list ap)
29 int ret;
31 ret = vsnprintf(dst, size, fmt, ap);
32 dst[size - 1] = '\0';
34 return ret;
37 int slprintf(char *dst, size_t size, const char *fmt, ...)
39 int ret;
40 va_list ap;
42 va_start(ap, fmt);
43 ret = vslprintf(dst, size, fmt, ap);
44 va_end(ap);
46 return ret;
49 int slprintf_nocheck(char *dst, size_t size, const char *fmt, ...)
51 int ret;
52 va_list ap;
54 va_start(ap, fmt);
55 ret = vslprintf(dst, size, fmt, ap);
56 va_end(ap);
58 return ret;
61 noinline void *xmemset(void *s, int c, size_t n)
63 size_t i;
64 uint8_t *ptr = s;
66 for (i = 0; i < n; ++i)
67 ptr[i] = (uint8_t) c;
69 return ptr;
72 char *strtrim_right(char *p, char c)
74 char *end;
75 size_t len;
77 len = strlen(p);
78 while (*p && len) {
79 end = p + len - 1;
80 if (c == *end)
81 *end = 0;
82 else
83 break;
84 len = strlen(p);
87 return p;