Convert libc to C.

This commit is contained in:
Jonas 'Sortie' Termansen 2016-02-28 12:11:02 +01:00
parent f633942124
commit 01b59c1947
668 changed files with 1895 additions and 1851 deletions

View File

@ -116,6 +116,7 @@ kernelinfo.o \
kernel.o \
kthread.o \
lfbtextbuffer.o \
libk.o \
linebuffer.o \
log.o \
logterminal.o \

View File

@ -34,9 +34,9 @@
extern "C" {
#endif
const uint32_t DISPMSG_CONTROL_VALID = 1 << 0;
const uint32_t DISPMSG_CONTROL_VGA = 1 << 1;
const uint32_t DISPMSG_CONTROL_OTHER_RESOLUTIONS = 1 << 2;
static const uint32_t DISPMSG_CONTROL_VALID = 1 << 0;
static const uint32_t DISPMSG_CONTROL_VGA = 1 << 1;
static const uint32_t DISPMSG_CONTROL_OTHER_RESOLUTIONS = 1 << 2;
struct dispmsg_string
{

177
kernel/libk.cpp Normal file
View File

@ -0,0 +1,177 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2016.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
libk.cpp
Hooks for libk.
*******************************************************************************/
#include <libk.h>
#include <sortix/mman.h>
#include <sortix/kernel/panic.h>
#include <sortix/kernel/addralloc.h>
#include <sortix/kernel/kernel.h>
#include <sortix/kernel/kthread.h>
#include <sortix/kernel/memorymanagement.h>
#include <sortix/kernel/random.h>
namespace Sortix {
static kthread_mutex_t heap_mutex = KTHREAD_MUTEX_INITIALIZER;
static kthread_mutex_t random_mutex = KTHREAD_MUTEX_INITIALIZER;
extern "C"
void libk_assert(const char* filename,
unsigned long line,
const char* function_name,
const char* expression)
{
Sortix::PanicF("Assertion failure: %s:%lu: %s: %s", filename, line,
function_name, expression);
}
extern "C"
size_t libk_getpagesize(void)
{
return Sortix::Page::Size();
}
extern "C"
void* libk_heap_expand(size_t* num_bytes)
{
// Decide where we would like to add memory to the heap.
uintptr_t mapto = Sortix::GetHeapUpper();
void* mapping = (void*) mapto;
// Attempt to allocate the needed virtual address space such that we can put
// memory there to extend the heap with.
if ( !(*num_bytes = Sortix::ExpandHeap(*num_bytes)) )
return NULL;
// Attempt to map actual memory at our new virtual addresses.
int prot = PROT_KREAD | PROT_KWRITE;
enum Sortix::page_usage page_usage = Sortix::PAGE_USAGE_KERNEL_HEAP;
if ( !Sortix::Memory::MapRange(mapto, *num_bytes, prot, page_usage) )
return NULL;
Sortix::Memory::Flush();
return mapping;
}
extern "C"
void libk_heap_lock(void)
{
kthread_mutex_lock(&heap_mutex);
}
extern "C"
void libk_heap_unlock(void)
{
kthread_mutex_unlock(&heap_mutex);
}
extern "C"
void libk_stack_chk_fail(void)
{
Panic("Stack smashing detected");
}
extern "C"
void libk_abort(void)
{
Sortix::PanicF("abort()");
}
extern "C"
void libk_random_lock(void)
{
kthread_mutex_lock(&random_mutex);
}
extern "C"
void libk_random_unlock(void)
{
kthread_mutex_unlock(&random_mutex);
}
extern "C"
bool libk_hasentropy(void)
{
return Sortix::Random::HasEntropy();
}
extern "C"
void libk_getentropy(void* buffer, size_t size)
{
Sortix::Random::GetEntropy(buffer, size);
}
extern "C"
__attribute__((noreturn))
void libk_ubsan_abort(const char* violation,
const char* filename,
uint32_t line,
uint32_t column)
{
Sortix::PanicF("Undefined behavior: %s at %s:%u:%u",
violation, filename, line, column);
}
extern "C"
void* libk_mmap(size_t size, int prot)
{
size = Page::AlignUp(size);
addralloc_t addralloc;
if ( !AllocateKernelAddress(&addralloc, size) )
return NULL;
if ( !Memory::MapRange(addralloc.from, size, prot, PAGE_USAGE_KERNEL_HEAP) )
{
Memory::Flush();
FreeKernelAddress(&addralloc);
return NULL;
}
Memory::Flush();
return (void*) addralloc.from;
}
extern "C"
void libk_mprotect(void* ptr, size_t size, int prot)
{
addr_t mapto = (addr_t) ptr;
for ( size_t i = 0; i < size; i += Page::Size() )
Memory::PageProtect(mapto + i, prot);
Memory::Flush();
}
extern "C"
void libk_munmap(void* ptr, size_t size)
{
size = Page::AlignUp(size);
addralloc_t addralloc;
addralloc.from = (addr_t) ptr;
addralloc.size = size;
Memory::UnmapRange(addralloc.from, size, PAGE_USAGE_KERNEL_HEAP);
Memory::Flush();
FreeKernelAddress(&addralloc);
}
} // namespace Sortix

View File

@ -1083,7 +1083,7 @@ static size_t shebang_count_arguments(char* line)
// can be shared somehow, you need to keep this comment in sync as well
// as the logic in these files:
// * kernel/process.cpp
// * libc/unistd/execvpe.cpp
// * libc/unistd/execvpe.c
// * utils/which.cpp
// NOTE: See comments in execvpe() for algorithmic commentary.

View File

@ -11,7 +11,7 @@ CPUDIR:=$(CPU)
CPPINCLUDES=-Iinclude
CPPFLAGS=-D__is_sortix_libc $(CPPINCLUDES)
FLAGS=-Wall -Wextra $(OPTLEVEL)
CFLAGS=-std=gnu99
CFLAGS=-std=gnu11 -Wstrict-prototypes -Werror=implicit-function-declaration
CXXFLAGS=-std=gnu++11 -fno-exceptions -fno-rtti
ASFLAGS=
@ -688,7 +688,7 @@ $(CRTOBJ) \
HEADERS:=$(shell find include -type f)
LIBK_OBJS:=$(FREEOBJS:.o=.libk.o)
LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_sortix_kernel
LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_sortix_libk
LIBK_FLAGS:=$(FLAGS) -ffreestanding
LIBK_CFLAGS:=$(CFLAGS)
LIBK_CXXFLAGS:=$(CXXFLAGS)
@ -745,17 +745,17 @@ headers:
%.o: %.c
$(CC) -c $< -o $@ $(CPPFLAGS) $(FLAGS) $(CFLAGS)
%.o: %.cpp
%.o: %.c++
$(CXX) -c $< -o $@ $(CPPFLAGS) $(FLAGS) $(CXXFLAGS)
%.o: %.S
$(CXX) -c $< -o $@ $(CPPFLAGS) $(FLAGS) $(CFLAGS)
$(CC) -c $< -o $@ $(CPPFLAGS) $(FLAGS) $(CFLAGS)
# libk
%.libk.o: %.c
$(CC) -c $< -o $@ $(LIBK_CPPFLAGS) $(LIBK_FLAGS) $(LIBK_CFLAGS)
%.libk.o: %.cpp
%.libk.o: %.c++
$(CXX) -c $< -o $@ $(LIBK_CPPFLAGS) $(LIBK_FLAGS) $(LIBK_CXXFLAGS)
%.libk.o: %.S

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
arpa/inet/inet_ntoa.cpp
arpa/inet/inet_addr.c
Internet address manipulation routines.
*******************************************************************************/
@ -29,8 +29,9 @@
#include <stdio.h>
#include <stdlib.h>
extern "C" char* inet_ntoa(struct in_addr)
in_addr_t inet_addr(const char* cp)
{
(void) cp;
fprintf(stderr, "%s is not implemented yet, aborting.\n", __func__);
abort();
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
arpa/inet/inet_addr.cpp
arpa/inet/inet_ntoa.c
Internet address manipulation routines.
*******************************************************************************/
@ -29,8 +29,9 @@
#include <stdio.h>
#include <stdlib.h>
extern "C" in_addr_t inet_addr(const char*)
char* inet_ntoa(struct in_addr in)
{
(void) in;
fprintf(stderr, "%s is not implemented yet, aborting.\n", __func__);
abort();
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
arpa/inet/inet_ntop.cpp
arpa/inet/inet_ntop.c
Internet address manipulation routines.
*******************************************************************************/
@ -29,9 +29,15 @@
#include <stdio.h>
#include <stdlib.h>
extern "C" const char* inet_ntop(int, const void* restrict, char* restrict,
socklen_t)
const char* inet_ntop(int af,
const void* restrict src,
char* restrict dst,
socklen_t size)
{
(void) af;
(void) src;
(void) dst;
(void) size;
fprintf(stderr, "%s is not implemented yet, aborting.\n", __func__);
abort();
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
arpa/inet/inet_pton.cpp
arpa/inet/inet_pton.c
Internet address manipulation routines.
*******************************************************************************/
@ -29,8 +29,11 @@
#include <stdio.h>
#include <stdlib.h>
extern "C" int inet_pton(int, const char* restrict, void* restrict)
int inet_pton(int af, const char* restrict src, void* restrict dst)
{
(void) af;
(void) src;
(void) dst;
fprintf(stderr, "%s is not implemented yet, aborting.\n", __func__);
abort();
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
assert/__assert.cpp
assert/__assert.c
Reports the occurence of an assertion failure.
*******************************************************************************/
@ -25,19 +25,17 @@
#include <assert.h>
#include <scram.h>
#if defined(__is_sortix_kernel)
#include <sortix/kernel/panic.h>
#ifdef __is_sortix_libk
#include <libk.h>
#endif
extern "C"
void __assert(const char* filename,
unsigned long line,
const char* function_name,
const char* expression)
{
#if defined(__is_sortix_kernel)
Sortix::PanicF("Assertion failure: %s:%lu: %s: %s", filename, line,
function_name, expression);
#ifdef __is_sortix_libk
libk_assert(filename, line, function_name, expression);
#else
struct scram_assert info;
info.filename = filename;

View File

@ -52,7 +52,6 @@
#define BLFRND(s,p,i,j,n) (i ^= F(s,j) ^ (p)[n])
extern "C"
void
Blowfish_encipher(blf_ctx *c, uint32_t *xl, uint32_t *xr)
{
@ -78,7 +77,6 @@ Blowfish_encipher(blf_ctx *c, uint32_t *xl, uint32_t *xr)
*xr = Xl;
}
extern "C"
void
Blowfish_decipher(blf_ctx *c, uint32_t *xl, uint32_t *xr)
{
@ -104,7 +102,6 @@ Blowfish_decipher(blf_ctx *c, uint32_t *xl, uint32_t *xr)
*xr = Xl;
}
extern "C"
void
Blowfish_initstate(blf_ctx *c)
{
@ -384,7 +381,6 @@ Blowfish_initstate(blf_ctx *c)
*c = initstate;
}
extern "C"
uint32_t
Blowfish_stream2word(const uint8_t *data, uint16_t databytes,
uint16_t *current)
@ -406,7 +402,6 @@ Blowfish_stream2word(const uint8_t *data, uint16_t databytes,
return temp;
}
extern "C"
void
Blowfish_expand0state(blf_ctx *c, const uint8_t *key, uint16_t keybytes)
{
@ -444,7 +439,6 @@ Blowfish_expand0state(blf_ctx *c, const uint8_t *key, uint16_t keybytes)
}
}
extern "C"
void
Blowfish_expandstate(blf_ctx *c, const uint8_t *data, uint16_t databytes,
const uint8_t *key, uint16_t keybytes)
@ -488,7 +482,6 @@ Blowfish_expandstate(blf_ctx *c, const uint8_t *data, uint16_t databytes,
}
extern "C"
void
blf_key(blf_ctx *c, const uint8_t *k, uint16_t len)
{
@ -499,7 +492,6 @@ blf_key(blf_ctx *c, const uint8_t *k, uint16_t len)
Blowfish_expand0state(c, k, len);
}
extern "C"
void
blf_enc(blf_ctx *c, uint32_t *data, uint16_t blocks)
{
@ -513,7 +505,6 @@ blf_enc(blf_ctx *c, uint32_t *data, uint16_t blocks)
}
}
extern "C"
void
blf_dec(blf_ctx *c, uint32_t *data, uint16_t blocks)
{
@ -527,7 +518,6 @@ blf_dec(blf_ctx *c, uint32_t *data, uint16_t blocks)
}
}
extern "C"
void
blf_ecb_encrypt(blf_ctx *c, uint8_t *data, uint32_t len)
{
@ -550,7 +540,6 @@ blf_ecb_encrypt(blf_ctx *c, uint8_t *data, uint32_t len)
}
}
extern "C"
void
blf_ecb_decrypt(blf_ctx *c, uint8_t *data, uint32_t len)
{
@ -573,7 +562,6 @@ blf_ecb_decrypt(blf_ctx *c, uint8_t *data, uint32_t len)
}
}
extern "C"
void
blf_cbc_encrypt(blf_ctx *c, uint8_t *iv, uint8_t *data, uint32_t len)
{
@ -599,7 +587,6 @@ blf_cbc_encrypt(blf_ctx *c, uint8_t *iv, uint8_t *data, uint32_t len)
}
}
extern "C"
void
blf_cbc_decrypt(blf_ctx *c, uint8_t *iva, uint8_t *data, uint32_t len)
{

View File

@ -17,8 +17,8 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
c++/c++.cpp
Implements required C++ stuff for use in the Sortix kernel.
c++/c++.c++
Implements required C++ stuff.
*******************************************************************************/
@ -26,5 +26,5 @@
extern "C" void __attribute__ ((weak)) __cxa_pure_virtual()
{
// This shouldn't happen. TODO: Possibly crash the kernel here.
// This shouldn't happen. TODO: Crash here.
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
c++/op-new.cpp
c++/op-new.c++
C++ allocation operators.
*******************************************************************************/

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isalnum.cpp
ctype/isalnum.c
Returns whether the character is a letter or a digit.
*******************************************************************************/
#include <ctype.h>
extern "C" int isalnum(int c)
int isalnum(int c)
{
return isalpha(c) || isdigit(c);
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isalpha.cpp
ctype/isalpha.c
Returns whether the character is a letter.
*******************************************************************************/
#include <ctype.h>
extern "C" int isalpha(int c)
int isalpha(int c)
{
return isupper(c) || islower(c);
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isascii.cpp
ctype/isascii.c
Returns whether the character is an ascii character.
*******************************************************************************/
#include <ctype.h>
extern "C" int isascii(int c)
int isascii(int c)
{
return 0 <= c && c < 128 ? 1 : 0;
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isblank.cpp
ctype/isblank.c
Returns whether the character is blank.
*******************************************************************************/
#include <ctype.h>
extern "C" int isblank(int c)
int isblank(int c)
{
return c == ' ' || c == '\t';
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/iscntrl.cpp
ctype/iscntrl.c
Returns whether the character is a control character.
*******************************************************************************/
#include <ctype.h>
extern "C" int iscntrl(int c)
int iscntrl(int c)
{
return 0 <= c && c < 32;
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isdigit.cpp
ctype/isdigit.c
Returns whether the character is a digit.
*******************************************************************************/
#include <ctype.h>
extern "C" int isdigit(int c)
int isdigit(int c)
{
return '0' <= c && c <= '9';
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isgraph.cpp
ctype/isgraph.c
Returns whether the character is graphical.
*******************************************************************************/
#include <ctype.h>
extern "C" int isgraph(int c)
int isgraph(int c)
{
return '!' <= c && c <= '~';
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/islower.cpp
ctype/islower.c
Returns whether the character is lower-case.
*******************************************************************************/
#include <ctype.h>
extern "C" int islower(int c)
int islower(int c)
{
return 'a' <= c && c <= 'z';
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isprint.cpp
ctype/isprint.c
Returns whether the character is printable.
*******************************************************************************/
#include <ctype.h>
extern "C" int isprint(int c)
int isprint(int c)
{
return isgraph(c) || c == ' ';
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/ispunct.cpp
ctype/ispunct.c
Returns whether the character is punctuational.
*******************************************************************************/
#include <ctype.h>
extern "C" int ispunct(int c)
int ispunct(int c)
{
return isprint(c) && c != ' ' && !isalnum(c);
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isspace.cpp
ctype/isspace.c
Returns whether the character is white-space.
*******************************************************************************/
#include <ctype.h>
extern "C" int isspace(int c)
int isspace(int c)
{
return c == '\t' || c == '\n' || c == '\v' ||
c == '\f' || c == '\r' || c == ' ';

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isupper.cpp
ctype/isupper.c
Returns whether the character is upper-case.
*******************************************************************************/
#include <ctype.h>
extern "C" int isupper(int c)
int isupper(int c)
{
return 'A' <= c && c <= 'Z';
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/isxdigit.cpp
ctype/isxdigit.c
Returns whether the character is a hexadecimal digit.
*******************************************************************************/
#include <ctype.h>
extern "C" int isxdigit(int c)
int isxdigit(int c)
{
if ( isdigit(c) )
return 1;

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/tolower.cpp
ctype/tolower.c
Converts the character to lower-case if it is upper-case.
*******************************************************************************/
#include <ctype.h>
extern "C" int tolower(int c)
int tolower(int c)
{
if ( 'A' <= c && c <= 'Z' )
return 'a' + c - 'A';

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ctype/toupper.cpp
ctype/toupper.c
Converts the character to upper-case if it is lower-case.
*******************************************************************************/
#include <ctype.h>
extern "C" int toupper(int c)
int toupper(int c)
{
if ( 'a' <= c && c <= 'z' )
return 'A' + c - 'a';

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/alphasort.cpp
dirent/alphasort.c
Compare directory entries alphabetically.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <dirent.h>
#include <string.h>
extern "C" int alphasort(const struct dirent** a, const struct dirent** b)
int alphasort(const struct dirent** a, const struct dirent** b)
{
return strcoll((*a)->d_name, (*b)->d_name);
}

View File

@ -17,14 +17,15 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/alphasort_r.cpp
dirent/alphasort_r.c
Compare directory entries alphabetically.
*******************************************************************************/
#include <dirent.h>
extern "C" int alphasort_r(const struct dirent** a, const struct dirent** b, void*)
int alphasort_r(const struct dirent** a, const struct dirent** b, void* ctx)
{
(void) ctx;
return alphasort(a, b);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/closedir.cpp
dirent/closedir.c
Closes a directory stream.
*******************************************************************************/
@ -26,7 +26,7 @@
#include <stdlib.h>
#include <unistd.h>
extern "C" int closedir(DIR* dir)
int closedir(DIR* dir)
{
close(dir->fd);
free(dir->entry);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/dirfd.cpp
dirent/dirfd.c
Returns the file descriptor associated with the directory stream.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <dirent.h>
#include <errno.h>
extern "C" int dirfd(DIR* dir)
int dirfd(DIR* dir)
{
return dir->fd;
}

View File

@ -17,18 +17,18 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/dscandir_r.cpp
dirent/dscandir_r.c
Filtered and sorted directory reading.
*******************************************************************************/
#include <errno.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
extern "C"
int dscandir_r(DIR* dir,
struct dirent*** namelist_ptr,
int (*filter)(const struct dirent*, void*),
@ -51,7 +51,8 @@ int dscandir_r(DIR* dir,
return errno = EOVERFLOW, -1;
}
while ( struct dirent* entry = readdir(dir) )
struct dirent* entry;
while ( (entry = readdir(dir)) )
{
if ( filter && !filter(entry, filter_ctx) )
continue;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/fdopendir.cpp
dirent/fdopendir.c
Handles the file descriptor backend for the DIR* API on Sortix.
*******************************************************************************/
@ -27,7 +27,7 @@
#include <fcntl.h>
#include <stdlib.h>
extern "C" DIR* fdopendir(int fd)
DIR* fdopendir(int fd)
{
int old_dflags = fcntl(fd, F_GETFD);
if ( 0 <= old_dflags && !(old_dflags & FD_CLOEXEC) )

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/opendir.cpp
dirent/opendir.c
Opens a stream for the directory specified by the path.
*******************************************************************************/
@ -27,7 +27,7 @@
#include <stdlib.h>
#include <unistd.h>
extern "C" DIR* opendir(const char* path)
DIR* opendir(const char* path)
{
int fd = open(path, O_SEARCH | O_DIRECTORY | O_CLOEXEC);
if ( fd < 0 )

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/readdir.cpp
dirent/readdir.c
Reads a directory entry from a directory stream into a DIR-specific buffer.
*******************************************************************************/
@ -28,7 +28,7 @@
#include <errno.h>
#include <stdlib.h>
extern "C" struct dirent* readdir(DIR* dir)
struct dirent* readdir(DIR* dir)
{
int old_errno = errno;
struct dirent fallback;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/rewinddir.cpp
dirent/rewinddir.c
Rewinds a directory stream to the start.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <dirent.h>
#include <unistd.h>
extern "C" void rewinddir(DIR* dir)
void rewinddir(DIR* dir)
{
lseek(dir->fd, 0, SEEK_SET);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/scandir.cpp
dirent/scandir.c
Filtered and sorted directory reading.
*******************************************************************************/
@ -39,7 +39,6 @@ static int wrap_compare(const struct dirent** dirent_a,
return ((int (*)(const struct dirent**, const struct dirent**)) function)(dirent_a, dirent_b);
}
extern "C"
int scandir(const char* path, struct dirent*** namelist_ptr,
int (*filter)(const struct dirent*),
int (*compare)(const struct dirent**, const struct dirent**))

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/versionsort.cpp
dirent/versionsort.c
Compare directory entries using strverscmp.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <dirent.h>
#include <string.h>
extern "C" int versionsort(const struct dirent** a, const struct dirent** b)
int versionsort(const struct dirent** a, const struct dirent** b)
{
return strverscmp((*a)->d_name, (*b)->d_name);
}

View File

@ -17,14 +17,15 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirent/versionsort_r.cpp
dirent/versionsort_r.c
Compare directory entries using strverscmp.
*******************************************************************************/
#include <dirent.h>
extern "C" int versionsort_r(const struct dirent** a, const struct dirent** b, void*)
int versionsort_r(const struct dirent** a, const struct dirent** b, void* ctx)
{
(void) ctx;
return versionsort(a, b);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dlfcn/dlfcn.cpp
dlfcn/dlfcn.c
Dynamic linking.
*******************************************************************************/
@ -27,7 +27,7 @@
static const char* dlerrormsg = NULL;
extern "C" void* dlopen(const char* filename, int mode)
void* dlopen(const char* filename, int mode)
{
(void) filename;
(void) mode;
@ -35,7 +35,7 @@ extern "C" void* dlopen(const char* filename, int mode)
return NULL;
}
extern "C" void* dlsym(void* handle, const char* name)
void* dlsym(void* handle, const char* name)
{
(void) handle;
(void) name;
@ -43,13 +43,13 @@ extern "C" void* dlsym(void* handle, const char* name)
return NULL;
}
extern "C" int dlclose(void* handle)
int dlclose(void* handle)
{
(void) handle;
return 0;
}
extern "C" char* dlerror()
char* dlerror(void)
{
const char* result = dlerrormsg;
dlerrormsg = NULL;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
err/err.cpp
err/err.c
Print an error message to stderr and exit the process.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <err.h>
#include <stdarg.h>
extern "C" void err(int exitcode, const char* fmt, ...)
void err(int exitcode, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
err/errx.cpp
err/errx.c
Print an error message to stderr and exit the process.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <err.h>
#include <stdarg.h>
extern "C" void errx(int exitcode, const char* fmt, ...)
void errx(int exitcode, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
err/verr.cpp
err/verr.c
Print an error message to stderr and exit the process.
*******************************************************************************/
@ -26,7 +26,7 @@
#include <stdarg.h>
#include <stdlib.h>
extern "C" void verr(int exitcode, const char* fmt, va_list ap)
void verr(int exitcode, const char* fmt, va_list ap)
{
vwarn(fmt, ap);
exit(exitcode);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
err/verrx.cpp
err/verrx.c
Print an error message to stderr and exit the process.
*******************************************************************************/
@ -26,7 +26,7 @@
#include <stdarg.h>
#include <stdlib.h>
extern "C" void verrx(int exitcode, const char* fmt, va_list ap)
void verrx(int exitcode, const char* fmt, va_list ap)
{
vwarnx(fmt, ap);
exit(exitcode);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
err/vwarn.cpp
err/vwarn.c
Print an error message to stderr.
*******************************************************************************/
@ -28,7 +28,7 @@
#include <stdio.h>
#include <string.h>
extern "C" void vwarn(const char* fmt, va_list ap)
void vwarn(const char* fmt, va_list ap)
{
int errnum = errno;
flockfile(stderr);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
err/vwarnx.cpp
err/vwarnx.c
Print an error message to stderr.
*******************************************************************************/
@ -27,7 +27,7 @@
#include <stdarg.h>
#include <stdio.h>
extern "C" void vwarnx(const char* fmt, va_list ap)
void vwarnx(const char* fmt, va_list ap)
{
flockfile(stderr);
fprintf_unlocked(stderr, "%s", program_invocation_name);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
err/warn.cpp
err/warn.c
Print an error message to stderr.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <err.h>
#include <stdarg.h>
extern "C" void warn(const char* fmt, ...)
void warn(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
err/warnx.cpp
err/warnx.c
Print an error message to stderr.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <err.h>
#include <stdarg.h>
extern "C" void warnx(const char* fmt, ...)
void warnx(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);

View File

@ -17,31 +17,31 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
errno/errno.cpp
errno/errno.c
Value storing a numeric value representing the last occured error.
*******************************************************************************/
#define __SORTIX_STDLIB_REDIRECTS 0
#include <errno.h>
#include <stddef.h>
#if __STDC_HOSTED__
#ifndef __is_sortix_libk
extern "C" { int __thread errno = 0; }
__thread int errno = 0;
#else
extern "C" { int global_errno = 0; }
extern "C" { errno_location_func_t errno_location_func = NULL; }
int global_errno = 0;
errno_location_func_t errno_location_func = NULL;
extern "C" int* get_errno_location(void)
int* get_errno_location(void)
{
if ( errno_location_func ) { return errno_location_func(); }
if ( errno_location_func )
return errno_location_func();
return &global_errno;
}
extern "C" void set_errno_location_func(errno_location_func_t func)
void set_errno_location_func(errno_location_func_t func)
{
errno_location_func = func;
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
error/gnu_error.cpp
error/gnu_error.c
Prints an error message to stderr and optionally exits the process.
*******************************************************************************/
@ -29,7 +29,7 @@
#include <stdlib.h>
#include <string.h>
extern "C" void gnu_error(int status, int errnum, const char *format, ...)
void gnu_error(int status, int errnum, const char *format, ...)
{
flockfile(stderr);

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fcntl/creat.cpp
fcntl/creat.c
Create a file.
*******************************************************************************/
#include <fcntl.h>
extern "C" int creat(const char* path, mode_t mode)
int creat(const char* path, mode_t mode)
{
return open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fcntl/fcntl.cpp
fcntl/fcntl.c
Manipulates a file descriptor.
*******************************************************************************/
@ -31,7 +31,7 @@
DEFN_SYSCALL3(int, sys_fcntl, SYSCALL_FCNTL, int, int, uintptr_t);
extern "C" int fcntl(int fd, int cmd, ...)
int fcntl(int fd, int cmd, ...)
{
uintptr_t arg;
va_list ap;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fcntl/open.cpp
fcntl/open.c
Open a file.
*******************************************************************************/
@ -28,7 +28,7 @@
#include <fcntl.h>
#include <stdarg.h>
extern "C" int open(const char* path, int flags, ...)
int open(const char* path, int flags, ...)
{
mode_t mode = 0;
if ( flags & O_CREAT )

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fcntl/openat.cpp
fcntl/openat.c
Open a file relative to directory.
*******************************************************************************/
@ -29,7 +29,7 @@
DEFN_SYSCALL4(int, sys_openat, SYSCALL_OPENAT, int, const char*, int, mode_t);
extern "C" int openat(int dirfd, const char* path, int flags, ...)
int openat(int dirfd, const char* path, int flags, ...)
{
mode_t mode = 0;
if ( flags & O_CREAT )

View File

@ -17,13 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fnmatch/fnmatch.cpp
fnmatch/fnmatch.c
Filename matching.
*******************************************************************************/
#include <errno.h>
#include <fnmatch.h>
#include <stdbool.h>
#include <stddef.h>
#define __FNM_NOT_LEADING (1 << 31)
@ -91,7 +92,7 @@ static bool matches_bracket_pattern(char c, const char* pattern, int flags)
return negated || matched_any;
}
extern "C" int fnmatch(const char* pattern, const char* string, int flags)
int fnmatch(const char* pattern, const char* string, int flags)
{
int next_flags = flags | __FNM_NOT_LEADING;
const char* pattern_end;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fsmarshall/fsm_fsbind.cpp
fsmarshall/fsm_fsbind.c
Binds a user-space filesystem inode at a mount point.
*******************************************************************************/
@ -30,7 +30,7 @@
DEFN_SYSCALL3(int, sys_fsm_fsbind, SYSCALL_FSM_FSBIND, int, int, int);
extern "C" int fsm_fsbind(int rootfd, int mountpoint, int flags)
int fsm_fsbind(int rootfd, int mountpoint, int flags)
{
return sys_fsm_fsbind(rootfd, mountpoint, flags);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fsmarshall/fsm_mountat.cpp
fsmarshall/fsm_mountat.c
Attaches a user-space filesystem at the specified mount point.
*******************************************************************************/
@ -28,7 +28,7 @@
DEFN_SYSCALL4(int, sys_fsm_mountat, SYSCALL_FSM_MOUNTAT, int, const char*, const struct stat*, int);
extern "C" int fsm_mountat(int dirfd, const char* path, const struct stat* rootst, int flags)
int fsm_mountat(int dirfd, const char* path, const struct stat* rootst, int flags)
{
return sys_fsm_mountat(dirfd, path, rootst, flags);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/endfsent.cpp
fstab/endfsent.c
Closes the filesystem table.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <fstab.h>
#include <stdio.h>
extern "C" void endfsent(void)
void endfsent(void)
{
if ( !__fstab_file )
return;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/getfsent.cpp
fstab/getfsent.c
Read filesystem table entry.
*******************************************************************************/
@ -26,7 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
extern "C" struct fstab* getfsent(void)
struct fstab* getfsent(void)
{
if ( !__fstab_file && !setfsent() )
return NULL;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/getfsfile.cpp
fstab/getfsfile.c
Lookup filesystem table by mount point.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <fstab.h>
#include <string.h>
extern "C" struct fstab* getfsfile(const char* mount_point)
struct fstab* getfsfile(const char* mount_point)
{
if ( !setfsent() )
return NULL;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/getfsspec.cpp
fstab/getfsspec.c
Lookup filesystem table by special file.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <fstab.h>
#include <string.h>
extern "C" struct fstab* getfsspec(const char* special_file)
struct fstab* getfsspec(const char* special_file)
{
if ( !setfsent() )
return NULL;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/scanfsent.cpp
fstab/scanfsent.c
Parse filesystem table entry.
*******************************************************************************/
@ -80,7 +80,7 @@ char* find_fstype(char* str)
return NULL;
}
extern "C" int scanfsent(char* str, struct fstab* fs)
int scanfsent(char* str, struct fstab* fs)
{
char* str_freq;
char* str_passno;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/setfsent.cpp
fstab/setfsent.c
Open filesystem table.
*******************************************************************************/
@ -25,9 +25,9 @@
#include <fstab.h>
#include <stdio.h>
extern "C" { FILE* __fstab_file = NULL; }
FILE* __fstab_file = NULL;
extern "C" int setfsent(void)
int setfsent(void)
{
if ( __fstab_file )
rewind(__fstab_file);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
getopt/getopt.cpp
getopt/getopt.c
Command-line parsing utility function.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <stddef.h>
#include <getopt.h>
extern "C" int getopt(int argc, char* const* argv, const char* shortopts)
int getopt(int argc, char* const* argv, const char* shortopts)
{
return getopt_long(argc, argv, shortopts, NULL, NULL);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
getopt/getopt_long.cpp
getopt/getopt_long.c
Command-line parsing utility function.
*******************************************************************************/
@ -26,12 +26,13 @@
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <stdbool.h>
#include <string.h>
extern "C" { char* optarg = NULL; }
extern "C" { int opterr = 1; }
extern "C" { int optind = 1; }
extern "C" { int optopt = 0; }
char* optarg = NULL;
int opterr = 1;
int optind = 1;
int optopt = 0;
static char* const* optcurargv;
static const char* optcurarg;
@ -93,7 +94,6 @@ const struct option* find_long_option(char* arg,
return NULL;
}
extern "C"
int getopt_long(int argc, char* const* argv, const char* shortopts,
const struct option* longopts, int* longindex)
{

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/endgrent.cpp
grp/endgrent.c
Closes the group database.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <grp.h>
#include <stdio.h>
extern "C" void endgrent(void)
void endgrent(void)
{
if ( !__grp_file )
return;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/fgetgrent.cpp
grp/fgetgrent.c
Reads a group entry from a FILE in a thread-insecure manner.
*******************************************************************************/
@ -26,7 +26,7 @@
#include <grp.h>
#include <stdlib.h>
extern "C" struct group* fgetgrent(FILE* fp)
struct group* fgetgrent(FILE* fp)
{
static struct group result_object;
static char* buf = NULL;
@ -39,8 +39,9 @@ extern "C" struct group* fgetgrent(FILE* fp)
buflen = new_buflen;
}
struct group* result;
int errnum;
retry:
int errnum = fgetgrent_r(fp, &result_object, buf, buflen, &result);
errnum = fgetgrent_r(fp, &result_object, buf, buflen, &result);
if ( errnum == ERANGE )
{
size_t new_buflen = 2 * buflen;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/fgetgrent_r.cpp
grp/fgetgrent_r.c
Reads a group entry from a FILE.
*******************************************************************************/
@ -25,9 +25,10 @@
#include <sys/types.h>
#include <errno.h>
#include <inttypes.h>
#include <grp.h>
#include <inttypes.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@ -106,7 +107,6 @@ static char* next_member(char** current)
return result;
}
extern "C"
int fgetgrent_r(FILE* restrict fp,
struct group* restrict result,
char* restrict buf,
@ -158,7 +158,7 @@ int fgetgrent_r(FILE* restrict fp,
if ( !buf_used && feof(fp) )
{
funlockfile(fp);
return *result_pointer = NULL, errno = original_errno, NULL;
return *result_pointer = NULL, errno = original_errno, 0;
}
if ( buf_used == buf_len )

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/getgrent.cpp
grp/getgrent.c
Reads a group entry in a thread-insecure manner.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <grp.h>
#include <stddef.h>
extern "C" struct group* getgrent(void)
struct group* getgrent(void)
{
if ( !__grp_file && !(__grp_file = opengr()) )
return NULL;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/getgrent_r.cpp
grp/getgrent_r.c
Reads a group entry (but not fully thread-securely).
*******************************************************************************/
@ -25,7 +25,6 @@
#include <errno.h>
#include <grp.h>
extern "C"
int getgrent_r(struct group* restrict result,
char* restrict buf,
size_t buflen,

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/getgrgid.cpp
grp/getgrgid.c
Searchs the group database for a group with the given numeric group id in a
thread-insecure manner.
@ -27,7 +27,7 @@
#include <grp.h>
#include <stdlib.h>
extern "C" struct group* getgrgid(gid_t gid)
struct group* getgrgid(gid_t gid)
{
static struct group result_object;
static char* buf = NULL;
@ -40,8 +40,9 @@ extern "C" struct group* getgrgid(gid_t gid)
buflen = new_buflen;
}
struct group* result;
int errnum;
retry:
int errnum = getgrgid_r(gid, &result_object, buf, buflen, &result);
errnum = getgrgid_r(gid, &result_object, buf, buflen, &result);
if ( errnum == ERANGE )
{
size_t new_buflen = 2 * buflen;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/getgrgid_r.cpp
grp/getgrgid_r.c
Searchs the group database for a group with the given numeric group id.
*******************************************************************************/
@ -26,7 +26,6 @@
#include <grp.h>
#include <stdio.h>
extern "C"
int getgrgid_r(gid_t gid,
struct group* restrict ret,
char* restrict buf,

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/getgrnam.cpp
grp/getgrnam.c
Searches the group database for a user with the given groupname in a
thread-insecure manner.
@ -27,7 +27,7 @@
#include <grp.h>
#include <stdlib.h>
extern "C" struct group* getgrnam(const char* groupname)
struct group* getgrnam(const char* groupname)
{
static struct group result_object;
static char* buf = NULL;
@ -40,8 +40,9 @@ extern "C" struct group* getgrnam(const char* groupname)
buflen = new_buflen;
}
struct group* result;
int errnum;
retry:
int errnum = getgrnam_r(groupname, &result_object, buf, buflen, &result);
errnum = getgrnam_r(groupname, &result_object, buf, buflen, &result);
if ( errnum == ERANGE )
{
size_t new_buflen = 2 * buflen;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/getgrnam_r.cpp
grp/getgrnam_r.c
Searchs the group database for a group with the given groupname.
*******************************************************************************/
@ -27,7 +27,6 @@
#include <stdio.h>
#include <string.h>
extern "C"
int getgrnam_r(const char* restrict groupname,
struct group* restrict ret,
char* restrict buf,

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/opengr.cpp
grp/opengr.c
Opens the group database and returns a FILE to it.
*******************************************************************************/
@ -25,7 +25,7 @@
#include <grp.h>
#include <stdio.h>
extern "C" FILE* opengr(void)
FILE* opengr(void)
{
return fopen("/etc/group", "r");
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
grp/setgrent.cpp
grp/setgrent.c
Rewinds the group database.
*******************************************************************************/
@ -25,9 +25,9 @@
#include <grp.h>
#include <stdio.h>
extern "C" { FILE* __grp_file = NULL; }
FILE* __grp_file = NULL;
extern "C" void setgrent(void)
void setgrent(void)
{
if ( __grp_file )
rewind(__grp_file);

View File

@ -72,8 +72,8 @@ typedef struct __FILE FILE;
#define _FILE_MAX_PUSHBACK 8
/* Note stdio/stdio.cpp's declarations of stdin/stdout/stderr also needs to be
changed if you make changes to this structure. */
/* Note libc's declarations of stdin/stdout/stderr also needs to be changed if
you make changes to this structure. */
struct __FILE
{
unsigned char* buffer;

View File

@ -37,7 +37,7 @@ extern "C" {
#define RTLD_LOCAL 0 /* Bit 8 is not set. */
int dlclose(void* handle);
char* dlerror();
char* dlerror(void);
void* dlopen(const char* filename, int mode);
void* dlsym(void* handle, const char* name);

63
libc/include/libk.h Normal file
View File

@ -0,0 +1,63 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014, 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
libk.h
Standalone C library hooks.
*******************************************************************************/
#ifndef INCLUDE_LIBK_H
#define INCLUDE_LIBK_H
#include <sys/cdefs.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
__attribute__((noreturn))
void libk_assert(const char*, unsigned long, const char*, const char*);
size_t libk_getpagesize(void);
void* libk_heap_expand(size_t*);
void libk_heap_lock(void);
void libk_heap_unlock(void);
__attribute__((noreturn))
void libk_stack_chk_fail(void);
__attribute__((noreturn))
void libk_abort(void);
void libk_random_lock(void);
void libk_random_unlock(void);
bool libk_hasentropy(void);
void libk_getentropy(void*, size_t);
__attribute__((noreturn))
void libk_ubsan_abort(const char*, const char*, uint32_t, uint32_t);
void* libk_mmap(size_t, int);
void libk_mprotect(void*, size_t, int);
void libk_munmap(void*, size_t);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif

View File

@ -54,10 +54,6 @@
/* Macro to declare a weak alias. */
#if defined(__is_sortix_libc)
#ifdef __cplusplus
#define weak_alias_cxx(old, new, mangled) \
extern "C" { extern __typeof(old) new __attribute__((weak, alias(mangled))); }
#endif
#define weak_alias(old, new) \
extern __typeof(old) new __attribute__((weak, alias(#old)))
#endif

View File

@ -88,7 +88,7 @@ syscall_type syscall_name syscall_formals;
/* System call accepting no parameters. */
#define DEFN_SYSCALL0(type, fn, num) \
DEFINE_SYSCALL(type, fn, num, ())
DEFINE_SYSCALL(type, fn, num, (void))
/* System call accepting 1 parameter. */
#define DEFN_SYSCALL1(type, fn, num, t1) \

View File

@ -101,7 +101,7 @@ static __inline struct timespec timespec_neg(struct timespec t)
return timespec_make(-t.tv_sec, 0);
}
static __inline struct timespec timespec_nul()
static __inline struct timespec timespec_nul(void)
{
return timespec_make(0, 0);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
init/init.cpp
init/init.c
Initializes the process by setting up the heap, signal handling, static
memory and other useful things.
@ -27,8 +27,8 @@
#include <pthread.h>
#include <string.h>
extern "C" { char* program_invocation_name; }
extern "C" { char* program_invocation_short_name; }
char* program_invocation_name;
char* program_invocation_short_name;
static char* find_last_elem(char* str)
{
@ -39,7 +39,7 @@ static char* find_last_elem(char* str)
return str;
}
extern "C" void initialize_standard_library(int argc, char* argv[])
void initialize_standard_library(int argc, char* argv[])
{
const char* argv0 = argc ? argv[0] : "";
program_invocation_name = (char*) argv0;

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
inttypes/imaxabs.cpp
inttypes/imaxabs.c
Computes the absolute value of an integer.
*******************************************************************************/
#include <inttypes.h>
extern "C" intmax_t imaxabs(intmax_t val)
intmax_t imaxabs(intmax_t val)
{
return val < 0 ? -val : val;
}

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
inttypes/imaxdiv.cpp
inttypes/imaxdiv.c
Compute quotient and remainder of integer division.
*******************************************************************************/
#include <inttypes.h>
extern "C" imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom)
imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom)
{
imaxdiv_t ret;
ret.quot = numer / denom;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
inttypes/strtoimax.cpp
inttypes/strtoimax.c
Converts integers represented as strings to binary representation.
*******************************************************************************/
@ -33,4 +33,4 @@
#define STRTOL_INT_MAX INTMAX_MAX
#define STRTOL_INT_IS_UNSIGNED false
#include "../stdlib/strtol.cpp"
#include "../stdlib/strtol.c"

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
inttypes/strtoumax.cpp
inttypes/strtoumax.c
Converts integers represented as strings to binary representation.
*******************************************************************************/
@ -33,4 +33,4 @@
#define STRTOL_INT_MAX UINTMAX_MAX
#define STRTOL_INT_IS_UNSIGNED true
#include "../stdlib/strtol.cpp"
#include "../stdlib/strtol.c"

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
inttypes/wcstoimax.cpp
inttypes/wcstoimax.c
Converts integers represented as strings to binary representation.
*******************************************************************************/
@ -33,4 +33,4 @@
#define STRTOL_INT_MAX INTMAX_MAX
#define STRTOL_INT_IS_UNSIGNED false
#include "../stdlib/strtol.cpp"
#include "../stdlib/strtol.c"

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
inttypes/wcstoumax.cpp
inttypes/wcstoumax.c
Converts integers represented as strings to binary representation.
*******************************************************************************/
@ -33,4 +33,4 @@
#define STRTOL_INT_MAX UINTMAX_MAX
#define STRTOL_INT_IS_UNSIGNED true
#include "../stdlib/strtol.cpp"
#include "../stdlib/strtol.c"

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ioleast/preadall.cpp
ioleast/preadall.c
Reads as much data as requested from the given offset.
*******************************************************************************/
#include <ioleast.h>
extern "C" size_t preadall(int fd, void* buf, size_t count, off_t off)
size_t preadall(int fd, void* buf, size_t count, off_t off)
{
return preadleast(fd, buf, count, count, off);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ioleast/preadleast.cpp
ioleast/preadleast.c
Reads at least as much data as requested or more from the given offset.
*******************************************************************************/
@ -28,7 +28,6 @@
#include <stdint.h>
#include <unistd.h>
extern "C"
size_t preadleast(int fd, void* buf_ptr, size_t least, size_t max, off_t off)
{
assert(least <= max);

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ioleast/pwriteall.cpp
ioleast/pwriteall.c
Writes as much data as requested at the given offset.
*******************************************************************************/
#include <ioleast.h>
extern "C" size_t pwriteall(int fd, const void* buf, size_t count, off_t off)
size_t pwriteall(int fd, const void* buf, size_t count, off_t off)
{
return pwriteleast(fd, buf, count, count, off);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ioleast/pwriteleast.cpp
ioleast/pwriteleast.c
Writes at least as much data as requested or more at the given offset.
*******************************************************************************/
@ -28,7 +28,6 @@
#include <stdint.h>
#include <unistd.h>
extern "C"
size_t pwriteleast(int fd, const void* buf_ptr, size_t least, size_t max, off_t off)
{
assert(least <= max);

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ioleast/readall.cpp
ioleast/readall.c
Reads as much data as requested.
*******************************************************************************/
#include <ioleast.h>
extern "C" size_t readall(int fd, void* buf, size_t count)
size_t readall(int fd, void* buf, size_t count)
{
return readleast(fd, buf, count, count);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ioleast/readleast.cpp
ioleast/readleast.c
Reads at least as much data as requested or more.
*******************************************************************************/
@ -28,7 +28,6 @@
#include <stdint.h>
#include <unistd.h>
extern "C"
size_t readleast(int fd, void* buf_ptr, size_t least, size_t max)
{
assert(least <= max);

View File

@ -17,14 +17,14 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ioleast/writeall.cpp
ioleast/writeall.c
Writes as much data as requested.
*******************************************************************************/
#include <ioleast.h>
extern "C" size_t writeall(int fd, const void* buf, size_t count)
size_t writeall(int fd, const void* buf, size_t count)
{
return writeleast(fd, buf, count, count);
}

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
ioleast/writeleast.cpp
ioleast/writeleast.c
Writes at least as much data as requested or more.
*******************************************************************************/
@ -28,7 +28,6 @@
#include <stdint.h>
#include <unistd.h>
extern "C"
size_t writeleast(int fd, const void* buf_ptr, size_t least, size_t max)
{
assert(least <= max);

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
libgen/basename.cpp
libgen/basename.c
Returns the name part of a path.
*******************************************************************************/
@ -27,7 +27,7 @@
static const char current_directory[2] = ".";
extern "C" char* basename(char* path)
char* basename(char* path)
{
if ( !path || !*path )
return (char*) current_directory;

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
libgen/dirname.cpp
libgen/dirname.c
Returns the directory part of a path.
*******************************************************************************/
@ -27,7 +27,7 @@
static const char current_directory[2] = ".";
extern "C" char* dirname(char* path)
char* dirname(char* path)
{
if ( !path || !*path )
return (char*) current_directory;

Some files were not shown because too many files have changed in this diff Show More