]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/lib/string.c
tools lib: Fix builds when glibc contains strlcpy()
[mirror_ubuntu-bionic-kernel.git] / tools / lib / string.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
7d85c434
WN
2/*
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
4ddd3274
ACM
16#include <stdlib.h>
17#include <string.h>
7d85c434 18#include <errno.h>
4ddd3274 19#include <linux/string.h>
ce990917 20#include <linux/compiler.h>
4ddd3274
ACM
21
22/**
23 * memdup - duplicate region of memory
24 *
25 * @src: memory region to duplicate
26 * @len: memory region length
27 */
28void *memdup(const void *src, size_t len)
29{
30 void *p = malloc(len);
31
32 if (p)
33 memcpy(p, src, len);
34
35 return p;
36}
7d85c434
WN
37
38/**
39 * strtobool - convert common user inputs into boolean values
40 * @s: input string
41 * @res: result
42 *
b99e4850
ACM
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.
7d85c434
WN
46 */
47int strtobool(const char *s, bool *res)
48{
b99e4850
ACM
49 if (!s)
50 return -EINVAL;
51
7d85c434
WN
52 switch (s[0]) {
53 case 'y':
54 case 'Y':
55 case '1':
56 *res = true;
b99e4850 57 return 0;
7d85c434
WN
58 case 'n':
59 case 'N':
60 case '0':
61 *res = false;
b99e4850
ACM
62 return 0;
63 case 'o':
64 case 'O':
65 switch (s[1]) {
66 case 'n':
67 case 'N':
68 *res = true;
69 return 0;
70 case 'f':
71 case 'F':
72 *res = false;
73 return 0;
74 default:
75 break;
76 }
7d85c434 77 default:
b99e4850 78 break;
7d85c434 79 }
b99e4850
ACM
80
81 return -EINVAL;
7d85c434 82}
ce990917
JP
83
84/**
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
89 *
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.
94 *
95 * If libc has strlcpy() then that version will override this
96 * implementation:
97 */
319f2d6f
VC
98#ifdef __clang__
99#pragma clang diagnostic push
100#pragma clang diagnostic ignored "-Wignored-attributes"
101#endif
ce990917
JP
102size_t __weak strlcpy(char *dest, const char *src, size_t size)
103{
104 size_t ret = strlen(src);
105
106 if (size) {
107 size_t len = (ret >= size) ? size - 1 : ret;
108 memcpy(dest, src, len);
109 dest[len] = '\0';
110 }
111 return ret;
112}
319f2d6f
VC
113#ifdef __clang__
114#pragma clang diagnostic pop
115#endif