Serenity Operating System
at master 73 lines 1.6 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/BuiltinWrappers.h> 8#include <assert.h> 9#include <ctype.h> 10#include <string.h> 11#include <strings.h> 12 13extern "C" { 14 15void bzero(void* dest, size_t n) 16{ 17 memset(dest, 0, n); 18} 19 20void bcopy(void const* src, void* dest, size_t n) 21{ 22 memmove(dest, src, n); 23} 24 25static char foldcase(char ch) 26{ 27 if (isalpha(ch)) 28 return tolower(ch); 29 return ch; 30} 31 32// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcasecmp.html 33int strcasecmp(char const* s1, char const* s2) 34{ 35 for (; foldcase(*s1) == foldcase(*s2); ++s1, ++s2) { 36 if (*s1 == 0) 37 return 0; 38 } 39 return foldcase(*(unsigned char const*)s1) < foldcase(*(unsigned char const*)s2) ? -1 : 1; 40} 41 42// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncasecmp.html 43int strncasecmp(char const* s1, char const* s2, size_t n) 44{ 45 if (!n) 46 return 0; 47 do { 48 if (foldcase(*s1) != foldcase(*s2++)) 49 return foldcase(*(unsigned char const*)s1) - foldcase(*(unsigned char const*)--s2); 50 if (*s1++ == 0) 51 break; 52 } while (--n); 53 return 0; 54} 55 56// https://pubs.opengroup.org/onlinepubs/009696799/functions/ffs.html 57int ffs(int i) 58{ 59 return bit_scan_forward(i); 60} 61 62// https://linux.die.net/man/3/ffsl (GNU extension) 63int ffsl(long int i) 64{ 65 return bit_scan_forward(i); 66} 67 68// https://linux.die.net/man/3/ffsll (GNU extension) 69int ffsll(long long int i) 70{ 71 return bit_scan_forward(i); 72} 73}