~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/string.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/lib/string.c (Version linux-6.11.5) and /tools/lib/string.c (Version linux-4.18.20)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  *  linux/tools/lib/string.c                        3  *  linux/tools/lib/string.c
  4  *                                                  4  *
  5  *  Copied from linux/lib/string.c, where it i      5  *  Copied from linux/lib/string.c, where it is:
  6  *                                                  6  *
  7  *  Copyright (C) 1991, 1992  Linus Torvalds        7  *  Copyright (C) 1991, 1992  Linus Torvalds
  8  *                                                  8  *
  9  *  More specifically, the first copied functi      9  *  More specifically, the first copied function was strtobool, which
 10  *  was introduced by:                             10  *  was introduced by:
 11  *                                                 11  *
 12  *  d0f1fed29e6e ("Add a strtobool function ma     12  *  d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents")
 13  *  Author: Jonathan Cameron <jic23@cam.ac.uk>     13  *  Author: Jonathan Cameron <jic23@cam.ac.uk>
 14  */                                                14  */
 15                                                    15 
 16 #include <stdlib.h>                                16 #include <stdlib.h>
 17 #include <string.h>                                17 #include <string.h>
 18 #include <errno.h>                                 18 #include <errno.h>
 19 #include <linux/string.h>                          19 #include <linux/string.h>
 20 #include <linux/ctype.h>                       << 
 21 #include <linux/compiler.h>                        20 #include <linux/compiler.h>
 22                                                    21 
 23 /**                                                22 /**
 24  * memdup - duplicate region of memory             23  * memdup - duplicate region of memory
 25  *                                                 24  *
 26  * @src: memory region to duplicate                25  * @src: memory region to duplicate
 27  * @len: memory region length                      26  * @len: memory region length
 28  */                                                27  */
 29 void *memdup(const void *src, size_t len)          28 void *memdup(const void *src, size_t len)
 30 {                                                  29 {
 31         void *p = malloc(len);                     30         void *p = malloc(len);
 32                                                    31 
 33         if (p)                                     32         if (p)
 34                 memcpy(p, src, len);               33                 memcpy(p, src, len);
 35                                                    34 
 36         return p;                                  35         return p;
 37 }                                                  36 }
 38                                                    37 
 39 /**                                                38 /**
 40  * strtobool - convert common user inputs into     39  * strtobool - convert common user inputs into boolean values
 41  * @s: input string                                40  * @s: input string
 42  * @res: result                                    41  * @res: result
 43  *                                                 42  *
 44  * This routine returns 0 iff the first charac     43  * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
 45  * [oO][NnFf] for "on" and "off". Otherwise it     44  * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL.  Value
 46  * pointed to by res is updated upon finding a     45  * pointed to by res is updated upon finding a match.
 47  */                                                46  */
 48 int strtobool(const char *s, bool *res)            47 int strtobool(const char *s, bool *res)
 49 {                                                  48 {
 50         if (!s)                                    49         if (!s)
 51                 return -EINVAL;                    50                 return -EINVAL;
 52                                                    51 
 53         switch (s[0]) {                            52         switch (s[0]) {
 54         case 'y':                                  53         case 'y':
 55         case 'Y':                                  54         case 'Y':
 56         case '1':                                  55         case '1':
 57                 *res = true;                       56                 *res = true;
 58                 return 0;                          57                 return 0;
 59         case 'n':                                  58         case 'n':
 60         case 'N':                                  59         case 'N':
 61         case '':                                   60         case '':
 62                 *res = false;                      61                 *res = false;
 63                 return 0;                          62                 return 0;
 64         case 'o':                                  63         case 'o':
 65         case 'O':                                  64         case 'O':
 66                 switch (s[1]) {                    65                 switch (s[1]) {
 67                 case 'n':                          66                 case 'n':
 68                 case 'N':                          67                 case 'N':
 69                         *res = true;               68                         *res = true;
 70                         return 0;                  69                         return 0;
 71                 case 'f':                          70                 case 'f':
 72                 case 'F':                          71                 case 'F':
 73                         *res = false;              72                         *res = false;
 74                         return 0;                  73                         return 0;
 75                 default:                           74                 default:
 76                         break;                     75                         break;
 77                 }                                  76                 }
 78         default:                                   77         default:
 79                 break;                             78                 break;
 80         }                                          79         }
 81                                                    80 
 82         return -EINVAL;                            81         return -EINVAL;
 83 }                                                  82 }
 84                                                    83 
 85 /**                                                84 /**
 86  * strlcpy - Copy a C-string into a sized buff     85  * strlcpy - Copy a C-string into a sized buffer
 87  * @dest: Where to copy the string to              86  * @dest: Where to copy the string to
 88  * @src: Where to copy the string from             87  * @src: Where to copy the string from
 89  * @size: size of destination buffer               88  * @size: size of destination buffer
 90  *                                                 89  *
 91  * Compatible with *BSD: the result is always      90  * Compatible with *BSD: the result is always a valid
 92  * NUL-terminated string that fits in the buff     91  * NUL-terminated string that fits in the buffer (unless,
 93  * of course, the buffer size is zero). It doe     92  * of course, the buffer size is zero). It does not pad
 94  * out the result like strncpy() does.             93  * out the result like strncpy() does.
 95  *                                                 94  *
 96  * If libc has strlcpy() then that version wil     95  * If libc has strlcpy() then that version will override this
 97  * implementation:                                 96  * implementation:
 98  */                                                97  */
 99 #ifdef __clang__                               << 
