Compare commits
3 commits
0960df714d
...
b5c9c0de9a
| Author | SHA1 | Date | |
|---|---|---|---|
| b5c9c0de9a | |||
| 510a18e123 | |||
| 3c732f27db |
36 changed files with 1098 additions and 14 deletions
1
Makefile
1
Makefile
|
|
@ -18,6 +18,7 @@ VISOR_SOURCES_x86_64 := \
|
|||
VISOR_SOURCES := \
|
||||
src/alloc.c \
|
||||
src/efi.c \
|
||||
src/ssp.c \
|
||||
src/strlcpy.c \
|
||||
$(VISOR_SOURCES_$(ARCH)) \
|
||||
# end of sources list
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ ARCH = x86_64
|
|||
CROSS_PATH = /usr/bin
|
||||
|
||||
# C Compiler & Linker commands
|
||||
CC = gcc
|
||||
LD = ld
|
||||
CC = $(CROSS_PATH)/$(ARCH)-linux-gcc
|
||||
LD = $(CROSS_PATH)/$(ARCH)-linux-ld
|
||||
|
||||
# Compilation flags
|
||||
CFLAGS = -std=c17 -Wall -ffreestanding -fpic -nostdlib -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -mno-sse -mno-mmx -mno-80387
|
||||
CPPFLAGS = -Ignuefi/inc -Iinclude
|
||||
CFLAGS = -std=c17 -Wall -ffreestanding -fpic -nostdlib -fstack-protector-all -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -mno-sse -mno-mmx -mno-80387
|
||||
CPPFLAGS = -Ignuefi/inc -Iinclude -Ilibc/include
|
||||
LDFLAGS = -shared -Bsymbolic -Lgnuefi -Tgnuefi/elf_$(ARCH)_efi.lds -nostdlib
|
||||
LIBS = -lgnuefi -lefi
|
||||
|
|
|
|||
|
|
@ -1,14 +1,44 @@
|
|||
#pragma once
|
||||
#ifndef VISOR_LIBC_STRING_H
|
||||
#define VISOR_LIBC_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
char *strchr(const char *s, int c);
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
char *strcpy(char *restrict d, const char *restrict s);
|
||||
size_t strlen(const char *s);
|
||||
size_t strspn(const char *s, const char *a);
|
||||
char *strpbrk(const char *s, const char *a);
|
||||
int strcoll(const char *s1, const char *s2);
|
||||
int ffs(int);
|
||||
int ffsl(long);
|
||||
int ffsll(long long);
|
||||
|
||||
void *memcpy(void *restrict dest, const void *restrict src, size_t n);
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
void *memccpy(void *restrict, const void *restrict, int, size_t);
|
||||
void *memchr (const void *, int, size_t);
|
||||
int memcmp (const void *, const void *, size_t);
|
||||
void *memcpy (void *restrict, const void *restrict, size_t);
|
||||
void *memmove(void *, const void *, size_t);
|
||||
void *memrchr(const void *, int, size_t);
|
||||
void *memset (void *, int, size_t);
|
||||
|
||||
char *stpcpy (char *restrict, const char *restrict);
|
||||
char *stpncpy(char *restrict, const char *restrict, size_t);
|
||||
|
||||
char *strcat (char *restrict, const char *restrict);
|
||||
char *strchr (const char *, int);
|
||||
int strcmp (const char *, const char *);
|
||||
char *strchrnul(const char *, int);
|
||||
int strcoll(const char *, const char *);
|
||||
char *strcpy (char *restrict, const char *restrict);
|
||||
size_t strcspn(const char *, const char *);
|
||||
char *stresep(char **, const char *, int);
|
||||
size_t strlcat(char *restrict, const char *restrict, size_t);
|
||||
size_t strlcpy(char *restrict, const char *restrict, size_t);
|
||||
size_t strlen (const char *);
|
||||
char *strncat(char *restrict, const char *restrict, size_t);
|
||||
int strncmp(const char *, const char *, size_t);
|
||||
char *strncpy(char *restrict, const char *restrict, size_t);
|
||||
size_t strnlen(const char *, size_t);
|
||||
char *strpbrk(const char *, const char *);
|
||||
char *strrchr(const char *, int);
|
||||
char *strsep (char **, const char *);
|
||||
size_t strspn (const char *, const char *);
|
||||
char *strstr (const char *, const char *);
|
||||
char *strtok_r(char *restrict, const char *restrict, char **restrict);
|
||||
size_t strxfrm(char *restrict, const char *restrict, size_t);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
25
libc/string/ffs.c
Normal file
25
libc/string/ffs.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/ffs.c
|
||||
* Returns the index of the first set bit.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int ffs(int val)
|
||||
{
|
||||
return __builtin_ffs(val);
|
||||
}
|
||||
25
libc/string/ffsl.c
Normal file
25
libc/string/ffsl.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/ffsl.c
|
||||
* Returns the index of the first set bit.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int ffsl(long int val)
|
||||
{
|
||||
return __builtin_ffsl(val);
|
||||
}
|
||||
25
libc/string/ffsll.c
Normal file
25
libc/string/ffsll.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/ffsll.c
|
||||
* Returns the index of the first set bit.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int ffsll(long long int val)
|
||||
{
|
||||
return __builtin_ffsll(val);
|
||||
}
|
||||
33
libc/string/memccpy.c
Normal file
33
libc/string/memccpy.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/memccpy.c
|
||||
* Copy memory until length is met or character is encountered.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void* memccpy(void* dest_ptr, const void* src_ptr, int c, size_t n)
|
||||
{
|
||||
unsigned char* dest = (unsigned char*) dest_ptr;
|
||||
const unsigned char* src = (const unsigned char*) src_ptr;
|
||||
for ( size_t i = 0; i < n; i++ )
|
||||
{
|
||||
if ( (dest[i] = src[i]) == (unsigned char) c )
|
||||
return dest + i + 1;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
29
libc/string/memchr.c
Normal file
29
libc/string/memchr.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/memchr.c
|
||||
* Scans memory for a character.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void* memchr(const void* s, int c, size_t size)
|
||||
{
|
||||
const unsigned char* buf = (const unsigned char*) s;
|
||||
for ( size_t i = 0; i < size; i++ )
|
||||
if ( buf[i] == (unsigned char) c )
|
||||
return (void*) (buf + i);
|
||||
return NULL;
|
||||
}
|
||||
34
libc/string/memcmp.c
Normal file
34
libc/string/memcmp.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/memcmp.c
|
||||
* Compares two memory regions.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int memcmp(const void* a_ptr, const void* b_ptr, size_t size)
|
||||
{
|
||||
const unsigned char* a = (const unsigned char*) a_ptr;
|
||||
const unsigned char* b = (const unsigned char*) b_ptr;
|
||||
for ( size_t i = 0; i < size; i++ )
|
||||
{
|
||||
if ( a[i] < b[i] )
|
||||
return -1;
|
||||
if ( a[i] > b[i] )
|
||||
return +1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
38
libc/string/memmove.c
Normal file
38
libc/string/memmove.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2013, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/memmove.c
|
||||
* Copy memory between potentionally overlapping regions.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void* memmove(void* dest_ptr, const void* src_ptr, size_t n)
|
||||
{
|
||||
unsigned char* dest = (unsigned char*) dest_ptr;
|
||||
const unsigned char* src = (const unsigned char*) src_ptr;
|
||||
if ( (uintptr_t) dest < (uintptr_t) src )
|
||||
{
|
||||
for ( size_t i = 0; i < n; i++ )
|
||||
dest[i] = src[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
for ( size_t i = 0; i < n; i++ )
|
||||
dest[n-(i+1)] = src[n-(i+1)];
|
||||
}
|
||||
return dest_ptr;
|
||||
}
|
||||
30
libc/string/memrchr.c
Normal file
30
libc/string/memrchr.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/memrchr.c
|
||||
* Scans memory in reverse directory for a character.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void* memrchr(const void* ptr, int c, size_t n)
|
||||
{
|
||||
const unsigned char* buf = (const unsigned char*) ptr;
|
||||
for ( size_t i = n; i != 0; i-- )
|
||||
if ( buf[i-1] == (unsigned char) c )
|
||||
return (void*) (buf + i-1);
|
||||
return NULL;
|
||||
}
|
||||
29
libc/string/stpcpy.c
Normal file
29
libc/string/stpcpy.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/stpcpy.c
|
||||
* Copy a string returning a pointer to its end.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* stpcpy(char* dest, const char* src)
|
||||
{
|
||||
size_t index;
|
||||
for ( index = 0; src[index]; index++ )
|
||||
dest[index] = src[index];
|
||||
dest[index] = '\0';
|
||||
return dest + index;
|
||||
}
|
||||
31
libc/string/stpncpy.c
Normal file
31
libc/string/stpncpy.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/stpncpy.c
|
||||
* Copies a string into a fixed size buffer and returns last byte.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* stpncpy(char* dest, const char* src, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
for ( i = 0; i < n && src[i] != '\0'; i++ )
|
||||
dest[i] = src[i];
|
||||
char* ret = dest + i;
|
||||
for ( ; i < n; i++ )
|
||||
dest[i] = '\0';
|
||||
return ret;
|
||||
}
|
||||
26
libc/string/strcat.c
Normal file
26
libc/string/strcat.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strcat.c
|
||||
* Appends a string onto another string.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* strcat(char* dest, const char* src)
|
||||
{
|
||||
strcpy(dest + strlen(dest), src);
|
||||
return dest;
|
||||
}
|
||||
26
libc/string/strchr.c
Normal file
26
libc/string/strchr.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2013, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strchr.c
|
||||
* Searches a string for a specific character.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* strchr(const char* str, int uc)
|
||||
{
|
||||
char* ret = strchrnul(str, uc);
|
||||
return (unsigned char) uc == ((unsigned char*) ret)[0] ? ret : NULL;
|
||||
}
|
||||
29
libc/string/strchrnul.c
Normal file
29
libc/string/strchrnul.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2013, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strchrnul.c
|
||||
* Searches a string for a specific character.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
char* strchrnul(const char* str, int uc)
|
||||
{
|
||||
const unsigned char* ustr = (const unsigned char*) str;
|
||||
for ( size_t i = 0; true; i++)
|
||||
if ( ustr[i] == (unsigned char) uc || !ustr[i] )
|
||||
return (char*) str + i;
|
||||
}
|
||||
36
libc/string/strcmp.c
Normal file
36
libc/string/strcmp.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strcmp.c
|
||||
* Compares two strings.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
int strcmp(const char* a, const char* b)
|
||||
{
|
||||
for ( size_t i = 0; true; i++ )
|
||||
{
|
||||
unsigned char ac = (unsigned char) a[i];
|
||||
unsigned char bc = (unsigned char) b[i];
|
||||
if ( ac == '\0' && bc == '\0' )
|
||||
return 0;
|
||||
if ( ac < bc )
|
||||
return -1;
|
||||
if ( ac > bc )
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
26
libc/string/strcoll.c
Normal file
26
libc/string/strcoll.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strcoll.c
|
||||
* Compare two strings using the current locale.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int strcoll(const char* s1, const char* s2)
|
||||
{
|
||||
// TODO: Pay attention to locales.
|
||||
return strcmp(s1, s2);
|
||||
}
|
||||
29
libc/string/strcpy.c
Normal file
29
libc/string/strcpy.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strcpy.c
|
||||
* Copies a string and returns dest.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* strcpy(char* dest, const char* src)
|
||||
{
|
||||
size_t index;
|
||||
for ( index = 0; src[index]; index++ )
|
||||
dest[index] = src[index];
|
||||
dest[index] = '\0';
|
||||
return dest;
|
||||
}
|
||||
44
libc/string/strcspn.c
Normal file
44
libc/string/strcspn.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strcspn.c
|
||||
* Search a string for a set of characters.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t strcspn(const char* str, const char* reject)
|
||||
{
|
||||
size_t reject_length = 0;
|
||||
while ( reject[reject_length] )
|
||||
reject_length++;
|
||||
for ( size_t result = 0; true; result++ )
|
||||
{
|
||||
char c = str[result];
|
||||
if ( !c )
|
||||
return result;
|
||||
bool matches = false;
|
||||
for ( size_t i = 0; i < reject_length; i++ )
|
||||
{
|
||||
if ( str[result] != reject[i] )
|
||||
continue;
|
||||
matches = true;
|
||||
break;
|
||||
}
|
||||
if ( matches )
|
||||
return result;
|
||||
}
|
||||
}
|
||||
59
libc/string/stresep.c
Normal file
59
libc/string/stresep.c
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/stresep.c
|
||||
* Extract a token from a string while handling escape characters.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
char* stresep(char** string_ptr, const char* delim, int escape)
|
||||
{
|
||||
char* token = *string_ptr;
|
||||
if ( !token )
|
||||
return NULL;
|
||||
size_t consumed_length = 0;
|
||||
size_t token_length = 0;
|
||||
bool escaped = false;
|
||||
while ( true )
|
||||
{
|
||||
char c = token[consumed_length++];
|
||||
bool true_nul = c == '\0';
|
||||
if ( !escaped && escape && (unsigned char) c == (unsigned char) escape )
|
||||
{
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
if ( !escaped && c != '\0' )
|
||||
{
|
||||
for ( size_t i = 0; delim[i]; i++ )
|
||||
{
|
||||
if ( c != delim[i] )
|
||||
{
|
||||
c = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
token[token_length++] = c;
|
||||
escaped = false;
|
||||
if ( c == '\0' )
|
||||
{
|
||||
*string_ptr = true_nul ? (char*) NULL : token + consumed_length;
|
||||
return token;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
libc/string/strlcat.c
Normal file
28
libc/string/strlcat.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strlcat.c
|
||||
* Appends a string onto another string truncating if the string is too small.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strlcat(char* restrict dest, const char* restrict src, size_t size)
|
||||
{
|
||||
size_t dest_len = strnlen(dest, size);
|
||||
if ( size <= dest_len )
|
||||
return dest_len + strlen(src);
|
||||
return dest_len + strlcpy(dest + dest_len, src, size - dest_len);
|
||||
}
|
||||
31
libc/string/strlcpy.c
Normal file
31
libc/string/strlcpy.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2013, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strlcpy.c
|
||||
* Copies a string and truncates it if the destination is too small.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strlcpy(char* restrict dest, const char* restrict src, size_t size)
|
||||
{
|
||||
if ( !size )
|
||||
return strlen(src);
|
||||
size_t result;
|
||||
for ( result = 0; result < size-1 && src[result]; result++ )
|
||||
dest[result] = src[result];
|
||||
dest[result] = '\0';
|
||||
return result + strlen(src + result);
|
||||
}
|
||||
28
libc/string/strlen.c
Normal file
28
libc/string/strlen.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strlen.c
|
||||
* Returns the length of a string.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strlen(const char* str)
|
||||
{
|
||||
size_t ret = 0;
|
||||
while ( str[ret] )
|
||||
ret++;
|
||||
return ret;
|
||||
}
|
||||
30
libc/string/strncat.c
Normal file
30
libc/string/strncat.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strncat.c
|
||||
* Appends parts of a string onto another string.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* strncat(char* dest, const char* src, size_t n)
|
||||
{
|
||||
size_t dest_len = strlen(dest);
|
||||
size_t i;
|
||||
for ( i = 0; i < n && src[i] != '\0'; i++ )
|
||||
dest[dest_len + i] = src[i];
|
||||
dest[dest_len + i] = '\0';
|
||||
return dest;
|
||||
}
|
||||
36
libc/string/strncmp.c
Normal file
36
libc/string/strncmp.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strncmp.c
|
||||
* Compares a prefix of two strings.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int strncmp(const char* a, const char* b, size_t max_count)
|
||||
{
|
||||
for ( size_t i = 0; i < max_count; i++ )
|
||||
{
|
||||
unsigned char ac = (unsigned char) a[i];
|
||||
unsigned char bc = (unsigned char) b[i];
|
||||
if ( ac == '\0' && bc == '\0' )
|
||||
return 0;
|
||||
if ( ac < bc )
|
||||
return -1;
|
||||
if ( ac > bc )
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
30
libc/string/strncpy.c
Normal file
30
libc/string/strncpy.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strncpy.c
|
||||
* Copies a string into a fixed size buffer and returns dest.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* strncpy(char* dest, const char* src, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
for ( i = 0; i < n && src[i] != '\0'; i++ )
|
||||
dest[i] = src[i];
|
||||
for ( ; i < n; i++ )
|
||||
dest[i] = '\0';
|
||||
return dest;
|
||||
}
|
||||
28
libc/string/strnlen.c
Normal file
28
libc/string/strnlen.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strnlen.c
|
||||
* Returns the length of a fixed length string.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strnlen(const char* str, size_t maxlen)
|
||||
{
|
||||
size_t ret = 0;
|
||||
while ( ret < maxlen && str[ret] )
|
||||
ret++;
|
||||
return ret;
|
||||
}
|
||||
28
libc/string/strpbrk.c
Normal file
28
libc/string/strpbrk.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strpbrk.c
|
||||
* Search a string for any of a set of characters.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* strpbrk(const char* str, const char* accept)
|
||||
{
|
||||
size_t reject_length = strcspn(str, accept);
|
||||
if ( !str[reject_length] )
|
||||
return NULL;
|
||||
return (char*) str + reject_length;
|
||||
}
|
||||
35
libc/string/strrchr.c
Normal file
35
libc/string/strrchr.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2013, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strrchr.c
|
||||
* Searches a string for a specific character.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
char* strrchr(const char* str, int uc)
|
||||
{
|
||||
const unsigned char* ustr = (const unsigned char*) str;
|
||||
const char* last = NULL;
|
||||
for ( size_t i = 0; true; i++ )
|
||||
{
|
||||
if ( ustr[i] == (unsigned char) uc )
|
||||
last = str + i;
|
||||
if ( !ustr[i] )
|
||||
break;
|
||||
}
|
||||
return (char*) last;
|
||||
}
|
||||
38
libc/string/strsep.c
Normal file
38
libc/string/strsep.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strsep.c
|
||||
* Extract a token from a string.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* strsep(char** string_ptr, const char* delim)
|
||||
{
|
||||
char* token = *string_ptr;
|
||||
if ( !token )
|
||||
return NULL;
|
||||
size_t token_length = strcspn(token, delim);
|
||||
if ( token[token_length] )
|
||||
{
|
||||
token[token_length++] = '\0';
|
||||
*string_ptr = token + token_length;
|
||||
}
|
||||
else
|
||||
{
|
||||
*string_ptr = (char*) NULL;
|
||||
}
|
||||
return token;
|
||||
}
|
||||
44
libc/string/strspn.c
Normal file
44
libc/string/strspn.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strspn.c
|
||||
* Search a string for a set of characters.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t strspn(const char* str, const char* accept)
|
||||
{
|
||||
size_t accept_length = 0;
|
||||
while ( accept[accept_length] )
|
||||
accept_length++;
|
||||
for ( size_t result = 0; true; result++ )
|
||||
{
|
||||
char c = str[result];
|
||||
if ( !c )
|
||||
return result;
|
||||
bool matches = false;
|
||||
for ( size_t i = 0; i < accept_length; i++ )
|
||||
{
|
||||
if ( str[result] != accept[i] )
|
||||
continue;
|
||||
matches = true;
|
||||
break;
|
||||
}
|
||||
if ( !matches )
|
||||
return result;
|
||||
}
|
||||
}
|
||||
41
libc/string/strstr.c
Normal file
41
libc/string/strstr.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strstr.c
|
||||
* Locate a substring.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// TODO: This simple and hacky implementation runs in O(N^2) even though this
|
||||
// problem can be solved in O(N).
|
||||
char* strstr(const char* haystack, const char* needle)
|
||||
{
|
||||
if ( !needle[0] )
|
||||
return (char*) haystack;
|
||||
for ( size_t i = 0; haystack[i]; i++ )
|
||||
{
|
||||
bool diff = false;
|
||||
for ( size_t j = 0; needle[j]; j++ )
|
||||
{
|
||||
if ( haystack[i+j] != needle[j] ) { diff = true; break; }
|
||||
}
|
||||
if ( diff )
|
||||
continue;
|
||||
return (char*) haystack + i;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
38
libc/string/strtok_r.c
Normal file
38
libc/string/strtok_r.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strtok_r.c
|
||||
* Extract tokens from strings.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char* strtok_r(char* str, const char* delim, char** saveptr)
|
||||
{
|
||||
if ( !str && !*saveptr )
|
||||
return NULL;
|
||||
if ( !str )
|
||||
str = *saveptr;
|
||||
str += strspn(str, delim); // Skip leading
|
||||
if ( !*str )
|
||||
return *saveptr = NULL;
|
||||
size_t amount = strcspn(str, delim);
|
||||
if ( str[amount] )
|
||||
*saveptr = str + amount + 1;
|
||||
else
|
||||
*saveptr = NULL;
|
||||
str[amount] = '\0';
|
||||
return str;
|
||||
}
|
||||
27
libc/string/strxfrm.c
Normal file
27
libc/string/strxfrm.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2012 Jonas 'Sortie' Termansen.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* string/strxfrm.c
|
||||
* Transform a string such that the result of strcmp is the same as strcoll.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strxfrm(char* dest, const char* src, size_t n)
|
||||
{
|
||||
size_t srclen = strlen(src);
|
||||
strncpy(dest, src, n);
|
||||
return srclen;
|
||||
}
|
||||
17
src/ssp.c
Normal file
17
src/ssp.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <stdint.h>
|
||||
#include <efi.h>
|
||||
#include <efilib.h>
|
||||
|
||||
#define STACK_CHK_GUARD 0x595e9fbd94fda766
|
||||
|
||||
uintptr_t __stack_chk_guard = STACK_CHK_GUARD;
|
||||
|
||||
_Noreturn void
|
||||
__stack_chk_fail(void)
|
||||
{
|
||||
Print(L"stack smashing detected");
|
||||
for (;;) {
|
||||
__asm__ ("cli\n\thlt\n\t" ::);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Reference in a new issue