1 // SPDX-License-Identifier: GPL-2.0
3 * linux/tools/lib/string.c
5 * Copied from linux/lib/string.c, where it is:
7 * Copyright (C) 1991, 1992 Linus Torvalds
9 * More specifically, the first copied function was strtobool, which
12 * d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents")
13 * Author: Jonathan Cameron <jic23@cam.ac.uk>
19 #include <linux/string.h>
20 #include <linux/compiler.h>
23 * memdup - duplicate region of memory
25 * @src: memory region to duplicate
26 * @len: memory region length
28 void *memdup(const void *src
, size_t len
)
30 void *p
= malloc(len
);
39 * strtobool - convert common user inputs into boolean values
43 * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
44 * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
45 * pointed to by res is updated upon finding a match.
47 int strtobool(const char *s
, bool *res
)
85 * strlcpy - Copy a C-string into a sized buffer
86 * @dest: Where to copy the string to
87 * @src: Where to copy the string from
88 * @size: size of destination buffer
90 * Compatible with *BSD: the result is always a valid
91 * NUL-terminated string that fits in the buffer (unless,
92 * of course, the buffer size is zero). It does not pad
93 * out the result like strncpy() does.
95 * If libc has strlcpy() then that version will override this
98 size_t __weak
strlcpy(char *dest
, const char *src
, size_t size
)
100 size_t ret
= strlen(src
);
103 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
104 memcpy(dest
, src
, len
);