]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/string.c
Update bcachefs sources to dbee44d5ab bcachefs: add bcachefs xxhash support
[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 <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include <linux/compiler.h>
27 #include <linux/string.h>
28
29 static char *skip_spaces(const char *str)
30 {
31         while (isspace(*str))
32                 ++str;
33         return (char *)str;
34 }
35
36 char *strim(char *s)
37 {
38         size_t size;
39         char *end;
40
41         size = strlen(s);
42         if (!size)
43                 return s;
44
45         end = s + size - 1;
46         while (end >= s && isspace(*end))
47                 end--;
48         *(end + 1) = '\0';
49
50         return skip_spaces(s);
51 }
52
53 size_t strlcpy(char *dest, const char *src, size_t size)
54 {
55         size_t ret = strlen(src);
56
57         if (size) {
58                 size_t len = (ret >= size) ? size - 1 : ret;
59                 memcpy(dest, src, len);
60                 dest[len] = '\0';
61         }
62         return ret;
63 }
64
65 void memzero_explicit(void *s, size_t count)
66 {
67         memset(s, 0, count);
68         barrier_data(s);
69 }
70
71 int match_string(const char * const *array, size_t n, const char *string)
72 {
73         int index;
74         const char *item;
75
76         for (index = 0; index < n; index++) {
77                 item = array[index];
78                 if (!item)
79                         break;
80                 if (!strcmp(item, string))
81                         return index;
82         }
83
84         return -EINVAL;
85 }