]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/string.c
bcache in userspace; userspace fsck
[bcachefs-tools-debian] / linux / string.c
1 /*
2  *  linux/lib/string.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * stupid library routines.. The optimized versions should generally be found
9  * as inline code in <asm-xx/string.h>
10  *
11  * These are buggy as well..
12  *
13  * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
14  * -  Added strsep() which will replace strtok() soon (because strsep() is
15  *    reentrant and should be faster). Use only strsep() in new code, please.
16  *
17  * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
18  *                    Matthew Hawkins <matt@mh.dropbear.id.au>
19  * -  Kissed strtok() goodbye
20  */
21
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/ctype.h>
25 #include <linux/kernel.h>
26 #include <linux/export.h>
27 #include <linux/bug.h>
28 #include <linux/errno.h>
29
30 #include <string.h>
31
32 /**
33  * skip_spaces - Removes leading whitespace from @str.
34  * @str: The string to be stripped.
35  *
36  * Returns a pointer to the first non-whitespace character in @str.
37  */
38 char *skip_spaces(const char *str)
39 {
40         while (isspace(*str))
41                 ++str;
42         return (char *)str;
43 }
44
45 /**
46  * strim - Removes leading and trailing whitespace from @s.
47  * @s: The string to be stripped.
48  *
49  * Note that the first trailing whitespace is replaced with a %NUL-terminator
50  * in the given string @s. Returns a pointer to the first non-whitespace
51  * character in @s.
52  */
53 char *strim(char *s)
54 {
55         size_t size;
56         char *end;
57
58         size = strlen(s);
59         if (!size)
60                 return s;
61
62         end = s + size - 1;
63         while (end >= s && isspace(*end))
64                 end--;
65         *(end + 1) = '\0';
66
67         return skip_spaces(s);
68 }
69
70 /**
71  * strlcpy - Copy a C-string into a sized buffer
72  * @dest: Where to copy the string to
73  * @src: Where to copy the string from
74  * @size: size of destination buffer
75  *
76  * Compatible with *BSD: the result is always a valid
77  * NUL-terminated string that fits in the buffer (unless,
78  * of course, the buffer size is zero). It does not pad
79  * out the result like strncpy() does.
80  */
81 size_t strlcpy(char *dest, const char *src, size_t size)
82 {
83         size_t ret = strlen(src);
84
85         if (size) {
86                 size_t len = (ret >= size) ? size - 1 : ret;
87                 memcpy(dest, src, len);
88                 dest[len] = '\0';
89         }
90         return ret;
91 }
92
93 void memzero_explicit(void *s, size_t count)
94 {
95         memset(s, 0, count);
96         barrier_data(s);
97 }