]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - linux/string.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / linux / string.c
index 0f23f07437f389345c67c96884d335b1b54552d4..a32a8995ddc46cb611490122bb43b60a007b0419 100644 (file)
  * -  Kissed strtok() goodbye
  */
 
-#include <linux/types.h>
-#include <linux/string.h>
-#include <linux/ctype.h>
-#include <linux/kernel.h>
-#include <linux/export.h>
-#include <linux/bug.h>
-#include <linux/errno.h>
-
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
 #include <string.h>
 
-/**
- * skip_spaces - Removes leading whitespace from @str.
- * @str: The string to be stripped.
- *
- * Returns a pointer to the first non-whitespace character in @str.
- */
-char *skip_spaces(const char *str)
+#include <linux/bug.h>
+#include <linux/compiler.h>
+#include <linux/string.h>
+
+static char *skip_spaces(const char *str)
 {
        while (isspace(*str))
                ++str;
        return (char *)str;
 }
 
-/**
- * strim - Removes leading and trailing whitespace from @s.
- * @s: The string to be stripped.
- *
- * Note that the first trailing whitespace is replaced with a %NUL-terminator
- * in the given string @s. Returns a pointer to the first non-whitespace
- * character in @s.
- */
 char *strim(char *s)
 {
        size_t size;
@@ -67,17 +52,6 @@ char *strim(char *s)
        return skip_spaces(s);
 }
 
-/**
- * strlcpy - Copy a C-string into a sized buffer
- * @dest: Where to copy the string to
- * @src: Where to copy the string from
- * @size: size of destination buffer
- *
- * Compatible with *BSD: the result is always a valid
- * NUL-terminated string that fits in the buffer (unless,
- * of course, the buffer size is zero). It does not pad
- * out the result like strncpy() does.
- */
 size_t strlcpy(char *dest, const char *src, size_t size)
 {
        size_t ret = strlen(src);
@@ -90,8 +64,49 @@ size_t strlcpy(char *dest, const char *src, size_t size)
        return ret;
 }
 
+ssize_t strscpy(char *dest, const char *src, size_t count)
+{
+       long res = 0;
+
+       if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
+               return -E2BIG;
+
+       while (count) {
+               char c;
+
+               c = src[res];
+               dest[res] = c;
+               if (!c)
+                       return res;
+               res++;
+               count--;
+       }
+
+       /* Hit buffer length without finding a NUL; force NUL-termination. */
+       if (res)
+               dest[res-1] = '\0';
+
+       return -E2BIG;
+}
+
 void memzero_explicit(void *s, size_t count)
 {
        memset(s, 0, count);
        barrier_data(s);
 }
+
+int match_string(const char * const *array, size_t n, const char *string)
+{
+       int index;
+       const char *item;
+
+       for (index = 0; index < n; index++) {
+               item = array[index];
+               if (!item)
+                       break;
+               if (!strcmp(item, string))
+                       return index;
+       }
+
+       return -EINVAL;
+}