^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/lib/string.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * stupid library routines.. The optimized versions should generally be found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * as inline code in <asm-xx/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * These are buggy as well..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * - Added strsep() which will replace strtok() soon (because strsep() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * reentrant and should be faster). Use only strsep() in new code, please.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Matthew Hawkins <matt@mh.dropbear.id.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * - Kissed strtok() goodbye
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <asm/word-at-a-time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #ifndef __HAVE_ARCH_STRNCASECMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * strncasecmp - Case insensitive, length-limited string comparison
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @s1: One string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @s2: The other string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @len: the maximum number of characters to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int strncasecmp(const char *s1, const char *s2, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* Yes, Virginia, it had better be unsigned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned char c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) c1 = *s1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) c2 = *s2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!c1 || !c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (c1 == c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) c1 = tolower(c1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) c2 = tolower(c2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (c1 != c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) } while (--len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return (int)c1 - (int)c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) EXPORT_SYMBOL(strncasecmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #ifndef __HAVE_ARCH_STRCASECMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int strcasecmp(const char *s1, const char *s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) c1 = tolower(*s1++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) c2 = tolower(*s2++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) } while (c1 == c2 && c1 != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return c1 - c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) EXPORT_SYMBOL(strcasecmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #ifndef __HAVE_ARCH_STRCPY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * strcpy - Copy a %NUL terminated string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @dest: Where to copy the string to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * @src: Where to copy the string from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #undef strcpy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) char *strcpy(char *dest, const char *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) char *tmp = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) while ((*dest++ = *src++) != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* nothing */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) EXPORT_SYMBOL(strcpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #ifndef __HAVE_ARCH_STRNCPY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * strncpy - Copy a length-limited, C-string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * @dest: Where to copy the string to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * @src: Where to copy the string from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * @count: The maximum number of bytes to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * The result is not %NUL-terminated if the source exceeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * @count bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * In the case where the length of @src is less than that of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * count, the remainder of @dest will be padded with %NUL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) char *strncpy(char *dest, const char *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) char *tmp = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if ((*tmp = *src) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) src++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) tmp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) EXPORT_SYMBOL(strncpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #ifndef __HAVE_ARCH_STRLCPY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * strlcpy - Copy a C-string into a sized buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * @dest: Where to copy the string to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @src: Where to copy the string from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * @size: size of destination buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * Compatible with ``*BSD``: the result is always a valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * NUL-terminated string that fits in the buffer (unless,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * of course, the buffer size is zero). It does not pad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * out the result like strncpy() does.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) size_t strlcpy(char *dest, const char *src, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) size_t ret = strlen(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) size_t len = (ret >= size) ? size - 1 : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) memcpy(dest, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) dest[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) EXPORT_SYMBOL(strlcpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #ifndef __HAVE_ARCH_STRSCPY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * strscpy - Copy a C-string into a sized buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @dest: Where to copy the string to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * @src: Where to copy the string from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @count: Size of destination buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * Copy the string, or as much of it as fits, into the dest buffer. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * behavior is undefined if the string buffers overlap. The destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * buffer is always NUL terminated, unless it's zero-sized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * Preferred to strlcpy() since the API doesn't require reading memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * from the src string beyond the specified "count" bytes, and since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * the return value is easier to error-check than strlcpy()'s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * In addition, the implementation is robust to the string changing out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * from underneath it, unlike the current strlcpy() implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * Preferred to strncpy() since it always returns a valid string, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * doesn't unnecessarily force the tail of the destination buffer to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * zeroed. If zeroing is desired please use strscpy_pad().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * * The number of characters copied (not including the trailing %NUL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * * -E2BIG if count is 0 or @src was truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ssize_t strscpy(char *dest, const char *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) size_t max = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) long res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * If src is unaligned, don't cross a page boundary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * since we don't know if the next page is mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if ((long)src & (sizeof(long) - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (limit < max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) max = limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* If src or dest is unaligned, don't do word-at-a-time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (((long) dest | (long) src) & (sizeof(long) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) while (max >= sizeof(unsigned long)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) unsigned long c, data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) c = read_word_at_a_time(src+res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (has_zero(c, &data, &constants)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) data = prep_zero_mask(c, data, &constants);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) data = create_zero_mask(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) *(unsigned long *)(dest+res) = c & zero_bytemask(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return res + find_zero(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) *(unsigned long *)(dest+res) = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) res += sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) count -= sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) max -= sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) c = src[res];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dest[res] = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) res++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* Hit buffer length without finding a NUL; force NUL-termination. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dest[res-1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) EXPORT_SYMBOL(strscpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * strscpy_pad() - Copy a C-string into a sized buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * @dest: Where to copy the string to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * @src: Where to copy the string from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * @count: Size of destination buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * Copy the string, or as much of it as fits, into the dest buffer. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * behavior is undefined if the string buffers overlap. The destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * buffer is always %NUL terminated, unless it's zero-sized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * If the source string is shorter than the destination buffer, zeros
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * the tail of the destination buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * For full explanation of why you may want to consider using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * 'strscpy' functions please see the function docstring for strscpy().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * * The number of characters copied (not including the trailing %NUL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * * -E2BIG if count is 0 or @src was truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ssize_t strscpy_pad(char *dest, const char *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ssize_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) written = strscpy(dest, src, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (written < 0 || written == count - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) memset(dest + written + 1, 0, count - written - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) EXPORT_SYMBOL(strscpy_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * stpcpy - copy a string from src to dest returning a pointer to the new end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * of dest, including src's %NUL-terminator. May overrun dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @dest: pointer to end of string being copied into. Must be large enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * to receive copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * @src: pointer to the beginning of string being copied from. Must not overlap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * stpcpy differs from strcpy in a key way: the return value is a pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * to the new %NUL-terminating character in @dest. (For strcpy, the return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * value is a pointer to the start of @dest). This interface is considered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * unsafe as it doesn't perform bounds checking of the inputs. As such it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * not recommended for usage. Instead, its definition is provided in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * the compiler lowers other libcalls to stpcpy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) while ((*dest++ = *src++) != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* nothing */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return --dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) EXPORT_SYMBOL(stpcpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) #ifndef __HAVE_ARCH_STRCAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * strcat - Append one %NUL-terminated string to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * @dest: The string to be appended to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * @src: The string to append to it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) #undef strcat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) char *strcat(char *dest, const char *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) char *tmp = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) while (*dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) dest++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) while ((*dest++ = *src++) != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) EXPORT_SYMBOL(strcat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) #ifndef __HAVE_ARCH_STRNCAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * strncat - Append a length-limited, C-string to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * @dest: The string to be appended to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * @src: The string to append to it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * @count: The maximum numbers of bytes to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * Note that in contrast to strncpy(), strncat() ensures the result is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) char *strncat(char *dest, const char *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) char *tmp = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) while (*dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) dest++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) while ((*dest++ = *src++) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (--count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) *dest = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) EXPORT_SYMBOL(strncat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #ifndef __HAVE_ARCH_STRLCAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * strlcat - Append a length-limited, C-string to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * @dest: The string to be appended to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * @src: The string to append to it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * @count: The size of the destination buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) size_t strlcat(char *dest, const char *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) size_t dsize = strlen(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) size_t len = strlen(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) size_t res = dsize + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* This would be a bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) BUG_ON(dsize >= count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) dest += dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) count -= dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (len >= count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) len = count-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) memcpy(dest, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) dest[len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) EXPORT_SYMBOL(strlcat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) #ifndef __HAVE_ARCH_STRCMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * strcmp - Compare two strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * @cs: One string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * @ct: Another string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) #undef strcmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) int strcmp(const char *cs, const char *ct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) unsigned char c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) c1 = *cs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) c2 = *ct++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (c1 != c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return c1 < c2 ? -1 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (!c1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) EXPORT_SYMBOL(strcmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) #ifndef __HAVE_ARCH_STRNCMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * strncmp - Compare two length-limited strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * @cs: One string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * @ct: Another string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * @count: The maximum number of bytes to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int strncmp(const char *cs, const char *ct, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) unsigned char c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) c1 = *cs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) c2 = *ct++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (c1 != c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return c1 < c2 ? -1 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!c1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) EXPORT_SYMBOL(strncmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) #ifndef __HAVE_ARCH_STRCHR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * strchr - Find the first occurrence of a character in a string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * @s: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * @c: The character to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * Note that the %NUL-terminator is considered part of the string, and can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * be searched for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) char *strchr(const char *s, int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) for (; *s != (char)c; ++s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (*s == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return (char *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) EXPORT_SYMBOL(strchr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) #ifndef __HAVE_ARCH_STRCHRNUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * strchrnul - Find and return a character in a string, or end of string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * @s: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * @c: The character to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * Returns pointer to first occurrence of 'c' in s. If c is not found, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * return a pointer to the null byte at the end of s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) char *strchrnul(const char *s, int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) while (*s && *s != (char)c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return (char *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) EXPORT_SYMBOL(strchrnul);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * strnchrnul - Find and return a character in a length limited string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * or end of string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * @s: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * @count: The number of characters to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * @c: The character to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * Returns pointer to the first occurrence of 'c' in s. If c is not found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * then return a pointer to the last character of the string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) char *strnchrnul(const char *s, size_t count, int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) while (count-- && *s && *s != (char)c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return (char *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) #ifndef __HAVE_ARCH_STRRCHR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * strrchr - Find the last occurrence of a character in a string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * @s: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * @c: The character to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) char *strrchr(const char *s, int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) const char *last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (*s == (char)c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) last = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) } while (*s++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return (char *)last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) EXPORT_SYMBOL(strrchr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) #ifndef __HAVE_ARCH_STRNCHR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * strnchr - Find a character in a length limited string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * @s: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * @count: The number of characters to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * @c: The character to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * Note that the %NUL-terminator is considered part of the string, and can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * be searched for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) char *strnchr(const char *s, size_t count, int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (*s == (char)c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return (char *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (*s++ == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) EXPORT_SYMBOL(strnchr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * skip_spaces - Removes leading whitespace from @str.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * @str: The string to be stripped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) * Returns a pointer to the first non-whitespace character in @str.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) char *skip_spaces(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) while (isspace(*str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) ++str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return (char *)str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) EXPORT_SYMBOL(skip_spaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * strim - Removes leading and trailing whitespace from @s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * @s: The string to be stripped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * Note that the first trailing whitespace is replaced with a %NUL-terminator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * in the given string @s. Returns a pointer to the first non-whitespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * character in @s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) char *strim(char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) size = strlen(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) end = s + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) while (end >= s && isspace(*end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) end--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) *(end + 1) = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return skip_spaces(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) EXPORT_SYMBOL(strim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) #ifndef __HAVE_ARCH_STRLEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * strlen - Find the length of a string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * @s: The string to be sized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) size_t strlen(const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) const char *sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) for (sc = s; *sc != '\0'; ++sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* nothing */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return sc - s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) EXPORT_SYMBOL(strlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) #ifndef __HAVE_ARCH_STRNLEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * strnlen - Find the length of a length-limited string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * @s: The string to be sized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * @count: The maximum number of bytes to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) size_t strnlen(const char *s, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) const char *sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) for (sc = s; count-- && *sc != '\0'; ++sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /* nothing */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return sc - s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) EXPORT_SYMBOL(strnlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) #ifndef __HAVE_ARCH_STRSPN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * @s: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) * @accept: The string to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) size_t strspn(const char *s, const char *accept)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) const char *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) size_t count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) for (p = s; *p != '\0'; ++p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) for (a = accept; *a != '\0'; ++a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (*p == *a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (*a == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) ++count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) EXPORT_SYMBOL(strspn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) #ifndef __HAVE_ARCH_STRCSPN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * @s: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * @reject: The string to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) size_t strcspn(const char *s, const char *reject)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) const char *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) size_t count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) for (p = s; *p != '\0'; ++p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) for (r = reject; *r != '\0'; ++r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (*p == *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) ++count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) EXPORT_SYMBOL(strcspn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) #ifndef __HAVE_ARCH_STRPBRK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * strpbrk - Find the first occurrence of a set of characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * @cs: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * @ct: The characters to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) char *strpbrk(const char *cs, const char *ct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) const char *sc1, *sc2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) for (sc1 = cs; *sc1 != '\0'; ++sc1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) for (sc2 = ct; *sc2 != '\0'; ++sc2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (*sc1 == *sc2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return (char *)sc1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) EXPORT_SYMBOL(strpbrk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) #ifndef __HAVE_ARCH_STRSEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * strsep - Split a string into tokens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) * @s: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * @ct: The characters to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * strsep() updates @s to point after the token, ready for the next call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * It returns empty tokens, too, behaving exactly like the libc function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * Same semantics, slimmer shape. ;)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) char *strsep(char **s, const char *ct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) char *sbegin = *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (sbegin == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) end = strpbrk(sbegin, ct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) *end++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) *s = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return sbegin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) EXPORT_SYMBOL(strsep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * sysfs_streq - return true if strings are equal, modulo trailing newline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * @s1: one string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * @s2: another string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) * This routine returns true iff two strings are equal, treating both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * NUL and newline-then-NUL as equivalent string terminations. It's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * geared for use with sysfs input strings, which generally terminate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * with newlines but are compared against values without newlines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) bool sysfs_streq(const char *s1, const char *s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) while (*s1 && *s1 == *s2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) s1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) s2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (*s1 == *s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (!*s1 && *s2 == '\n' && !s2[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (*s1 == '\n' && !s1[1] && !*s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) EXPORT_SYMBOL(sysfs_streq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * match_string - matches given string in an array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * @array: array of strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * @n: number of strings in the array or -1 for NULL terminated arrays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * @string: string to match with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * This routine will look for a string in an array of strings up to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * n-th element in the array or until the first NULL element.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * Historically the value of -1 for @n, was used to search in arrays that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * are NULL terminated. However, the function does not make a distinction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * when finishing the search: either @n elements have been compared OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * the first NULL element was found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) * index of a @string in the @array if matches, or %-EINVAL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) int match_string(const char * const *array, size_t n, const char *string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) const char *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) for (index = 0; index < n; index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) item = array[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) if (!strcmp(item, string))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) EXPORT_SYMBOL(match_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * __sysfs_match_string - matches given string in an array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * @array: array of strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * @n: number of strings in the array or -1 for NULL terminated arrays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * @str: string to match with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * Returns index of @str in the @array or -EINVAL, just like match_string().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * Uses sysfs_streq instead of strcmp for matching.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * This routine will look for a string in an array of strings up to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) * n-th element in the array or until the first NULL element.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * Historically the value of -1 for @n, was used to search in arrays that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * are NULL terminated. However, the function does not make a distinction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * when finishing the search: either @n elements have been compared OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * the first NULL element was found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) int __sysfs_match_string(const char * const *array, size_t n, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) const char *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) for (index = 0; index < n; index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) item = array[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (!item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (sysfs_streq(item, str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) EXPORT_SYMBOL(__sysfs_match_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) #ifndef __HAVE_ARCH_MEMSET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * memset - Fill a region of memory with the given value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * @s: Pointer to the start of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * @c: The byte to fill the area with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * @count: The size of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * Do not use memset() to access IO space, use memset_io() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) void *memset(void *s, int c, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) char *xs = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) *xs++ = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) EXPORT_SYMBOL(memset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) #ifndef __HAVE_ARCH_MEMSET16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * memset16() - Fill a memory area with a uint16_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * @s: Pointer to the start of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * @v: The value to fill the area with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * @count: The number of values to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) * Differs from memset() in that it fills with a uint16_t instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * of a byte. Remember that @count is the number of uint16_ts to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * store, not the number of bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) void *memset16(uint16_t *s, uint16_t v, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) uint16_t *xs = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) *xs++ = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) EXPORT_SYMBOL(memset16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) #ifndef __HAVE_ARCH_MEMSET32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * memset32() - Fill a memory area with a uint32_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * @s: Pointer to the start of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * @v: The value to fill the area with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * @count: The number of values to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * Differs from memset() in that it fills with a uint32_t instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) * of a byte. Remember that @count is the number of uint32_ts to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) * store, not the number of bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) void *memset32(uint32_t *s, uint32_t v, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) uint32_t *xs = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) *xs++ = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) EXPORT_SYMBOL(memset32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) #ifndef __HAVE_ARCH_MEMSET64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * memset64() - Fill a memory area with a uint64_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * @s: Pointer to the start of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * @v: The value to fill the area with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * @count: The number of values to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * Differs from memset() in that it fills with a uint64_t instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * of a byte. Remember that @count is the number of uint64_ts to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * store, not the number of bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) void *memset64(uint64_t *s, uint64_t v, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) uint64_t *xs = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) *xs++ = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) EXPORT_SYMBOL(memset64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) #ifndef __HAVE_ARCH_MEMCPY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * memcpy - Copy one area of memory to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * @dest: Where to copy to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) * @src: Where to copy from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) * @count: The size of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * You should not use this function to access IO space, use memcpy_toio()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * or memcpy_fromio() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) void *memcpy(void *dest, const void *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) char *tmp = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) const char *s = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) *tmp++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) return dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) EXPORT_SYMBOL(memcpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) #ifndef __HAVE_ARCH_MEMMOVE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * memmove - Copy one area of memory to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * @dest: Where to copy to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * @src: Where to copy from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) * @count: The size of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * Unlike memcpy(), memmove() copes with overlapping areas.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) void *memmove(void *dest, const void *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) char *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) const char *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (dest <= src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) tmp = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) s = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) *tmp++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) tmp = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) tmp += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) s = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) s += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) *--tmp = *--s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) return dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) EXPORT_SYMBOL(memmove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) #ifndef __HAVE_ARCH_MEMCMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * memcmp - Compare two areas of memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * @cs: One area of memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) * @ct: Another area of memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * @count: The size of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) #undef memcmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) __visible int memcmp(const void *cs, const void *ct, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) const unsigned char *su1, *su2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if ((res = *su1 - *su2) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) EXPORT_SYMBOL(memcmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) #ifndef __HAVE_ARCH_BCMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * bcmp - returns 0 if and only if the buffers have identical contents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * @a: pointer to first buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * @b: pointer to second buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * @len: size of buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * The sign or magnitude of a non-zero return value has no particular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * meaning, and architectures may implement their own more efficient bcmp(). So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) * while this particular implementation is a simple (tail) call to memcmp, do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) * not rely on anything but whether the return value is zero or non-zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) #undef bcmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) int bcmp(const void *a, const void *b, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return memcmp(a, b, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) EXPORT_SYMBOL(bcmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) #ifndef __HAVE_ARCH_MEMSCAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) * memscan - Find a character in an area of memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) * @addr: The memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) * @c: The byte to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * @size: The size of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) * returns the address of the first occurrence of @c, or 1 byte past
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) * the area if @c is not found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) void *memscan(void *addr, int c, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) unsigned char *p = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) while (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (*p == c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) return (void *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) size--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) return (void *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) EXPORT_SYMBOL(memscan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) #ifndef __HAVE_ARCH_STRSTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * strstr - Find the first substring in a %NUL terminated string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) * @s1: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) * @s2: The string to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) char *strstr(const char *s1, const char *s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) size_t l1, l2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) l2 = strlen(s2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (!l2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) return (char *)s1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) l1 = strlen(s1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) while (l1 >= l2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) l1--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (!memcmp(s1, s2, l2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return (char *)s1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) s1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) EXPORT_SYMBOL(strstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) #ifndef __HAVE_ARCH_STRNSTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * strnstr - Find the first substring in a length-limited string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * @s1: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * @s2: The string to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * @len: the maximum number of characters to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) char *strnstr(const char *s1, const char *s2, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) size_t l2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) l2 = strlen(s2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (!l2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) return (char *)s1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) while (len >= l2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (!memcmp(s1, s2, l2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) return (char *)s1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) s1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) EXPORT_SYMBOL(strnstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) #ifndef __HAVE_ARCH_MEMCHR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) * memchr - Find a character in an area of memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * @s: The memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * @c: The byte to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * @n: The size of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * returns the address of the first occurrence of @c, or %NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * if @c is not found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) void *memchr(const void *s, int c, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) const unsigned char *p = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) while (n-- != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) if ((unsigned char)c == *p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) return (void *)(p - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) EXPORT_SYMBOL(memchr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) while (bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) if (*start != value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) return (void *)start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) start++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) bytes--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) * memchr_inv - Find an unmatching character in an area of memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * @start: The memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) * @c: Find a character other than c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) * @bytes: The size of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) * returns the address of the first character other than @c, or %NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) * if the whole buffer contains just @c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) void *memchr_inv(const void *start, int c, size_t bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) u8 value = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) u64 value64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) unsigned int words, prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if (bytes <= 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) return check_bytes8(start, value, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) value64 = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) value64 *= 0x0101010101010101ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) value64 *= 0x01010101;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) value64 |= value64 << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) value64 |= value64 << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) value64 |= value64 << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) value64 |= value64 << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) prefix = (unsigned long)start % 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) if (prefix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) u8 *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) prefix = 8 - prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) r = check_bytes8(start, value, prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) start += prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) bytes -= prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) words = bytes / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) while (words) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) if (*(u64 *)start != value64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) return check_bytes8(start, value, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) start += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) words--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) return check_bytes8(start, value, bytes % 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) EXPORT_SYMBOL(memchr_inv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) * strreplace - Replace all occurrences of character in string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) * @s: The string to operate on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) * @old: The character being replaced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) * @new: The character @old is replaced with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) * Returns pointer to the nul byte at the end of @s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) char *strreplace(char *s, char old, char new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) for (; *s; ++s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) if (*s == old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) *s = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) EXPORT_SYMBOL(strreplace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) void fortify_panic(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) pr_emerg("detected buffer overflow in %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) EXPORT_SYMBOL(fortify_panic);