Added libc code
This commit is contained in:
17
include/assert.h
Normal file
17
include/assert.h
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_ASSERT_H
|
||||
#define _LIBC_ASSERT_H
|
||||
|
||||
#define assert(...) do {} while(0)
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
27
include/ctype.h
Normal file
27
include/ctype.h
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_CTYPE_H
|
||||
#define _LIBC_CTYPE_H
|
||||
|
||||
int isspace(int ch);
|
||||
int isdigit(int ch);
|
||||
int isxdigit(int ch);
|
||||
int isalpha(int ch);
|
||||
int isalnum(int ch);
|
||||
int isprint(int ch);
|
||||
int ispunct(int ch);
|
||||
int isupper(int ch);
|
||||
int islower(int ch);
|
||||
int toupper(int ch);
|
||||
int tolower(int ch);
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
9
include/dirent.h
Normal file
9
include/dirent.h
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
30
include/errno.h
Normal file
30
include/errno.h
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_ERRNO_H
|
||||
#define _LIBC_ERRNO_H
|
||||
|
||||
//extern int errno;
|
||||
|
||||
/* On modern Linux platforms the errno is simulated. This is presumably so that each
|
||||
* thread can have it's own errno without the ABI becoming a huge mess.
|
||||
*/
|
||||
#ifdef __MAC
|
||||
int errno;
|
||||
#else
|
||||
int* __errno_location();
|
||||
|
||||
#define errno __errno_location()[0]
|
||||
#endif
|
||||
|
||||
#define ENOENT 2
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
9
include/fcntl.h
Normal file
9
include/fcntl.h
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
17
include/float.h
Normal file
17
include/float.h
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_FLOAT_H
|
||||
#define _LIBC_FLOAT_H
|
||||
|
||||
#define DBL_MAX_EXP 1024
|
||||
#define DBL_MANT_DIG 53
|
||||
|
||||
#endif
|
18
include/limits.h
Normal file
18
include/limits.h
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_LIMITS_H
|
||||
#define _LIBC_LIMITS_H
|
||||
|
||||
#define CHAR_BIT 8
|
||||
#define UINT_MAX 0xFFFFFFFFU
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
21
include/math.h
Normal file
21
include/math.h
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_MATH_H
|
||||
#define _LIBC_MATH_H
|
||||
|
||||
double fabs(double n);
|
||||
double pow(double x, double p);
|
||||
double exp(double x);
|
||||
|
||||
double ldexp(double x, int exp);
|
||||
|
||||
// From ifndef at top of file:
|
||||
#endif
|
15
include/memory.h
Normal file
15
include/memory.h
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_MEMORY_H
|
||||
#define _LIBC_MEMORY_H
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
9
include/pwd.h
Normal file
9
include/pwd.h
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
28
include/setjmp.h
Normal file
28
include/setjmp.h
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_SETJMP_H
|
||||
#define _LIBC_SETJMP_H
|
||||
|
||||
/*struct jmp_buf_struct {
|
||||
int todo;
|
||||
};
|
||||
|
||||
typedef struct jmp_buf_struct jmp_buf;*/
|
||||
typedef int jmp_buf;
|
||||
|
||||
#define setjmp(x) \
|
||||
0
|
||||
// (printf("WARNING: Unimplemented: setjmp\n") && 0)
|
||||
#define longjmp(x,y) \
|
||||
printf("WARNING: Unimplemented: longjmp\n")
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
9
include/signal.h
Normal file
9
include/signal.h
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
45
include/stdarg.h
Normal file
45
include/stdarg.h
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_STDARG_H
|
||||
#define _LIBC_STDARG_H
|
||||
|
||||
#ifdef _ZCC
|
||||
|
||||
typedef long long** va_list;
|
||||
|
||||
#define _VA_CHECK() \
|
||||
if (__builtin_func_callconv != 101) {\
|
||||
printf("ERROR: Unpacking varargs currently only works with __classic_call (#101). Function %s uses convention %d instead.\n", __func__, __builtin_func_callconv);\
|
||||
}
|
||||
|
||||
#define va_start(list,lastarg) \
|
||||
do {\
|
||||
_VA_CHECK();\
|
||||
list = &lastarg;\
|
||||
list++;\
|
||||
} while(0)
|
||||
|
||||
#define va_arg(list,T) \
|
||||
(*((T*)(list++)))
|
||||
|
||||
#define va_end(list) \
|
||||
do {list = (void*)0;} while(0)
|
||||
|
||||
/* Not new C compiler, use GCC ABI */
|
||||
#else
|
||||
#define va_list __builtin_va_list
|
||||
#define va_start __builtin_va_start
|
||||
#define va_end __builtin_va_end
|
||||
#define va_arg __builtin_va_arg
|
||||
#endif
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
20
include/stdbool.h
Normal file
20
include/stdbool.h
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_STDBOOL_H
|
||||
#define _LIBC_STDBOOL_H
|
||||
|
||||
typedef int bool;
|
||||
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
23
include/stddef.h
Normal file
23
include/stddef.h
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_STDDEF_H
|
||||
#define _LIBC_STDDEF_H
|
||||
|
||||
//#ifndef _LIBC_STDLIB_H
|
||||
typedef long size_t;
|
||||
//#endif
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void*) 0)
|
||||
#endif
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
36
include/stdint.h
Normal file
36
include/stdint.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_STDINT_H
|
||||
#define _LIBC_STDINT_H
|
||||
|
||||
typedef char int8_t;
|
||||
typedef short int16_t;
|
||||
typedef int int32_t;
|
||||
typedef long long int64_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned long long uint64_t;
|
||||
|
||||
typedef int64_t intptr_t;
|
||||
typedef uint64_t uintptr_t;
|
||||
|
||||
typedef uint32_t uint_fast8_t;
|
||||
typedef int32_t int_fast8_t;
|
||||
typedef uint32_t uint_fast16_t;
|
||||
typedef int32_t int_fast16_t;
|
||||
typedef uint32_t uint_fast32_t;
|
||||
typedef int32_t int_fast32_t;
|
||||
typedef uint64_t uint_fast64_t;
|
||||
typedef int64_t int_fast64_t;
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
120
include/stdio.h
Normal file
120
include/stdio.h
Normal file
@@ -0,0 +1,120 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_STDIO_H
|
||||
#define _LIBC_STDIO_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef LIBC_OOP
|
||||
// Theoretical OOP version of FILE, this would be ideal for C+OOP apps
|
||||
// This is split into FILE (base class for interoperable stream objects)
|
||||
// and _libc_FILE (the default FILE implementation using system file
|
||||
// descriptors)
|
||||
|
||||
// Class hierarchy doesn't exist yet...
|
||||
//#import <Resource.h>
|
||||
@interface Resource {
|
||||
}
|
||||
@end
|
||||
/* The extensible FILE base type. */
|
||||
@interface FILE : Resource {
|
||||
FILE* backend;
|
||||
}
|
||||
-(id) init;
|
||||
@end
|
||||
|
||||
/* The subclass of FILE which works over an OS file descriptor. */
|
||||
@interface _libc_FILE : FILE {
|
||||
int fd;
|
||||
int ungot; // Character pushed back or -1
|
||||
int eof;
|
||||
}
|
||||
@end
|
||||
|
||||
#else
|
||||
// Simplified data structure, only intended for bootstrapping (does not
|
||||
// provide complex buffering or overloading)
|
||||
|
||||
struct _libc_FILE_internals {
|
||||
int fd;
|
||||
int ungot; // Character pushed back or -1
|
||||
int eof;
|
||||
};
|
||||
|
||||
typedef struct _libc_FILE_internals FILE;
|
||||
#endif
|
||||
|
||||
#define EXIT_FAILURE 1
|
||||
#define EXIT_SUCCESS 0
|
||||
|
||||
extern FILE* stderr;
|
||||
extern FILE* stdin;
|
||||
extern FILE* stdout;
|
||||
|
||||
#define EOF ((int)-1)
|
||||
|
||||
#ifdef _ZCC
|
||||
#define _LIBC_PRINTF_CALLCONV __classic_call
|
||||
#else
|
||||
#define _LIBC_PRINTF_CALLCONV
|
||||
#endif
|
||||
|
||||
int _LIBC_PRINTF_CALLCONV printf(const char* fmt, ...);
|
||||
int _LIBC_PRINTF_CALLCONV sprintf(char *buf, const char* fmt, ...);
|
||||
int _LIBC_PRINTF_CALLCONV snprintf(char *buf, size_t n, const char* fmt, ...);
|
||||
int _LIBC_PRINTF_CALLCONV dprintf(int fd, const char* fmt, ...);
|
||||
int _LIBC_PRINTF_CALLCONV fprintf(FILE* f, const char* fmt, ...);
|
||||
|
||||
int _LIBC_PRINTF_CALLCONV vfprintf(FILE* f, const char* fmt, va_list list);
|
||||
int _LIBC_PRINTF_CALLCONV vsnprintf(char* str, size_t n, const char* fmt, va_list list);
|
||||
|
||||
// These are non-standard, but the other *printf functions need to be implemented somehow,
|
||||
// so fnprintf/vfnprintf just use a callback function for output of n bytes of string output.
|
||||
typedef int(*_libc_fnprintf_fn_t)(const char* str, int n, void* udata);
|
||||
int _LIBC_PRINTF_CALLCONV _libc_fnprintf(_libc_fnprintf_fn_t fn, void* udata, const char* fmt, ...);
|
||||
int _LIBC_PRINTF_CALLCONV _libc_vfnprintf(_libc_fnprintf_fn_t fn, void* udata, const char* fmt, va_list list);
|
||||
|
||||
FILE* fopen(const char* name, const char* mode);
|
||||
FILE* freopen(const char* name, const char* mode, FILE* f);
|
||||
int fclose(FILE* f);
|
||||
int fflush(FILE* f);
|
||||
|
||||
long fread(void* buffer, long size, long count, FILE* f);
|
||||
long fwrite(void* buffer, long size, long count, FILE* f);
|
||||
|
||||
char* fgets(char*, int, FILE*);
|
||||
|
||||
int fputs(const char*, FILE*);
|
||||
int fputc(int, FILE*);
|
||||
|
||||
int feof(FILE* f);
|
||||
|
||||
void perror(const char*);
|
||||
|
||||
int putc(int c, FILE* f);
|
||||
|
||||
int putchar(int c);
|
||||
|
||||
int getc(FILE* f);
|
||||
int ungetc(int c, FILE* f);
|
||||
|
||||
int fseek(FILE* f, long offset, int wh);
|
||||
#define SEEK_SET 0
|
||||
#define SEEK_CUR 1
|
||||
#define SEEK_END 2
|
||||
|
||||
long ftell(FILE* f);
|
||||
|
||||
long getline(char** linevar, long *nvar, FILE* f);
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
50
include/stdlib.h
Normal file
50
include/stdlib.h
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_STDLIB_H
|
||||
#define _LIBC_STDLIB_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
// By default, this libc includes both traditional and garbage collected
|
||||
// memory managers. _libc_gcalloc is therefore preconfigured as a malloc
|
||||
// alternative with garbage collection, but internally
|
||||
// malloc/calloc/realloc/free are also just wrappers over a flexible
|
||||
// memory manager.
|
||||
|
||||
void* malloc(size_t sz);
|
||||
void* calloc(size_t n, size_t sz);
|
||||
void* realloc(void* mem, size_t sz);
|
||||
void free(void* mem);
|
||||
void* _libc_gcalloc(size_t sz);
|
||||
char* getenv(const char* key);
|
||||
int setenv(const char* key, const char* value, int overwrite);
|
||||
int unsetenv(const char* key);
|
||||
void exit(int x);
|
||||
void abort();
|
||||
|
||||
long strtol(const char* str, char**endvar, int base);
|
||||
long long strtoll(const char* str, char**endvar, int base);
|
||||
unsigned long strtoul(const char* str, char**endvar, int base);
|
||||
unsigned long long strtoull(const char* str, char**endvar, int base);
|
||||
|
||||
float strtof(const char* str, char**endvar);
|
||||
double strtod(const char* str, char**endvar);
|
||||
long double strtold(const char* str, char**endvar);
|
||||
|
||||
double atof(const char* str);
|
||||
int atoi(const char* str);
|
||||
|
||||
int rand();
|
||||
void srand(unsigned int r);
|
||||
#define RAND_MAX 999999999
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
33
include/string.h
Normal file
33
include/string.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_STRING_H
|
||||
#define _LIBC_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
size_t strlen(const char* foo);
|
||||
|
||||
char *strchr(const char* str, int chr);
|
||||
char *strrchr(const char* str, int chr);
|
||||
char* strcat(char* str, const char* cat);
|
||||
char* strcpy(char* buffer, const char* str);
|
||||
char* strncpy(char* buffer, const char* str, size_t n);
|
||||
const char* strpbrk(const char* str, const char* search);
|
||||
int strcmp(const char* a, const char* b);
|
||||
char* strdup(const char* str);
|
||||
char* strndup(const char* str, size_t n);
|
||||
|
||||
void* memcpy(void* dst, const void* src, size_t nbytes);
|
||||
|
||||
void* memset(void* mem, int byt, size_t nbytes);
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
48
include/sys/event.h
Normal file
48
include/sys/event.h
Normal file
@@ -0,0 +1,48 @@
|
||||
// Zak's kqueue implementation
|
||||
#ifndef SYS_EVENT_H
|
||||
#define SYS_EVENT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// The kevent structure is used for waiting/receiving notifications.
|
||||
struct kevent {
|
||||
uintptr_t ident;
|
||||
int16_t filter;
|
||||
uint16_t flags;
|
||||
uint32_t fflags;
|
||||
int64_t data;
|
||||
void* udata;
|
||||
uint64_t ext[4];
|
||||
};
|
||||
|
||||
// kqueue1 is the kqueue system call with a flags argument,
|
||||
// as defined on NetBSD & OpenBSD.
|
||||
int kqueue1(int flags);
|
||||
|
||||
// kqueue() without flags is equivalent to kqueue1(0)
|
||||
#define kqueue() kqueue1(0)
|
||||
|
||||
// On FreeBSD, kqueue1 is also known as kqueuex
|
||||
#define kqueuex(flags) kqueue1(flags)
|
||||
|
||||
// After creating a kqueue, kevent is the main syscall used.
|
||||
// This accepts a list of changes and can receive multiple events.
|
||||
int kevent(int queue, const struct kevent* changes, int nch, struct kevent* events, int nev, void* todo_timeout);
|
||||
|
||||
// The EV_SET() macro is for initialising a struct kevent*
|
||||
#define EV_SET(ev,idn,flt,flg,ffl,dat,udt) \
|
||||
do { \
|
||||
(ev)->ident = idn; \
|
||||
(ev)->filter = flt; \
|
||||
(ev)->flags = flg; \
|
||||
(ev)->fflags = ffl; \
|
||||
(ev)->data = dat; \
|
||||
(ev)->udata = udt; \
|
||||
(ev)->ext[0] = 0; \
|
||||
(ev)->ext[1] = 0; \
|
||||
(ev)->ext[2] = 0; \
|
||||
(ev)->ext[3] = 0; \
|
||||
} while(0)
|
||||
|
||||
// From ifndef at top of file:
|
||||
#endif
|
23
include/syscalls.h
Normal file
23
include/syscalls.h
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
#ifndef _LIBC_SYSCALLS_H
|
||||
#define _LIBC_SYSCALLS_H
|
||||
#include <stdint.h>
|
||||
|
||||
// NOTE: This is NOT the definitive list of system calls, this is just any
|
||||
// syscall definitions which didn't fit anywhere else!
|
||||
|
||||
// TODO: Add some platform-checking here
|
||||
|
||||
void* sbrk(int incr);
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
||||
|
40
include/time.h
Normal file
40
include/time.h
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
|
||||
#ifndef _LIBC_TIME_H
|
||||
#define _LIBC_TIME_H
|
||||
|
||||
typedef long time_t;
|
||||
|
||||
char* ctime(const time_t* timevar);
|
||||
time_t time(time_t* timevar);
|
||||
|
||||
/* TODO: This is not implemented on the current OS. */
|
||||
struct tm {
|
||||
int tm_sec;
|
||||
int tm_min;
|
||||
int tm_hour;
|
||||
int tm_mday;
|
||||
int tm_mon;
|
||||
int tm_year;
|
||||
int tm_wday;
|
||||
int tm_yday;
|
||||
int tm_isdst;
|
||||
};
|
||||
|
||||
struct timespec {
|
||||
int tv_sec;
|
||||
long tv_nsec;
|
||||
};
|
||||
|
||||
struct tm* localtime(const time_t* timep);
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
18
include/unistd.h
Normal file
18
include/unistd.h
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
||||
#ifndef _LIBC_UNISTD_H
|
||||
#define _LIBC_UNISTD_H
|
||||
|
||||
void exit(int status);
|
||||
typedef int pid_t;
|
||||
pid_t getpid();
|
||||
|
||||
/* From ifndef at top of file: */
|
||||
#endif
|
9
include/utime.h
Normal file
9
include/utime.h
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2025, Zak Fenton
|
||||
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||||
// software according to the terms and conditions of the Mulan PSL v2.
|
||||
// You may obtain a copy of Mulan PSL v2 at:
|
||||
// http://license.coscl.org.cn/MulanPSL2
|
||||
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||||
// any kind, either express or implied, including but not limited to
|
||||
// non-infringement, merchantability or fit for a particular purpose.
|
||||
// See the Mulan PSL v2 for more details.
|
Reference in New Issue
Block a user