2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-03-13 03:09:54 -03:00
|
|
|
* Copyright (c) 2021-2022, Brian Gianforcaro <bgianf@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-04-12 13:29:36 -03:00
|
|
|
#include <AK/Format.h>
|
2020-08-20 20:17:15 -03:00
|
|
|
#include <AK/MemMem.h>
|
2021-09-12 18:28:17 -03:00
|
|
|
#include <AK/Memory.h>
|
2019-07-09 10:48:13 -03:00
|
|
|
#include <AK/Platform.h>
|
2019-06-07 06:49:03 -03:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
#include <assert.h>
|
2019-07-09 10:48:13 -03:00
|
|
|
#include <ctype.h>
|
2018-11-06 12:45:16 -02:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <signal.h>
|
2019-06-07 06:49:03 -03:00
|
|
|
#include <stdio.h>
|
2019-02-01 13:03:21 -02:00
|
|
|
#include <stdlib.h>
|
2019-06-07 06:49:03 -03:00
|
|
|
#include <string.h>
|
2018-10-23 05:12:50 -03:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strspn.html
|
2022-04-01 14:58:27 -03:00
|
|
|
size_t strspn(char const* s, char const* accept)
|
2018-10-31 13:50:43 -03:00
|
|
|
{
|
2022-04-01 14:58:27 -03:00
|
|
|
char const* p = s;
|
2018-10-31 13:50:43 -03:00
|
|
|
cont:
|
|
|
|
|
char ch = *p++;
|
|
|
|
|
char ac;
|
2022-04-01 14:58:27 -03:00
|
|
|
for (char const* ap = accept; (ac = *ap++) != '\0';) {
|
2018-10-31 13:50:43 -03:00
|
|
|
if (ac == ch)
|
|
|
|
|
goto cont;
|
|
|
|
|
}
|
|
|
|
|
return p - 1 - s;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcspn.html
|
2022-04-01 14:58:27 -03:00
|
|
|
size_t strcspn(char const* s, char const* reject)
|
2018-10-31 13:50:43 -03:00
|
|
|
{
|
|
|
|
|
for (auto* p = s;;) {
|
|
|
|
|
char c = *p++;
|
|
|
|
|
auto* rp = reject;
|
|
|
|
|
char rc;
|
|
|
|
|
do {
|
|
|
|
|
if ((rc = *rp++) == c)
|
|
|
|
|
return p - 1 - s;
|
2019-06-07 06:49:03 -03:00
|
|
|
} while (rc);
|
2018-10-31 13:50:43 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strlen.html
|
2022-04-01 14:58:27 -03:00
|
|
|
size_t strlen(char const* str)
|
2018-10-23 05:12:50 -03:00
|
|
|
{
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
while (*(str++))
|
|
|
|
|
++len;
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strnlen.html
|
2022-04-01 14:58:27 -03:00
|
|
|
size_t strnlen(char const* str, size_t maxlen)
|
2020-01-16 18:11:05 -03:00
|
|
|
{
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
for (; len < maxlen && *str; str++)
|
|
|
|
|
len++;
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strdup.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strdup(char const* str)
|
2019-02-01 13:03:21 -02:00
|
|
|
{
|
|
|
|
|
size_t len = strlen(str);
|
2019-02-03 08:48:41 -02:00
|
|
|
char* new_str = (char*)malloc(len + 1);
|
2020-08-23 07:34:08 -03:00
|
|
|
memcpy(new_str, str, len);
|
|
|
|
|
new_str[len] = '\0';
|
2019-02-01 13:03:21 -02:00
|
|
|
return new_str;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strndup.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strndup(char const* str, size_t maxlen)
|
2019-02-03 13:11:28 -02:00
|
|
|
{
|
2020-08-23 07:34:08 -03:00
|
|
|
size_t len = strnlen(str, maxlen);
|
2019-02-03 13:11:28 -02:00
|
|
|
char* new_str = (char*)malloc(len + 1);
|
|
|
|
|
memcpy(new_str, str, len);
|
|
|
|
|
new_str[len] = 0;
|
|
|
|
|
return new_str;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcmp.html
|
2022-04-01 14:58:27 -03:00
|
|
|
int strcmp(char const* s1, char const* s2)
|
2018-10-26 08:32:13 -03:00
|
|
|
{
|
2018-11-05 15:16:00 -02:00
|
|
|
while (*s1 == *s2++)
|
|
|
|
|
if (*s1++ == 0)
|
2018-10-26 08:32:13 -03:00
|
|
|
return 0;
|
2022-04-01 14:58:27 -03:00
|
|
|
return *(unsigned char const*)s1 - *(unsigned char const*)--s2;
|
2018-11-05 15:16:00 -02:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncmp.html
|
2022-04-01 14:58:27 -03:00
|
|
|
int strncmp(char const* s1, char const* s2, size_t n)
|
2018-11-05 15:16:00 -02:00
|
|
|
{
|
|
|
|
|
if (!n)
|
|
|
|
|
return 0;
|
|
|
|
|
do {
|
|
|
|
|
if (*s1 != *s2++)
|
2022-04-01 14:58:27 -03:00
|
|
|
return *(unsigned char const*)s1 - *(unsigned char const*)--s2;
|
2018-11-05 15:16:00 -02:00
|
|
|
if (*s1++ == 0)
|
|
|
|
|
break;
|
|
|
|
|
} while (--n);
|
|
|
|
|
return 0;
|
2018-10-26 08:32:13 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memcmp.html
|
2022-04-01 14:58:27 -03:00
|
|
|
int memcmp(void const* v1, void const* v2, size_t n)
|
2018-10-28 05:36:21 -03:00
|
|
|
{
|
2022-04-01 14:58:27 -03:00
|
|
|
auto* s1 = (uint8_t const*)v1;
|
|
|
|
|
auto* s2 = (uint8_t const*)v2;
|
2018-10-28 05:36:21 -03:00
|
|
|
while (n-- > 0) {
|
|
|
|
|
if (*s1++ != *s2++)
|
|
|
|
|
return s1[-1] < s2[-1] ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
int timingsafe_memcmp(void const* b1, void const* b2, size_t len)
|
2022-03-13 03:09:54 -03:00
|
|
|
{
|
|
|
|
|
return AK::timing_safe_compare(b1, b2, len) ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memcpy.html
|
2022-04-01 14:58:27 -03:00
|
|
|
void* memcpy(void* dest_ptr, void const* src_ptr, size_t n)
|
2018-10-26 08:32:13 -03:00
|
|
|
{
|
2022-07-22 15:48:24 -03:00
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
2020-07-27 10:54:39 -03:00
|
|
|
void* original_dest = dest_ptr;
|
2019-01-15 05:14:08 -02:00
|
|
|
asm volatile(
|
2020-07-27 10:54:39 -03:00
|
|
|
"rep movsb"
|
|
|
|
|
: "+D"(dest_ptr), "+S"(src_ptr), "+c"(n)::"memory");
|
|
|
|
|
return original_dest;
|
2022-07-22 15:48:24 -03:00
|
|
|
#else
|
|
|
|
|
# error Unknown architecture
|
|
|
|
|
#endif
|
2018-10-26 08:32:13 -03:00
|
|
|
}
|
|
|
|
|
|
2022-03-24 12:41:18 -03:00
|
|
|
#if ARCH(I386)
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memset.html
|
2019-02-22 06:45:32 -03:00
|
|
|
void* memset(void* dest_ptr, int c, size_t n)
|
2019-01-15 05:14:08 -02:00
|
|
|
{
|
2021-04-07 10:11:13 -03:00
|
|
|
size_t dest = (size_t)dest_ptr;
|
|
|
|
|
// FIXME: Support starting at an unaligned address.
|
|
|
|
|
if (!(dest & 0x3) && n >= 12) {
|
|
|
|
|
size_t size_ts = n / sizeof(size_t);
|
|
|
|
|
size_t expanded_c = explode_byte((u8)c);
|
|
|
|
|
asm volatile(
|
|
|
|
|
"rep stosl\n"
|
|
|
|
|
: "=D"(dest)
|
|
|
|
|
: "D"(dest), "c"(size_ts), "a"(expanded_c)
|
|
|
|
|
: "memory");
|
|
|
|
|
n -= size_ts * sizeof(size_t);
|
|
|
|
|
if (n == 0)
|
|
|
|
|
return dest_ptr;
|
|
|
|
|
}
|
2019-01-15 05:14:08 -02:00
|
|
|
asm volatile(
|
|
|
|
|
"rep stosb\n"
|
2021-04-07 10:11:13 -03:00
|
|
|
: "=D"(dest), "=c"(n)
|
|
|
|
|
: "0"(dest), "1"(n), "a"(c)
|
2019-06-07 06:49:03 -03:00
|
|
|
: "memory");
|
2021-04-07 10:11:13 -03:00
|
|
|
return dest_ptr;
|
2019-01-15 05:14:08 -02:00
|
|
|
}
|
2022-07-22 15:48:24 -03:00
|
|
|
#elif ARCH(X86_64)
|
|
|
|
|
// For x86-64, an optimized ASM implementation is found in ./arch/x86_64/memset.S
|
|
|
|
|
#else
|
|
|
|
|
# error Unknown architecture
|
2022-03-24 12:41:18 -03:00
|
|
|
#endif
|
2019-01-15 05:14:08 -02:00
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memmove.html
|
2022-04-01 14:58:27 -03:00
|
|
|
void* memmove(void* dest, void const* src, size_t n)
|
2018-11-11 12:36:40 -02:00
|
|
|
{
|
2021-09-13 12:23:24 -03:00
|
|
|
if (((FlatPtr)dest - (FlatPtr)src) >= n)
|
2018-11-11 12:36:40 -02:00
|
|
|
return memcpy(dest, src, n);
|
2019-01-23 05:31:23 -02:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
u8* pd = (u8*)dest;
|
2022-04-01 14:58:27 -03:00
|
|
|
u8 const* ps = (u8 const*)src;
|
2019-01-23 05:31:23 -02:00
|
|
|
for (pd += n, ps += n; n--;)
|
|
|
|
|
*--pd = *--ps;
|
|
|
|
|
return dest;
|
2018-11-11 12:36:40 -02:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
void const* memmem(void const* haystack, size_t haystack_length, void const* needle, size_t needle_length)
|
2020-07-31 08:56:33 -03:00
|
|
|
{
|
2020-08-20 20:17:15 -03:00
|
|
|
return AK::memmem(haystack, haystack_length, needle, needle_length);
|
2020-07-31 08:56:33 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcpy.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strcpy(char* dest, char const* src)
|
2018-10-31 06:14:56 -03:00
|
|
|
{
|
2021-04-29 05:15:28 -03:00
|
|
|
char* original_dest = dest;
|
2019-06-07 06:49:03 -03:00
|
|
|
while ((*dest++ = *src++) != '\0')
|
|
|
|
|
;
|
2021-04-29 05:15:28 -03:00
|
|
|
return original_dest;
|
2018-10-31 06:14:56 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncpy.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strncpy(char* dest, char const* src, size_t n)
|
2018-10-31 06:14:56 -03:00
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
for (i = 0; i < n && src[i] != '\0'; ++i)
|
|
|
|
|
dest[i] = src[i];
|
2019-06-07 06:49:03 -03:00
|
|
|
for (; i < n; ++i)
|
2018-10-31 06:14:56 -03:00
|
|
|
dest[i] = '\0';
|
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
size_t strlcpy(char* dest, char const* src, size_t n)
|
2020-08-23 06:31:21 -03:00
|
|
|
{
|
2020-08-23 07:09:42 -03:00
|
|
|
size_t i;
|
|
|
|
|
// Would like to test i < n - 1 here, but n might be 0.
|
|
|
|
|
for (i = 0; i + 1 < n && src[i] != '\0'; ++i)
|
|
|
|
|
dest[i] = src[i];
|
|
|
|
|
if (n)
|
|
|
|
|
dest[i] = '\0';
|
|
|
|
|
for (; src[i] != '\0'; ++i)
|
|
|
|
|
; // Determine the length of src, don't copy.
|
|
|
|
|
return i;
|
2020-08-23 06:31:21 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strchr.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strchr(char const* str, int c)
|
2018-10-31 06:14:56 -03:00
|
|
|
{
|
2018-11-06 11:13:16 -02:00
|
|
|
char ch = c;
|
|
|
|
|
for (;; ++str) {
|
|
|
|
|
if (*str == ch)
|
2018-11-09 07:09:46 -02:00
|
|
|
return const_cast<char*>(str);
|
2018-11-06 11:13:16 -02:00
|
|
|
if (!*str)
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-10-31 06:14:56 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-28 00:27:46 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699959399/functions/index.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* index(char const* str, int c)
|
2021-12-28 00:27:46 -03:00
|
|
|
{
|
|
|
|
|
return strchr(str, c);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strchrnul(char const* str, int c)
|
2020-02-14 18:58:36 -03:00
|
|
|
{
|
|
|
|
|
char ch = c;
|
|
|
|
|
for (;; ++str) {
|
|
|
|
|
if (*str == ch || !*str)
|
|
|
|
|
return const_cast<char*>(str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memchr.html
|
2022-04-01 14:58:27 -03:00
|
|
|
void* memchr(void const* ptr, int c, size_t size)
|
2019-02-03 01:32:31 -02:00
|
|
|
{
|
|
|
|
|
char ch = c;
|
2022-04-01 14:58:27 -03:00
|
|
|
auto* cptr = (char const*)ptr;
|
2019-02-03 01:32:31 -02:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
|
if (cptr[i] == ch)
|
2019-04-23 08:00:53 -03:00
|
|
|
return const_cast<char*>(cptr + i);
|
2019-02-03 01:32:31 -02:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strrchr.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strrchr(char const* str, int ch)
|
2018-10-31 13:50:43 -03:00
|
|
|
{
|
2019-06-07 06:49:03 -03:00
|
|
|
char* last = nullptr;
|
2018-10-31 13:50:43 -03:00
|
|
|
char c;
|
|
|
|
|
for (; (c = *str); ++str) {
|
|
|
|
|
if (c == ch)
|
2018-11-09 07:09:46 -02:00
|
|
|
last = const_cast<char*>(str);
|
2018-10-31 13:50:43 -03:00
|
|
|
}
|
|
|
|
|
return last;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 00:27:46 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699959399/functions/rindex.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* rindex(char const* str, int ch)
|
2021-12-28 00:27:46 -03:00
|
|
|
{
|
|
|
|
|
return strrchr(str, ch);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcat.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strcat(char* dest, char const* src)
|
2018-10-31 06:14:56 -03:00
|
|
|
{
|
2019-01-31 14:31:23 -02:00
|
|
|
size_t dest_length = strlen(dest);
|
2018-10-31 06:14:56 -03:00
|
|
|
size_t i;
|
2019-06-07 06:49:03 -03:00
|
|
|
for (i = 0; src[i] != '\0'; i++)
|
2019-01-31 14:31:23 -02:00
|
|
|
dest[dest_length + i] = src[i];
|
|
|
|
|
dest[dest_length + i] = '\0';
|
2018-10-31 06:14:56 -03:00
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncat.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strncat(char* dest, char const* src, size_t n)
|
2018-10-31 06:14:56 -03:00
|
|
|
{
|
2019-01-31 14:31:23 -02:00
|
|
|
size_t dest_length = strlen(dest);
|
2018-10-31 06:14:56 -03:00
|
|
|
size_t i;
|
2019-06-07 06:49:03 -03:00
|
|
|
for (i = 0; i < n && src[i] != '\0'; i++)
|
2019-01-31 14:31:23 -02:00
|
|
|
dest[dest_length + i] = src[i];
|
|
|
|
|
dest[dest_length + i] = '\0';
|
2018-10-31 06:14:56 -03:00
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
char const* const sys_errlist[] = {
|
2021-09-12 08:29:28 -03:00
|
|
|
#define __ENUMERATE_ERRNO_CODE(c, s) s,
|
|
|
|
|
ENUMERATE_ERRNO_CODES(__ENUMERATE_ERRNO_CODE)
|
|
|
|
|
#undef __ENUMERATE_ERRNO_CODE
|
2018-11-06 11:13:16 -02:00
|
|
|
};
|
2021-12-01 20:33:28 -03:00
|
|
|
static_assert(array_size(sys_errlist) == (EMAXERRNO + 1));
|
2019-02-26 18:40:35 -03:00
|
|
|
|
2019-02-24 11:19:32 -03:00
|
|
|
int sys_nerr = EMAXERRNO;
|
2018-11-06 11:13:16 -02:00
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html
|
2021-05-24 11:43:12 -03:00
|
|
|
int strerror_r(int errnum, char* buf, size_t buflen)
|
|
|
|
|
{
|
|
|
|
|
auto saved_errno = errno;
|
2021-06-11 07:02:26 -03:00
|
|
|
if (errnum < 0 || errnum >= EMAXERRNO) {
|
2021-05-24 11:43:12 -03:00
|
|
|
auto rc = strlcpy(buf, "unknown error", buflen);
|
|
|
|
|
if (rc >= buflen)
|
|
|
|
|
dbgln("strerror_r(): Invalid error number '{}' specified and the output buffer is too small.", errnum);
|
|
|
|
|
errno = saved_errno;
|
|
|
|
|
return EINVAL;
|
|
|
|
|
}
|
|
|
|
|
auto text = strerror(errnum);
|
|
|
|
|
auto rc = strlcpy(buf, text, buflen);
|
|
|
|
|
if (rc >= buflen) {
|
|
|
|
|
errno = saved_errno;
|
|
|
|
|
return ERANGE;
|
|
|
|
|
}
|
|
|
|
|
errno = saved_errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror.html
|
2018-11-05 11:50:41 -02:00
|
|
|
char* strerror(int errnum)
|
2018-10-25 07:06:00 -03:00
|
|
|
{
|
2020-11-14 07:23:39 -03:00
|
|
|
if (errnum < 0 || errnum >= EMAXERRNO) {
|
2018-11-09 07:09:46 -02:00
|
|
|
return const_cast<char*>("Unknown error");
|
2018-10-25 07:06:00 -03:00
|
|
|
}
|
2018-11-09 07:09:46 -02:00
|
|
|
return const_cast<char*>(sys_errlist[errnum]);
|
2018-10-25 07:06:00 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strsignal.html
|
2018-11-06 12:45:16 -02:00
|
|
|
char* strsignal(int signum)
|
|
|
|
|
{
|
2019-02-26 11:57:59 -03:00
|
|
|
if (signum >= NSIG) {
|
2021-04-12 13:29:36 -03:00
|
|
|
dbgln("strsignal() missing string for signum={}", signum);
|
2018-11-09 07:09:46 -02:00
|
|
|
return const_cast<char*>("Unknown signal");
|
2018-11-06 12:45:16 -02:00
|
|
|
}
|
2018-11-09 07:09:46 -02:00
|
|
|
return const_cast<char*>(sys_siglist[signum]);
|
2018-10-23 05:12:50 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strstr.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strstr(char const* haystack, char const* needle)
|
2018-11-11 17:42:41 -02:00
|
|
|
{
|
|
|
|
|
char nch;
|
|
|
|
|
char hch;
|
|
|
|
|
|
|
|
|
|
if ((nch = *needle++) != 0) {
|
|
|
|
|
size_t len = strlen(needle);
|
|
|
|
|
do {
|
|
|
|
|
do {
|
|
|
|
|
if ((hch = *haystack++) == 0)
|
|
|
|
|
return nullptr;
|
|
|
|
|
} while (hch != nch);
|
|
|
|
|
} while (strncmp(haystack, needle, len) != 0);
|
|
|
|
|
--haystack;
|
|
|
|
|
}
|
|
|
|
|
return const_cast<char*>(haystack);
|
2018-11-06 12:45:16 -02:00
|
|
|
}
|
2018-11-11 17:42:41 -02:00
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strpbrk.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strpbrk(char const* s, char const* accept)
|
2019-02-07 23:38:21 -02:00
|
|
|
{
|
|
|
|
|
while (*s)
|
2019-06-07 06:49:03 -03:00
|
|
|
if (strchr(accept, *s++))
|
2019-04-23 08:00:53 -03:00
|
|
|
return const_cast<char*>(--s);
|
2019-02-07 23:38:21 -02:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok_r.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strtok_r(char* str, char const* delim, char** saved_str)
|
2019-02-26 08:57:02 -03:00
|
|
|
{
|
2019-10-16 01:29:22 -03:00
|
|
|
if (!str) {
|
2022-02-26 08:53:02 -03:00
|
|
|
if (!saved_str || *saved_str == nullptr)
|
2019-10-16 01:29:22 -03:00
|
|
|
return nullptr;
|
2019-11-10 12:24:52 -03:00
|
|
|
str = *saved_str;
|
2019-10-16 01:29:22 -03:00
|
|
|
}
|
2019-11-10 12:24:52 -03:00
|
|
|
|
2019-10-16 01:29:22 -03:00
|
|
|
size_t token_start = 0;
|
|
|
|
|
size_t token_end = 0;
|
2019-10-16 20:44:42 -03:00
|
|
|
size_t str_len = strlen(str);
|
|
|
|
|
size_t delim_len = strlen(delim);
|
2019-11-10 12:24:52 -03:00
|
|
|
|
2019-10-16 20:44:42 -03:00
|
|
|
for (size_t i = 0; i < str_len; ++i) {
|
|
|
|
|
bool is_proper_delim = false;
|
2019-11-10 12:24:52 -03:00
|
|
|
|
2019-10-16 20:44:42 -03:00
|
|
|
for (size_t j = 0; j < delim_len; ++j) {
|
2019-10-16 01:29:22 -03:00
|
|
|
if (str[i] == delim[j]) {
|
2019-10-16 20:44:42 -03:00
|
|
|
// Skip beginning delimiters
|
|
|
|
|
if (token_end - token_start == 0) {
|
|
|
|
|
++token_start;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-11-10 12:24:52 -03:00
|
|
|
|
2019-10-16 20:44:42 -03:00
|
|
|
is_proper_delim = true;
|
2019-10-16 01:29:22 -03:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-10 12:24:52 -03:00
|
|
|
|
2019-10-16 20:44:42 -03:00
|
|
|
++token_end;
|
|
|
|
|
if (is_proper_delim && token_end > 0) {
|
|
|
|
|
--token_end;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-16 01:29:22 -03:00
|
|
|
}
|
2019-11-10 12:24:52 -03:00
|
|
|
|
2022-02-26 08:53:02 -03:00
|
|
|
if (str[token_start] == '\0') {
|
|
|
|
|
*saved_str = nullptr;
|
2019-10-16 01:29:22 -03:00
|
|
|
return nullptr;
|
2022-02-26 08:53:02 -03:00
|
|
|
}
|
2019-11-10 12:24:52 -03:00
|
|
|
|
2019-10-16 01:29:22 -03:00
|
|
|
if (token_end == 0) {
|
2019-11-10 12:24:52 -03:00
|
|
|
*saved_str = nullptr;
|
2019-10-16 01:29:22 -03:00
|
|
|
return &str[token_start];
|
|
|
|
|
}
|
2019-11-10 12:24:52 -03:00
|
|
|
|
2020-11-14 07:24:42 -03:00
|
|
|
if (str[token_end] == '\0')
|
|
|
|
|
*saved_str = &str[token_end];
|
|
|
|
|
else
|
|
|
|
|
*saved_str = &str[token_end + 1];
|
|
|
|
|
|
2019-10-16 01:29:22 -03:00
|
|
|
str[token_end] = '\0';
|
|
|
|
|
return &str[token_start];
|
2019-02-26 08:57:02 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok.html
|
2022-04-01 14:58:27 -03:00
|
|
|
char* strtok(char* str, char const* delim)
|
2019-11-10 12:24:52 -03:00
|
|
|
{
|
|
|
|
|
static char* saved_str;
|
|
|
|
|
return strtok_r(str, delim, &saved_str);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcoll.html
|
2022-04-01 14:58:27 -03:00
|
|
|
int strcoll(char const* s1, char const* s2)
|
2019-05-03 13:17:33 -03:00
|
|
|
{
|
|
|
|
|
return strcmp(s1, s2);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:56:28 -03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strxfrm.html
|
2022-04-01 14:58:27 -03:00
|
|
|
size_t strxfrm(char* dest, char const* src, size_t n)
|
2019-05-03 13:17:33 -03:00
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
for (i = 0; i < n && src[i] != '\0'; ++i)
|
|
|
|
|
dest[i] = src[i];
|
2019-06-07 06:49:03 -03:00
|
|
|
for (; i < n; ++i)
|
2019-05-03 13:17:33 -03:00
|
|
|
dest[i] = '\0';
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2021-01-09 16:04:18 -03:00
|
|
|
|
2022-01-10 17:44:44 -03:00
|
|
|
// Not in POSIX, originated in BSD but also supported on Linux.
|
|
|
|
|
// https://man.openbsd.org/strsep.3
|
|
|
|
|
char* strsep(char** str, char const* delim)
|
|
|
|
|
{
|
|
|
|
|
if (*str == nullptr)
|
|
|
|
|
return nullptr;
|
|
|
|
|
auto* begin = *str;
|
|
|
|
|
auto* end = begin + strcspn(begin, delim);
|
|
|
|
|
if (*end) {
|
|
|
|
|
*end = '\0';
|
|
|
|
|
*str = ++end;
|
|
|
|
|
} else {
|
|
|
|
|
*str = nullptr;
|
|
|
|
|
}
|
|
|
|
|
return begin;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 16:04:18 -03:00
|
|
|
void explicit_bzero(void* ptr, size_t size)
|
|
|
|
|
{
|
2021-09-12 18:28:17 -03:00
|
|
|
secure_zero(ptr, size);
|
2021-01-09 16:04:18 -03:00
|
|
|
}
|
2018-11-11 17:42:41 -02:00
|
|
|
}
|