Update on readme
[transsip.git] / src / strlcpy.c
blobf7e46172a0121cb8003fc76014a65d3eca8d4bf5
1 /*
2 * transsip - the telephony network
3 * By Daniel Borkmann <daniel@transsip.org>
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
7 * strlcpy: Copyright (C) 1991, 1992 Linus Torvalds, GPL, version 2
8 */
10 #define _BSD_SOURCE
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdarg.h>
15 #include "strlcpy.h"
16 #include "xmalloc.h"
17 #include "die.h"
19 size_t strlcpy(char *dest, const char *src, size_t size)
21 size_t ret = strlen(src);
22 if (size) {
23 size_t len = (ret >= size) ? size - 1 : ret;
24 memcpy(dest, src, len);
25 dest[len] = '\0';
27 return ret;
30 int slprintf(char *dst, size_t size, const char *fmt, ...)
32 int ret;
33 va_list ap;
34 va_start(ap, fmt);
35 ret = vsnprintf(dst, size, fmt, ap);
36 dst[size - 1] = '\0';
37 va_end(ap);
38 return ret;
41 char **strntoargv(char *str, size_t len, int *argc)
43 int done = 0;
44 char **argv = NULL;
45 if (argc == NULL)
46 panic("argc is null!\n");
47 *argc = 0;
48 if (len <= 1) /* '\0' */
49 goto out;
50 while (!done) {
51 while (len > 0 && *str == ' ') {
52 len--;
53 str++;
55 if (len > 0 && *str != '\0') {
56 (*argc)++;
57 argv = xrealloc(argv, 1, sizeof(char *) * (*argc));
58 argv[(*argc) - 1] = str;
59 while (len > 0 && *str != ' ') {
60 len--;
61 str++;
63 if (len > 0 && *str == ' ') {
64 len--;
65 *str = '\0';
66 str++;
68 } else {
69 done = 1;
72 out:
73 return argv;