0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <stdlib.h>
0017 #include <string.h>
0018 #include <errno.h>
0019 #include <linux/string.h>
0020 #include <linux/ctype.h>
0021 #include <linux/compiler.h>
0022
0023
0024
0025
0026
0027
0028
0029 void *memdup(const void *src, size_t len)
0030 {
0031 void *p = malloc(len);
0032
0033 if (p)
0034 memcpy(p, src, len);
0035
0036 return p;
0037 }
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 int strtobool(const char *s, bool *res)
0049 {
0050 if (!s)
0051 return -EINVAL;
0052
0053 switch (s[0]) {
0054 case 'y':
0055 case 'Y':
0056 case '1':
0057 *res = true;
0058 return 0;
0059 case 'n':
0060 case 'N':
0061 case '0':
0062 *res = false;
0063 return 0;
0064 case 'o':
0065 case 'O':
0066 switch (s[1]) {
0067 case 'n':
0068 case 'N':
0069 *res = true;
0070 return 0;
0071 case 'f':
0072 case 'F':
0073 *res = false;
0074 return 0;
0075 default:
0076 break;
0077 }
0078 default:
0079 break;
0080 }
0081
0082 return -EINVAL;
0083 }
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099 #ifdef __clang__
0100 #pragma clang diagnostic push
0101 #pragma clang diagnostic ignored "-Wignored-attributes"
0102 #endif
0103 size_t __weak strlcpy(char *dest, const char *src, size_t size)
0104 {
0105 size_t ret = strlen(src);
0106
0107 if (size) {
0108 size_t len = (ret >= size) ? size - 1 : ret;
0109 memcpy(dest, src, len);
0110 dest[len] = '\0';
0111 }
0112 return ret;
0113 }
0114 #ifdef __clang__
0115 #pragma clang diagnostic pop
0116 #endif
0117
0118
0119
0120
0121
0122
0123
0124 char *skip_spaces(const char *str)
0125 {
0126 while (isspace(*str))
0127 ++str;
0128 return (char *)str;
0129 }
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 char *strim(char *s)
0140 {
0141 size_t size;
0142 char *end;
0143
0144 size = strlen(s);
0145 if (!size)
0146 return s;
0147
0148 end = s + size - 1;
0149 while (end >= s && isspace(*end))
0150 end--;
0151 *(end + 1) = '\0';
0152
0153 return skip_spaces(s);
0154 }
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164 char *strreplace(char *s, char old, char new)
0165 {
0166 for (; *s; ++s)
0167 if (*s == old)
0168 *s = new;
0169 return s;
0170 }
0171
0172 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
0173 {
0174 while (bytes) {
0175 if (*start != value)
0176 return (void *)start;
0177 start++;
0178 bytes--;
0179 }
0180 return NULL;
0181 }
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 void *memchr_inv(const void *start, int c, size_t bytes)
0193 {
0194 u8 value = c;
0195 u64 value64;
0196 unsigned int words, prefix;
0197
0198 if (bytes <= 16)
0199 return check_bytes8(start, value, bytes);
0200
0201 value64 = value;
0202 value64 |= value64 << 8;
0203 value64 |= value64 << 16;
0204 value64 |= value64 << 32;
0205
0206 prefix = (unsigned long)start % 8;
0207 if (prefix) {
0208 u8 *r;
0209
0210 prefix = 8 - prefix;
0211 r = check_bytes8(start, value, prefix);
0212 if (r)
0213 return r;
0214 start += prefix;
0215 bytes -= prefix;
0216 }
0217
0218 words = bytes / 8;
0219
0220 while (words) {
0221 if (*(u64 *)start != value64)
0222 return check_bytes8(start, value, 8);
0223 start += 8;
0224 words--;
0225 }
0226
0227 return check_bytes8(start, value, bytes % 8);
0228 }