blob: 50d400822bb386af66ae00a7a010d6568e18ee20 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Wang Nan7d85c432015-11-16 11:42:05 -03002/*
3 * linux/tools/lib/string.c
4 *
5 * Copied from linux/lib/string.c, where it is:
6 *
7 * Copyright (C) 1991, 1992 Linus Torvalds
8 *
9 * More specifically, the first copied function was strtobool, which
10 * was introduced by:
11 *
12 * d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents")
13 * Author: Jonathan Cameron <jic23@cam.ac.uk>
14 */
15
Arnaldo Carvalho de Melo4ddd3272015-11-16 11:36:29 -030016#include <stdlib.h>
17#include <string.h>
Wang Nan7d85c432015-11-16 11:42:05 -030018#include <errno.h>
Arnaldo Carvalho de Melo4ddd3272015-11-16 11:36:29 -030019#include <linux/string.h>
Arnaldo Carvalho de Melo7bd330d2019-06-25 21:23:18 -030020#include <linux/ctype.h>
Josh Poimboeufce990912015-12-15 09:39:33 -060021#include <linux/compiler.h>
Arnaldo Carvalho de Melo4ddd3272015-11-16 11:36:29 -030022
23/**
24 * memdup - duplicate region of memory
25 *
26 * @src: memory region to duplicate
27 * @len: memory region length
28 */
29void *memdup(const void *src, size_t len)
30{
31 void *p = malloc(len);
32
33 if (p)
34 memcpy(p, src, len);
35
36 return p;
37}
Wang Nan7d85c432015-11-16 11:42:05 -030038
39/**
40 * strtobool - convert common user inputs into boolean values
41 * @s: input string
42 * @res: result
43 *
Arnaldo Carvalho de Melob99e4852017-07-20 15:35:33 -030044 * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
45 * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
46 * pointed to by res is updated upon finding a match.
Wang Nan7d85c432015-11-16 11:42:05 -030047 */
48int strtobool(const char *s, bool *res)
49{
Arnaldo Carvalho de Melob99e4852017-07-20 15:35:33 -030050 if (!s)
51 return -EINVAL;
52
Wang Nan7d85c432015-11-16 11:42:05 -030053 switch (s[0]) {
54 case 'y':
55 case 'Y':
56 case '1':
57 *res = true;
Arnaldo Carvalho de Melob99e4852017-07-20 15:35:33 -030058 return 0;
Wang Nan7d85c432015-11-16 11:42:05 -030059 case 'n':
60 case 'N':
61 case '0':
62 *res = false;
Arnaldo Carvalho de Melob99e4852017-07-20 15:35:33 -030063 return 0;
64 case 'o':
65 case 'O':
66 switch (s[1]) {
67 case 'n':
68 case 'N':
69 *res = true;
70 return 0;
71 case 'f':
72 case 'F':
73 *res = false;
74 return 0;
75 default:
76 break;
77 }
Wang Nan7d85c432015-11-16 11:42:05 -030078 default:
Arnaldo Carvalho de Melob99e4852017-07-20 15:35:33 -030079 break;
Wang Nan7d85c432015-11-16 11:42:05 -030080 }
Arnaldo Carvalho de Melob99e4852017-07-20 15:35:33 -030081
82 return -EINVAL;
Wang Nan7d85c432015-11-16 11:42:05 -030083}
Josh Poimboeufce990912015-12-15 09:39:33 -060084
85/**
86 * strlcpy - Copy a C-string into a sized buffer
87 * @dest: Where to copy the string to
88 * @src: Where to copy the string from
89 * @size: size of destination buffer
90 *
91 * Compatible with *BSD: the result is always a valid
92 * NUL-terminated string that fits in the buffer (unless,
93 * of course, the buffer size is zero). It does not pad
94 * out the result like strncpy() does.
95 *
96 * If libc has strlcpy() then that version will override this
97 * implementation:
98 */
99size_t __weak strlcpy(char *dest, const char *src, size_t size)
100{
101 size_t ret = strlen(src);
102
103 if (size) {
104 size_t len = (ret >= size) ? size - 1 : ret;
105 memcpy(dest, src, len);
106 dest[len] = '\0';
107 }
108 return ret;
109}
Arnaldo Carvalho de Melo7bd330d2019-06-25 21:23:18 -0300110
111/**
112 * skip_spaces - Removes leading whitespace from @str.
113 * @str: The string to be stripped.
114 *
115 * Returns a pointer to the first non-whitespace character in @str.
116 */
117char *skip_spaces(const char *str)
118{
119 while (isspace(*str))
120 ++str;
121 return (char *)str;
122}