100 #pragma clang diagnostic push                  << 
101 #pragma clang diagnostic ignored "-Wignored-at << 
102 #endif                                         << 
103 size_t __weak strlcpy(char *dest, const char *     98 size_t __weak strlcpy(char *dest, const char *src, size_t size)
104 {                                                  99 {
105         size_t ret = strlen(src);                 100         size_t ret = strlen(src);
106                                                   101 
107         if (size) {                               102         if (size) {
108                 size_t len = (ret >= size) ? s    103                 size_t len = (ret >= size) ? size - 1 : ret;
109                 memcpy(dest, src, len);           104                 memcpy(dest, src, len);
110                 dest[len] = '\0';                 105                 dest[len] = '\0';
111         }                                         106         }
112         return ret;                               107         return ret;
113 }                                              << 
114 #ifdef __clang__                               << 
115 #pragma clang diagnostic pop                   << 
116 #endif                                         << 
117                                                << 
118 /**                                            << 
119  * skip_spaces - Removes leading whitespace fr << 
120  * @str: The string to be stripped.            << 
121  *                                             << 
122  * Returns a pointer to the first non-whitespa << 
123  */                                            << 
124 char *skip_spaces(const char *str)             << 
125 {                                              << 
126         while (isspace(*str))                  << 
127                 ++str;                         << 
128         return (char *)str;                    << 
129 }                                              << 
130                                                << 
131 /**                                            << 
132  * strim - Removes leading and trailing whites << 
133  * @s: The string to be stripped.              << 
134  *                                             << 
135  * Note that the first trailing whitespace is  << 
136  * in the given string @s. Returns a pointer t << 
137  * character in @s.                            << 
138  */                                            << 
139 char *strim(char *s)                           << 
140 {                                              << 
141         size_t size;                           << 
142         char *end;                             << 
143                                                << 
144         size = strlen(s);                      << 
145         if (!size)                             << 
146                 return s;                      << 
147                                                << 
148         end = s + size - 1;                    << 
149         while (end >= s && isspace(*end))      << 
150                 end--;                         << 
151         *(end + 1) = '\0';                     << 
152                                                << 
153         return skip_spaces(s);                 << 
154 }                                              << 
155                                                << 
156 /**                                            << 
157  * strreplace - Replace all occurrences of cha << 
158  * @s: The string to operate on.               << 
159  * @old: The character being replaced.         << 
160  * @new: The character @old is replaced with.  << 
161  *                                             << 
162  * Returns pointer to the nul byte at the end  << 
163  */                                            << 
164 char *strreplace(char *s, char old, char new)  << 
165 {                                              << 
166         for (; *s; ++s)                        << 
167                 if (*s == old)                 << 
168                         *s = new;              << 
169         return s;                              << 
170 }                                              << 
171                                                << 
172 static void *check_bytes8(const u8 *start, u8  << 
173 {                                              << 
174         while (bytes) {                        << 
175                 if (*start != value)           << 
176                         return (void *)start;  << 
177                 start++;                       << 
178                 bytes--;                       << 
179         }                                      << 
180         return NULL;                           << 
181 }                                              << 
182                                                << 
183 /**                                            << 
184  * memchr_inv - Find an unmatching character i << 
185  * @start: The memory area                     << 
186  * @c: Find a character other than c           << 
187  * @bytes: The size of the area.               << 
188  *                                             << 
189  * returns the address of the first character  << 
190  * if the whole buffer contains just @c.       << 
191  */                                            << 
192 void *memchr_inv(const void *start, int c, siz << 
193 {                                              << 
194         u8 value = c;                          << 
195         u64 value64;                           << 
196         unsigned int words, prefix;            << 
197                                                << 
198         if (bytes <= 16)                       << 
199                 return check_bytes8(start, val << 
200                                                << 
201         value64 = value;                       << 
202         value64 |= value64 << 8;               << 
203         value64 |= value64 << 16;              << 
204         value64 |= value64 << 32;              << 
205                                                << 
206         prefix = (unsigned long)start % 8;     << 
207         if (prefix) {                          << 
208                 u8 *r;                         << 
209                                                << 
210                 prefix = 8 - prefix;           << 
211                 r = check_bytes8(start, value, << 
212                 if (r)                         << 
213                         return r;              << 
214                 start += prefix;               << 
215                 bytes -= prefix;               << 
216         }                                      << 
217                                                << 
218         words = bytes / 8;                     << 
219                                                << 
220         while (words) {                        << 
221                 if (*(u64 *)start != value64)  << 
222                         return check_bytes8(st << 
223                 start += 8;                    << 
224                 words--;                       << 
225         }                                      << 
226                                                << 
227         return check_bytes8(start, value, byte << 
228 }                                                 108 }
229                                                   109 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php