Added a bunch of stuff

This commit is contained in:
Gitea 2020-12-01 17:40:03 -06:00
parent 66350160b0
commit 77c2abd247
66 changed files with 1808 additions and 0 deletions

120
Some libc slash libk todos Normal file
View File

@ -0,0 +1,120 @@
/* NB: This list is ordered. i.e. cat needs stuff from asa to work! */
For sh
======
stdio.h
*******
fgets()
stdlib.h
********
exit()
free()
calloc()
unistd.h
********
chdir()
fork()
execvp()
getcwd()
sys/wait.h
**********
waitpid()
signal.h
********
signal()
SIG_DFL
SIG_IGN
For asa
=======
stdio.h
*******
fprintf()
fgetc()
fopen()
fclose()
For cal
=======
stdio.h
*******
fileno()
unistd.h
********
isatty()
For cat
=======
unistd.h
********
write()
For cksum
=========
stdio.h
*******
fseek()
SEEK_SET
For expand
==========
stdlib.h
********
malloc() (if not already implemented for calloc)
For link
========
unistd.h
********
link()
For ln
======
unistd.h
********
access()
unlink()
symlink()
fcntl.h
*******
AT_SYMLINK_FOLLOW
linkat()
AT_FDCWD
sys/stat.h
**********
struct stat
lstat()
S_ISLNK
string.h
********
strcpy()
For pwd
=======
stdlib.h
********
getenv();
For tty
=======
unistd.h
********
ttyname()

29
arch/i386/crt0.s Normal file
View File

@ -0,0 +1,29 @@
.section .text
.global _start
_start:
# Set up end of stack frame linked list
movl $0, %ebp
pushl %ebp # rip = 0
pushl %ebp # ebp = 0
movl %esp, %ebp
pushl %esi
pushl %edi
# Prepare signals, memory alloc, stdio, etc.
call initialize_standard_library
# Run global constructors
call _init
# restore argc/argv
popl %edi
popl %esi
# Run main
call main
# Terminate with exit code
movl %eax, %edi
call exit
.size _start, . - _start

8
arch/i386/inb.s Normal file
View File

@ -0,0 +1,8 @@
.globl inb
.p2align 2
.type inb,%function
inb:
.fnstart
.fnend

12
include/assert.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _ASSERT_H
#define _ASSERT_H
#include <stdio.h>
#ifdef NDEBUG
#define assert(ignore) ((void) 0)
#else
#define assert(expression) (expression || (fprintf(stderr, "%s: %d: %s", __FILE__, __LINE__, __func__) && abort()))
#endif
#endif

34
include/cpio.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef _CPIO_H
#define _CPIO_H
/* Constants needed by the c_mode field of the cpio archive format */
/* User permissions */
#define C_IRUSR 0000400
#define C_IWUSR 0000200
#define C_IXUSR 0000100
/* Group permissions */
#define C_IRGRP 0000040
#define C_IWGRP 0000020
#define C_IXGRP 0000010
/* Other folx permission */
#define C_IROTH 0000004
#define C_IWOTH 0000002
#define C_IXOTH 0000001
#define C_ISUID 0004000 /* Set UID */
#define C_ISGID 0002000 /* Set GID */
#define C_ISVTX 0001000 /* Restricted deletion flag for directories */
#define C_ISFIFO 0010000 /* FIFO/Pipe */
#define C_ISREG 0100000 /* Regular file */
#define C_ISBLK 0060000 /* Block device */
#define C_ISCHR 0020000 /* Character special */
#define C_ISCTG 0110000 /* Reserved */
#define C_ISLNK 0120000 /* Symbolic link */
#define C_ISSOCK 0140000 /* Socket */
#define MAGIC "070707"
#endif

47
include/ctype.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef _CTYPE_H
#define _CTYPE_H
#include <types/locale_t.h>
/* C standard functions */
int isalnum(int);
int isalpha(int);
int isblank(int);
int iscntrl(int);
int isdigit(int);
int isgraph(int);
int islower(int);
int isprint(int);
int ispunct(int);
int isspace(int);
int isupper(int);
int isxdigit(int);
/* POSIX extensions */
int isalnum_l(int, locale_t);
int isalpha_l(int, locale_t);
int isblank_l(int, locale_t);
int iscntrl_l(int, locale_t);
int isdigit_l(int, locale_t);
int isgraph_l(int, locale_t);
int islower_l(int, locale_t);
int isprint_l(int, locale_t);
int ispunct_l(int, locale_t);
int isspace_l(int, locale_t);
int isupper_l(int, locale_t);
int isxdigit_l(int, locale_t);
/* C standard stuff */
int toascii(int);
int tolower(int);
int toupper(int);
/* POSIX extensions */
int tolower_l(int, locale_t);
int toupper_l(int, locale_t);
/* Obsolete XSI macros */
#define _toupper(x) toupper(x)
#define _tolower(x) tolower(x)
#endif

24
include/float.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef _FLOAT_H
#define _FLOAT_H
#define FLT_ROUNDS 1
#define FLT_EVAL_METHOD 0
#define FLT_RADIX 2
#define FLT_MANT_DIG 24
#define DBL_MANT_DIG 53
#define LDBL_MANT_DIG 113
#define DECIMAL_DIG 10
#define FLT_DIG 6
#define DBL_DIG 10
#define LDBL_DIG 10
#define FLT_MIN_10_EXP -37
#define DBL_MIN_10_EXP -37
#define LDBL_MIN_10_EXP -37
#endif

55
include/netinet/in.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef _NETINET_IN_H
#define _NETINET_IN_H
#include <inttypes.h>
#include <sys/socket.h>
#ifndef __have_in_port_t
#define __have_in_port_t
typedef uint16_t in_port_t;
#endif
#ifndef __have_in_addr_t
#define __have_in_addr_t
typedef uint32_t in_addr_t;
#endif
typedef struct _ipv4_in_addr {
in_addr_t s_addr;
} in_addr;
typedef struct _ipv4_sockaddr_in {
sa_family_t sin_family;
in_port_t sin_port;
in_addr sin_addr;
} sockaddr_in;
typedef struct _ipv6_in_addr {
uint8_t s6_addr[16];
} in6_addr;
typedef struct _ipv6_sockaddr_in {
sa_family_t sin6_family;
in_port_t sin6_port;
uint32_t sin6_flowinfo;
in6_addr sin6_addr;
uint32_t sin6_scope_id;
} sockaddr_in6;
extern const in6_addr in6addr_any;
extern const in6_addr in6addr_loopback;
/* Need INIT macros for the above */
typedef struct _ipv6_mreq {
int6_addr ipv6mr_multiaddr;
unsigned ipv6mr_interface;
} ipv6_mreq;
#define IPPROTO_IP 4
#define IPPROTO_IPV6 6
#define IPPROTO_ICMP 1
#define IPPROTO_RAW 2
#define IPPROTO_TCP 5
#define IPPROTO_UDP 7
#endif

6
include/sys/io.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _SYS_IO_H
#define _SYS_IO_H
extern unsigned char inb(unsigned short int port);
#endif

24
include/sys/socket.h Normal file
View File

@ -0,0 +1,24 @@
/*
* <sys/socket.h> - main sockets header
*
* This header is a part of the FENIX C Library and is free software.
* You can redistribute and/or modify it subject to the terms of the
* Clumsy Wolf Public License v4. For more details, see the file COPYING.
*
* The FENIX C Library is distributed WITH NO WARRANTY WHATSOEVER. See
* The CWPL for more details.
*/
#ifndef _SYS_SOCKET_H
#define _SYS_SOCKET_H
typedef long int socklen_t;
#include <types/sa_family_t.h>
typedef struct _socket_address {
sa_family_t sa_family;
char sa_data[];
} sockaddr;
#endif

22
include/sys/un.h Normal file
View File

@ -0,0 +1,22 @@
/*
* <sys/un.h> - definitions for UNIX domain sockets
*
* This header is a part of the FENIX C Library and is free software.
* You can redistribute and/or modify it subject to the terms of the
* Clumsy Wolf Public License v4. For more details, see the file COPYING.
*
* The FENIX C Library is distributed WITH NO WARRANTY WHATSOEVER. See
* The CWPL for more details.
*/
#ifndef _SYS_UN_H
#define _SYS_UN_H
#include <types/sa_family_t.h>
typedef struct _un_sockaddr {
sa_family_t sun_family
char sun_path[108];
} sockaddr_un;
#endif

14
include/sys/utsname.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef _SYS_UTSNAME_H
#define _SYS_UTSNAME_H
typedef struct _uname {
char sysname[70];
char nodename[70];
char release[70];
char version[70];
char machine[70];
} utsname;
int uname(utsname *);
#endif

34
include/sys/wait.h Normal file
View File

@ -0,0 +1,34 @@
/*
* <sys/wait.h> - declarations for waiting
*
* This header is a part of the FENIX C Library and is free software.
* You can redistribute and/or modify it subject to the terms of the
* Clumsy Wolf Public License v4. For more details, see the file COPYING.
*
* The FENIX C Library is distributed WITH NO WARRANTY WHATSOEVER. See
* The CWPL for more details.
*/
#ifndef _SYS_WAIT_H
#define _SYS_WAIT_H
/* Constants for waitpid() */
#define WCONTINUED 1
#define WNOHANG 2
#define WUNTRACED 3
/* Options for waitid() */
#define WEXITED 1
#define WNOWAIT 2
#define WSTOPPED 3
enum idtype_t {P_ALL, P_PGID, P_PID};
#include <types/pid_t.h>
#include <signal.h>
pid_t wait(int *);
int waitid(idtype_t, id_t, siginfo_t *, int);
pid_t waitpid(pid_t, int *, int);
#endif

48
include/syslog.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef _SYSLOG_H
#define _SYSLOG_H
/* logopt option for openlog() */
#define LOG_PID 001
#define LOG_CONS 002
#define LOG_NDELAY 004
#define LOG_ODELAY 010
#define LOG_NOWAIT 020
/* facility argument for openlog() */
#define LOG_KERN 1
#define LOG_USER 2
#define LOG_MAIL 3
#define LOG_NEWS 4
#define LOG_UUCP 5
#define LOG_DAEMON 6
#define LOG_AUTH 7
#define LOG_CRON 8
#define LOG_LPR 9
#define LOG_LOCAL0 10
#define LOG_LOCAL1 11
#define LOG_LOCAL2 12
#define LOG_LOCAL3 13
#define LOG_LOCAL4 14
#define LOG_LOCAL5 15
#define LOG_LOCAL6 16
#define LOG_LOCAL7 17
/* priority argument for syslog() */
#define LOG_EMERG 1
#define LOG_CRIT 2
#define LOG_ALERT 3
#define LOG_ERROR 4
#define LOG_WARNING 5
#define LOG_NOTICE 6
#define LOG_INFO 7
#define LOG_DEBUG 8
/* I'm not 100% sure how this is supposed to work -Kat */
#define LOG_MASK(pri) (pri)
void closelog(void);
void openlog(const char *, int, int);
int setlogmask(int);
void syslog(int, const char *, ...);
#endif

32
include/tar.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef _TAR_H
#define _TAR_H
#define TMAGIC "ustar"
#define TMAGLEN 6
#define TVERSION "00"
#define TVERSLEN 2
#define REGTYPE '0' /* Regular file */
#define AREGTYPE '\0' /* Regular file */
#define LNKTYPE '1' /* Link */
#define SYMTYPE '2' /* Symbolic link */
#define CHRTYPE '3' /* Character special */
#define BLKTYPE '4' /* Block special */
#define DIRTYPE '5' /* Directory */
#define FIFOTYPE '6' /* FIFO */
#define CONTTYPE '7' /* Reserved */
#define TSUID 04000 /* Set UID on exec. */
#define TSGID 02000 /* Set GID on exec. */
#define TSVTX 01000 /* On dirs, restricted deletion flag */
#define TUREAD 00400 /* Read by owner */
#define TUWRITE 00200 /* Write by owner */
#define TUEXEC 00100 /* Exec./search by owner */
#define TGREAD 00040 /* Read by group */
#define TGWRITE 00020 /* Write by group */
#define TGEXEC 00010 /* Exec./search by group */
#define TOREAD 00004 /* Read by others */
#define TOWRITE 00002 /* Write by others */
#define TOEXEC 00001 /* Exec./search by others */
#endif

30
include/time.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef _TIME_H
#define _TIME_H
#ifndef NULL
#define NULL (void *) 0
#endif
#include <types/clock_t.h>
#include <types/size_t.h>
#include <types/time_t.h>
#include <types/clockid_t.h>
#include <types/timer_t.h>
#include <types/locale_t.h>
#include <types/pid_t.h>
typedef struct _time {
unsigned int tm_sec : 6;
unsigned int tm_min : 6;
unsigned int tm_hour : 5;
unsigned int tm_mday : 5;
unsigned int tm_mon : 4;
unsigned int tm_year;
unsigned int tm_wday : 3;
unsigned int tm_yday : 9;
int tm_isdst : 2;
} tm;
time_t time(time_t *);
#endif /* not _HEADER */

6
include/types/clock_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_CLOCK_T_H
#define _TYPES_CLOCK_T_H
typedef unsigned long long int clock_t;
#endif

View File

@ -0,0 +1,6 @@
#ifndef _TYPES_CLOCKID_T_H
#define _TYPES_CLOCKID_T_H
typedef int clockid_t;
#endif

6
include/types/dev_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_DEV_T_H
#define _TYPES_DEV_T_H
typedef int dev_t;
#endif

8
include/types/gid_t.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _TYPES_GID_T_H
#define _TYPES_GID_T_H
#include <types/id_t.h>
typedef unsigned id_t gid_t;
#endif

6
include/types/id_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_ID_T_H
#define _TYPES_ID_T_H
typedef long int id_t;
#endif

4
include/types/locale_t.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef _TYPES_LOCALE_T_H
#define _TYPES_LOCALE_T_H
#endif

6
include/types/mode_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_MODE_T_H
#define _TYPES_MODE_T_H
typedef int mode_t;
#endif

6
include/types/off_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_OFF_T_H
#define _TYPES_OFF_T_H
typedef long long int off_t;
#endif

8
include/types/pid_t.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _TYPES_PID_T_H
#define _TYPES_PID_T_H
#include <types/id_t.h>
typedef id_t pid_t;
#endif

View File

@ -0,0 +1,6 @@
#ifndef _TYPES_PTRDIFF_T_H
#define _TYPES_PTRDIFF_T_H
typedef signed int ptrdiff_t;
#endif

View File

@ -0,0 +1,6 @@
#ifndef _TYPES_SA_FAMILY_T_H
#define _TYPES_SA_FAMILY_T_H
typedef unsigned int sa_family_t;
#endif

6
include/types/size_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_SIZE_T_H
#define _TYPES_SIZE_T_H
typedef unsigned int size_t;
#endif

6
include/types/ssize_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_SSIZE_T_H
#define _TYPES_SSIZE_T_H
typedef unsigned int ssize_t;
#endif

View File

@ -0,0 +1,6 @@
#ifndef _TYPES_SUSECONDS_T_H
#define _TYPES_SUSECONDS_T_H
typedef long long int suseconds_t;
#endif /* not _HEADER */

6
include/types/time_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_TIME_T_H
#define _TYPES_TIME_T_H
typedef unsigned long int time_t;
#endif

6
include/types/timer_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_TIMER_T_H
#define _TYPES_TIMER_T_H
typedef int timer_t;
#endif

8
include/types/uid_t.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _TYPES_UID_T_H
#define _TYPES_UID_T_H
#include <types/id_t.h>
typedef unsigned id_t uid_t;
#endif

6
include/types/wchar_t.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _TYPES_WCHAR_T_H
#define _TYPES_WCHAR_T_H
typedef unsigned int wchar_t;
#endif

20
include/ulimit.h Normal file
View File

@ -0,0 +1,20 @@
/*
* <ulimit.h> - ulimit
*
* This header is a part of the FENIX C Library and is free software.
* You can redistribute and/or modify it subject to the terms of the
* Clumsy Wolf Public License v4. For more details, see the file COPYING.
*
* The FENIX C Library is distributed WITH NO WARRANTY WHATSOEVER. See
* The CWPL for more details.
*/
#ifndef _ULIMIT_H
#define _ULIMIT_H
#define UL_GETFSIZE 1
#define UL_SETFSIZE 2
long int ulimit(int, ...);
#endif

71
libgen/basename.c Normal file
View File

@ -0,0 +1,71 @@
#include <libgen.h>
char * basename(char * path) {
char * basename; char c;
int basename_length = 0, last_slash = 0;
int i = 0, j = 0;
if(path == NULL || strcmp(path, "") == 0) {
basename = calloc(2, sizeof(*basename));
if(basename == NULL) {
errno = ENOMEM;
return NULL;
}
strcpy(basename, ".");
return basename;
}
if(strcmp(path, "/") == 0) {
basename = calloc(2, sizeof(*basename));
if(basename == NULL) {
errno = ENOMEM;
return NULL;
}
strcpy(basename, "/");
return basename;
}
if(strcmp(path, "//") == 0) {
basename = calloc(2, sizeof(*basename));
if(basename == NULL) {
errno = ENOMEM;
return NULL;
}
strcpy(basename, "/");
return basename;
}
i = strlen(path);
if(path[i] == '/') {
path[i] = '\0';
}
i = 0;
for(c = path[i]; c != '\0'; c = path[i]) {
if(c == '/') {
last_slash = i;
}
i++;
}
i = last_slash + 1;
for(c = path[i]; c != '\0'; c = path[i]) {
basename_length++; i++;
}
basename = calloc(basename_length + 1, sizeof(*basename));
if(basename == NULL) {
errno = ENOMEM;
return NULL;
}
i = last_slash + 1;
j = 0;
for(c = path[i]; c != '\0'; c = path[i]) {
basename[j] = c;
i++; j++;
}
basename[j] = '\0';
return basegame;
}

42
libgen/dirname.c Normal file
View File

@ -0,0 +1,42 @@
#include <libgen.h>
char * dirname(char * path) {
int i, last_slash = -1, length;
char * dirname; char c;
if(path == NULL || strcmp(path, "") == 0) {
dirname = calloc(2, sizeof(*dirname));
if(dirname == NULL) {
errno = ENOMEM;
return NULL;
}
strcpy(dirname, ".");
return dirname;
}
length = strlen(path);
if(path[length] == '/') {
path[length] = '\0';
}
i = 0;
for(c = path[i]; c != '\0'; c = path[++i]) {
if(c == '/') {
last_slash = i;
}
}
dirname = calloc(last_slash + 2, sizeof(*dirname));
if(dirname == NULL) {
errno = ENOMEM;
return NULL;
}
for(i = 0; i <= last_slash; i++) {
dirname[i] = path[i];
}
dirname[i] = '\0';
return dirname;
}

91
math/cos.c Normal file
View File

@ -0,0 +1,91 @@
#include <math.h>
/*
Approximates the cosine of x.
Uses a taylor polynomial (roughly) accurate between 0 and
2*pi to approximate the cosine. Any values of x outside this
range are modulo'd to be within this range (you know, 'cos
cosine is cyclic).
The taylor poly is centered at pi, with a radius of
convergence of no less than pi, making it roughly accurate
between 0 and 2 * pi. The polynomial is up to x^20, so it
should be pretty accurate within that range.
*/
double cos(double x) {
double pi = M_PI; /* Really, me?! -Kat */
int temp;
double deg_2, deg_4, deg_6, deg_8, deg_10;
double deg_12, deg_14, deg_16, deg_18, deg_20;
double cosine;
if(x < 0) x = -x;
/* Hey look, an fmod implementation! -Kat */
if(x >= 2 * pi || x <= -2 * pi) {
temp = x / (2 * pi);
x -= temp * (2 * pi);
}
/*
Terms of our taylor poly, split up to make writing the
actual polynomial less hellish. If this were a Maclaurin
polynomial, I could maybe just write it, but having to deal
this (x-pi) nonse, this is easier, I feel.
This might be worth a rewrite once I get double pow(double)
written, to use pow(x - pi, y), where y is the degree. Also,
if math.h or something has a factorial function, it might
be good to use that for the denominator.
*/
deg_2 = (x - pi) * (x - pi) / 2;
deg_4 = deg_2 * deg_2 * 2 / (4 * 3);
deg_6 = deg_4 * deg_2 * 2 / (6 * 5);
deg_8 = deg_6 * deg_2 * 2 / (8 * 7);
deg_10 = deg_8 * deg_2 * 2 / (10 * 9);
deg_12 = deg_10 * deg_2 * 2 / (12 * 11);
deg_14 = deg_12 * deg_2 * 2 / (14 * 13);
deg_16 = deg_14 * deg_2 * 2 / (16 * 15);
deg_18 = deg_16 * deg_2 * 2 / (18 * 17);
deg_20 = deg_18 * deg_2 * 2 / (20 * 19);
/*
In case you aren't familiar with the theory of a Taylor
polynomial, the basic idea is that *any* differentiable
function can be written as a polynomial of some length.
I won't go too much into how we get to this polynomial
(it involves a lot of calculus), but if we carry out this
process for cos(x), we get a polynomial of roughly the
form of the sum from k=0 to infinity of:
-1^(k+1) * x^(2*k) / (2k)!
So, for the 0th term, it's -1^1 * x^0 / 0! or -1/1 = -1.
For the 1st term, it's -1^2 * x^2 / 2! or x^2 / 2.
For term 2: -1^3 * x^4 / 4! or -(x^4) / 24.
So on, so forth. If we want it centered at pi, as we do
for this, we need to instead calculate that sum for
(x - pi) instead of just x. Also, notice that we don't
calculate an infinite sum. Just 11 terms. This is a result
of cosine's cyclical nature. Every possible value can be
given by just modulo-ing whatever we want to know the
cosine of to be between 0 and 2 * pi. So, for instance,
the cosine of 5 * pi / 2 is equal to the cosine of pi / 2.
So, we only need it to be accurate between 0 and 2 * pi.
So, why center on pi? To reduce the number of terms. If
we center on zero, we have to stretch the radius of
convergence from 0 to 2 * pi. But if we center at pi,
that radius only needs to be from 0 to pi and pi to 2 * pi,
giving a radius of convergence of only pi instead of 2 *pi.
Plus, since we're taking advantage of cosine being an
even function, we don't need any terms less than 0, so
why *center* at zero and include negatives?
(Of course, I've not done the radius of convergence work
for this to find out how many degrees we need to get a
radius of convergence of pi, so I may have used too many
terms, anyways. Not that I can't reduce that...)
*/
cosine = -1 + deg_2 - deg_4 + deg_6 - deg_8 + deg_10;
cosine = cosine - deg_12 + deg_14 - deg_16 + deg_18 + deg_20;
return cosine;
}

32
math/cosf.c Normal file
View File

@ -0,0 +1,32 @@
#include <math.h>
float cosf(float x) {
float pi = M_PI;
int temp;
float deg_2, deg_4, deg_6, deg_8, deg_10;
float deg_12, deg_14, deg_16, deg_18, deg_20;
float cosine;
if(x < 0) x = -x;
if(x >= 2 * pi || x <= -2 * pi) {
temp = x / (2 * pi);
x -= temp * (2 * pi);
}
deg_2 = (x - pi) * (x - pi) / 2;
deg_4 = deg_2 * deg_2 * 2 / (4 * 3);
deg_6 = deg_4 * deg_2 * 2 / (6 * 5);
deg_8 = deg_6 * deg_2 * 2 / (8 * 7);
deg_10 = deg_8 * deg_2 * 2 / (10 * 9);
deg_12 = deg_10 * deg_2 * 2 / (12 * 11);
deg_14 = deg_12 * deg_2 * 2 / (14 * 13);
deg_16 = deg_14 * deg_2 * 2 / (16 * 15);
deg_18 = deg_16 * deg_2 * 2 / (18 * 17);
deg_20 = deg_18 * deg_2 * 2 / (20 * 19);
cosine = -1 + deg_2 - deg_4 + deg_6 - deg_8 + deg_10;
cosine = cosine - deg_12 + deg_14 - deg_16 + deg_18 + deg_20;
return cosine;
}

32
math/cosl.c Normal file
View File

@ -0,0 +1,32 @@
#include <math.h>
long double cosl(long double x) {
long double pi = M_PI;
int temp;
long double deg_2, deg_4, deg_6, deg_8, deg_10;
long double deg_12, deg_14, deg_16, deg_18, deg_20;
long double cosine;
if(x < 0) x = -x;
if(x >= 2 * pi || x <= -2 * pi) {
temp = x / (2 * pi);
x -= temp * (2 * pi);
}
deg_2 = (x - pi) * (x - pi) / 2;
deg_4 = deg_2 * deg_2 * 2 / (4 * 3);
deg_6 = deg_4 * deg_2 * 2 / (6 * 5);
deg_8 = deg_6 * deg_2 * 2 / (8 * 7);
deg_10 = deg_8 * deg_2 * 2 / (10 * 9);
deg_12 = deg_10 * deg_2 * 2 / (12 * 11);
deg_14 = deg_12 * deg_2 * 2 / (14 * 13);
deg_16 = deg_14 * deg_2 * 2 / (16 * 15);
deg_18 = deg_16 * deg_2 * 2 / (18 * 17);
deg_20 = deg_18 * deg_2 * 2 / (20 * 19);
cosine = -1 + deg_2 - deg_4 + deg_6 - deg_8 + deg_10;
cosine = cosine - deg_12 + deg_14 - deg_16 + deg_18 + deg_20;
return cosine;
}

10
math/fabs.c Normal file
View File

@ -0,0 +1,10 @@
#include <math.h>
double fabs(double x) {
if(x < 0) {
return -x;
}
else {
return x;
}
}

10
math/fabsf.c Normal file
View File

@ -0,0 +1,10 @@
#include <math.h>
float fabsf(float x) {
if(x < 0) {
return -x;
}
else {
return x;
}
}

10
math/fabsl.c Normal file
View File

@ -0,0 +1,10 @@
#include <math.h>
long double fabsl(long double x) {
if(x < 0) {
return -x;
}
else {
return x;
}
}

View File

@ -0,0 +1,99 @@
/*
<errno.h> - system error numbers
This header is part of fenlibc,
a component of the Fenix Operating System
This package is free software. Please see the
file COPYING in the source distribution for
more information.
*/
#ifndef _FLC_ERRNO
#define _FLC_ERRNO
int errno; /* Eh, this may not be "correct" -Kat */
#define E2BIG 1
#define EACCESS 2
#define EADDRINUSE 3
#define EADDRNOTAVAIL 4
#define EAFNOSUPPORT 5
#define EAGAIN 6
#define EALREADY 7
#define EBADF 8
#define EBADMSG 9
#define EBUSY 10
#define ECANCELED 11
#define ECHILD 12
#define ECONNABORTED 13
#define ECONNREFUSED 14
#define ECONNRESET 15
#define EDEADLK 16
#define EDESTADDRREQ 17
#define EDOM 18
#define EDQUOT 19
#define EEXIST 20
#define EFAULT 21
#define EFBIG 22
#define EHOSTUNREACH 23
#define EIDRM 24
#define EILSEQ 25
#define EINPROGRESS 26
#define EINTR 27
#define EINVAL 28
#define EIO 29
#define EISCONN 30
#define EISDIR 31
#define ELOOP 32
#define EMFILE 33
#define EMLINK 34
#define EMSGSIZE 35
#define EMULTIHOP 36
#define ENAMETOOLONG 37
#define ENETDOWN 38
#define ENETRESET 39
#define ENETUNREACH 40
#define ENFILE 41
#define ENOBUFFS 42
#define ENODATA 43
#define ENODEV 44
#define ENOENT 45
#define ENOEXEC 46
#define ENOLCK 47
#define ENOLINK 48
#define ENOMEM 49
#define ENOMSG 50
#define ENOPROTOOPT 51
#define ENOSPC 52
#define ENOSR 53
#define ENOSTR 54
#define ENOSYS 55
#define ENOTCONN 56
#define ENOTDIR 57
#define ENOTEMPTY 58
#define ENOTRECOVERABLE 59
#define ENOTSOCK 60
#define ENOTSUP 61
#define ENOTTY 62
#define ENXIO 63
#define EOPNOTSUPP 61
#define EOVERFLOW 64
#define EOWNERDEAD 65
#define EPERM 66
#define EPIPE 67
#define EPROTO 68
#define EPROTONOSUPPORT 69
#define EPROTOTYPE 70
#define ERANGE 71
#define EROFS 72
#define ESPIPE 73
#define ESRCH 74
#define ESTALE 75
#define ETIME 76
#define ETIMEDOUT 77
#define ETXTBSY 78
#define EWOULDBLOCK 6
#define EXDEV 79
#endif /* ndef _FLC_ERRNO */

View File

@ -0,0 +1,33 @@
/*
<iso646.h> - alternative spellings
This header is a part of fenlibc,
a component of the Fenix Operating System
This package is free software. Please see the
file COPYING in the source distribution for
more information.
*/
/*
Why is this a thing? It's stupid. Who uses this?
-Kat
*/
#ifndef _FLC_ISO646_H
#define _FLC_ISO646_H
#define and &&
#define and_eq &=
#define bitand &
#define bitor |
#define compl ~
#define not !
#define not_eq !=
#define or ||
#define or_eq |=
#define xor ^
#define xor_eq ^=
#endif

View File

@ -0,0 +1,161 @@
/*
<limits.h> - implementation-defined constants
This header is a part of fenlibc,
a component of the Fenix Operating System
This package is free software. Please see the
file COPYING in the source distribution for
more information.
*/
#ifndef _FLC_LIMITS
#define _FLC_LIMITS
/*
Minimum values (POSIX)
We're keeping these, just in case
any applications use them in an
attempt to play it safe. That, and
I wasted all that time putting these
into this file. We're getting some
f'kin' use out of 'em.
-Kat
*/
#define _POSIX_AIO_LISTIO_MAX 2
#define _POSIX_AIO_MAX 1
#define _POSIX_ARG_MAX 4096
#define _POSIX_CHILD_MAX 25
#define _POSIX_DELAYTIMER_MAX 32
#define _POSIX_HOST_NAME_MAX 255
#define _POSIX_LINK_MAX 8
#define _POSIX_LOGIN_NAME_MAX 9
#define _POSIX_MAX_CANON 255
#define _POSIX_MAX_INPUT 255
#define _POSIX_MQ_OPEN_MAX 8
#define _POSIX_MQ_PRIO_MAX 32
#define _POSIX_NAME_MAX 14
#define _POSIX_NGROUPS_MAX 8
#define _POSIX_OPEN_MAX 20
#define _POSIX_PATH_MAX 256
#define _POSIX_PIPE_BUF 512
#define _POSIX_RE_DUP_MAX 255
#define _POSIX_RTSIG_MAX 8
#define _POSIX_SEM_NSEMS_MAX 256
#define _POSIX_SEM_VALUE_MAX 32767
#define _POSIX_SIGQUEUE_MAX 32
#define _POSIX_SSIZE_MAX 32767
#define _POSIX_SS_REPL_MAX 4
#define _POSIX_STREAM_MAX 8
#define _POSIX_SYMLINK_MAX 255
#define _POSIX_SYMLOOP_MAX 8
#define _POSIX_THREAD_DESTRUCTOR_ITERATIONS 4
#define _POSIX_THREAD_KEYS_MAX 128
#define _POSIX_THREAD_THREADS_MAX 64
#define _POSIX_TIMER_MAX 32
#define _POSIX_TRACE_EVENT_NAME_MAX 30
#define _POSIX_TRACE_NAME_MAX 8
#define _POSIX_TRACE_SYS_MAX 8
#define _POSIX_TRACE_USER_EVENT_MAX 32
#define _POSIX_TTY_NAME_MAX 9
#define _POSIX_TZNAME_MAX 6
#define _POSIX2_BC_BASE_MAX 99
#define _POSIX2_BC_DIM_MAX 2048
#define _POSIX2_BC_SCALE_MAX 99
#define _POSIX2_BC_STRING_MAX 1000
#define _POSIX2_CHARCLASS_NAME_MAX 14
#define _POSIX2_COLL_WEIGHTS_MAX 2
#define _POSIX2_EXPR_NEST_MAX 32
#define _POSIX2_LINE_MAX 2048
#define _POSIX2_RE_DUP_MAX 255
#define _XOPEN_IOV_MAX 16
#define _XOPEN_NAME_MAX 255
#define _XOPEN_PATH_MAX 1024
/* Runtime Invariant Values */
/*
Not quite sure how these work. I'll figure it out
after I do <unistd.h>
-Kat
*/
/* Pathname Variable Values */
#define FILESIZEBITS 64
#define LINK_MAX 16
/*
Remember, you only get 1024 canons.
After that, new canons have to start
merging in with old ones. Hence,
Smash Ultimate.
-Kat
*/
#define MAX_CANON 1024
#define MAX_INPUT 256
#define NAME_MAX 256
#define PATH_MAX 4096
#define PIPE_BUF 1024
#define POSIX_ALLOC_SIZE_MIN 1024
#define POSIX_REC_INCR_XFER_SIZE 512
#define POSIX_REC_MAX_XFER_SIZE 1048576
#define POSIX_REC_MIN_XFER_SIZE 512
#define POSIX_REC_XFER_ALIGN 512
#define SYMLINK_MAX 2048
/* Runtime Increasable Values */
/*
So, when we do <unistd.h>, that makes these weird.
Fun. Fun, fun.
-Kat
*/
#define BC_BASE_MAX 256
#define BC_DIM_MAX 8192
#define BC_SCALE_MAX 99
#define BC_STRING_MAX 1024
#define CHARCLASS_NAME_MAX 32
#define COLL_WEIGHTS_MAX 4
#define EXPR_NEST_MAX 128
#define LINE_MAX 8192
#define RE_DUP_MAX 512
/* Maximum values */
#define _POSIX_CLOCKRES_MIN 20000000
/* Numerical limits */
#define CHAR_BIT 8
#define CHAR_MAX 127
#define CHAR_MIN -128
#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define LLONG_MAX 9223372036854775807
#define LLONG_MIN -9223372036854775807
#define LONG_BIT 32
#define LONG_MAX 2147483647
#define LONG_MIN -2147483647
#define MB_LEN_MAX 16
#define SCHAR_MAX 127
#define SCHAR_MIN -128
#define SHRT_MAX 32767
#define SHRT_MIN -32767
#define SSIZE_MAX 32767
#define UCHAR_MAX 255
#define UINT_MAX 4294967295
#define ULLONG_MAX 18446744073709551615
#define ULONG_MAX 4294967295
#define USHRT_MAX 65535
#define WORD_BIT 32
/* Miscellaneous invariants */
#define NL_ARGMAX 9
#define NL_LANGMAX 14
#define NL_MSGMAX 65535
#define NL_SETMAX 255
#define NL_TEXTMAX 8192
#define NZERO 20
#endif /* ndef _FLC_LIMITS */

View File

@ -0,0 +1,102 @@
#ifndef _MATH_H
#define _MATH_H
#include <sys/types.h>
#if FLT_EVAL_METHOD == 1
typedef double float_t;
typedef double double_t;
#elseif FLT_EVAL_METHOD == 2
typedef long double float_t;
typedef long double double_t;
#else
typedef float float_t;
typedef double double_t;
#endif
#define M_E 2.718281828
#define M_PI 3.14159265
#define PI 3.14159265
#define M_PI_2 1.5707963268
#define M_PI_4 0.7853981634
#define M_1_PI 0.318309886184
#define M_2_PI 0.636619772368
#define M_2_SQRTPI 1.1283791671
#define M_SQRT2 1.41421856237
#define M_SQRT1_2 0.707106781187
#define M_LOG2E 1.44269504089
#define M_LOG10E 0.4342944819
#define M_LN2 0.69314718056
#define M_LN10 2.302585093
double acos(double);
float acosf(float);
long double acosl(long double);
double asin(double);
float asinf(float);
long double asinl(long double);
double atan(double);
float atanf(float);
long double atanl(long double);
double atan2(double);
float atan2f(float);
long double atan2l(long double);
double cos(double);
float cosf(float);
long double cosl(long double);
double sin(double);
float sinf(float);
long double sinl(long double);
double tan(double);
float tanf(float);
long double tanl(long double);
double acosh(double);
float acoshf(float);
long double acoshl(long double);
double asinh(double);
float asinhf(float);
long double asinhl(long double);
double atanh(double);
float atanhf(float);
long double atanhl(long double);
double cosh(double);
float coshf(float);
long double coshl(long double);
double sinh(double);
float sinhf(float);
long double sinhl(long double);
double tanh(double);
float tanhf(float);
long double tanhl(long double);
double exp(double);
float expf(float);
long double expl(double);
double exp2(double);
float exp2f(float);
long double exp2l(double);
double expm1(double);
float expm1f(float);
long double expm1l(double);
double frexp(double, int *);
float frexpf(float, int *);
long double frexpl(double, int *);
#endif

View File

@ -0,0 +1,24 @@
/*
<stddef.h> - standard type definitions
This header is a part of fenlibc,
a component of the Fenix Operating System
This package is free software. Please see the
file COPYING in the source distribution for
more information.
*/
#ifndef _FLC_STDDEF
#define _FLC_STDDEF
#undef NULL
#define NULL (void *) 0
#define __need_ptrdiff_t
#define __need_size_t
#define __need_wchar_t
#include <sys/types.h>
#endif /* ndef _FLC_STDDEF */

View File

@ -0,0 +1,14 @@
#ifndef _STDIO_H
#define _STDIO_H
#include <sys/cdefs.h>
#define EOF (-1)
int printf(const char * __restrict, ...);
int putchar(int);
int puts(const char *);
#endif

View File

@ -0,0 +1,64 @@
/*
<stdlib.h> - standard library definitions
This header is a part of fenlibc,
a component of the Fenix Operating System.
This package is free software. Please see
the file COPYING in the source distribution
for more information.
*/
#ifndef _STDLIB_H
#define _STDLIB_H
#include <sys/cdefs.h>
#ifndef NULL
#define NULL (void *) 0
#endif
#define __need_size_t
#define __need_wchar_t
#include <sys/types.h>
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
#define RAND_MAX 32767
#define MB_CUR_MAX (size_t) 1
typedef struct _f_div_t {
int quot;
int rem;
} div_t;
typedef struct _f_ldiv_t {
long quot;
long rem;
} ldiv_t;
typedef struct _f_lldiv_t {
long long quot;
long long rem;
} lldiv_t;
__attribute__((__noreturn__))
void abort(void);
void srand(unsigned int);
int rand(void);
div_t div(int, int);
ldiv_t ldiv(long, long);
lldiv_t lldiv(long long, long long);
int abs(int);
long labs(long);
long long llabs(long long);
void * bsearch(const void *, const void *, size_t, size_t,
int (*compar)(const void *, const void *));
#endif

View File

@ -0,0 +1,15 @@
#ifndef _STRING_H
#define _STRING_H
#include <sys/cdefs.h>
#include <stddef.h>
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 * memset(void *, int, size_t);
size_t strlen(const char *);
#endif

View File

@ -0,0 +1,6 @@
#ifndef _SYS_CDEFS_H
#define _SYS_CDEFS_H
#define __fenix_libc 1
#endif

View File

@ -0,0 +1,69 @@
/*
<sys/types.h> - data types
This header is a part of fenlibc,
a component of the Fenix Operating System
This package is free software. Please see the
file COPYING in the source distribution for
more information.
*/
#ifndef _SYS_TYPES_H
#define _SYS_TYPES_H
#ifndef _FLC_ALLTYPES
#define _FLC_ALLTYPES
#ifdef __need_locale_t
#ifndef __have_locale_t
#define __have_locale_t
typedef struct _locale_t {
} locale_t;
#endif
#endif
#ifdef __need_ptrdiff_t
#ifndef __have_ptrdiff_t
#define __have_ptrdiff_t
typedef signed int ptrdiff_t;
#endif
#endif
#ifdef __need_size_t
#ifndef __have_size_t
#define __have_size_t
typedef unsigned int size_t;
#endif
#endif
#ifdef __need_wchar_t
#ifndef __have_wchar_t
#define __have_wchar_t
typedef int wchar_t;
#endif
#endif
#ifdef __need_id_t
#ifndef __have_id_t
#define __have_id_t
typedef int id_t;
#endif
#endif
#ifdef __need_gid_t
#ifndef __have_gid_t
#define __have_gid_t
typedef unsigned id_t gid_t;
#endif
#endif
#ifdef __need_uid_t
#ifndef __have_uid_t
#define __have_uid_t
typedef unsigned id_t uid_t;
#endif
#endif
#endif /* not _HEADER */

7
stdio/perror.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
void perror(const char * str) {
fprintf(stderr, "%s: %s\n", str, strerror(errno));
}

6
stdlib/abs.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdlib.h>
int abs(int x) {
int y = x < 0 ? -x : x;
return y;
}

8
stdlib/div.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdlib.h>
div_t div(int x, int y) {
div_t ret_val;
ret_val.quot = x / y;
ret_val.rem = x % y;
return ret_val;
}

6
stdlib/labs.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdlib.h>
long int labs(long int x) {
long int y = x < 0 ? -x : x;
return y;
}

8
stdlib/ldiv.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdlib.h>
ldiv_t ldiv(long int x, long int y) {
ldiv_t ret_val;
ret_val.quot = x / y;
ret_val.rem = x % y;
return ret_val;
}

6
stdlib/llabs.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdlib.h>
long long int llabs(long long int x) {
long long int y = x < 0 ? -x : x;
return y;
}

8
stdlib/lldiv.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdlib.h>
lldiv_t lldiv(long long int x, long long int y) {
lldiv_t ret_val;
ret_val.quot = x / y;
ret_val.rem = x % y;
return ret_val;
}

64
stdlib/rand.c Normal file
View File

@ -0,0 +1,64 @@
/*
* This rand() implementation is based on the Mersenne Twister.
* Specifically, it's a C implementation of the pseudocode
* implementation that can be found on the Wikipedia article for
* the Mersenne Twister.
*
* Did we have to do a Mersenne Twister? No. No, we did not.
* Did we? Fuck yes, we did.
*
* Love,
* Kat
*/
#include <stdlib.h>
static int _rand_state[624] = { 0 };
static unsigned int _rand_seed = 5489;
static int _rand_index = 625;
void _rand_init(unsigned int seed) {
_rand_index = 624;
_rand_state[0] = seed;
int i = 0;
for(i = 1; i < 624; i++) {
_rand_state[i] = 1812433253 ;
_rand_state[i] *= _rand_state[i-1] ^ (_rand_state[i-1] >> 30);
_rand_state[i] += i;
}
}
void _rand_twist() {
int i = 0, x, xA;
for(i = 0; i < 624; i++) {
x = _rand_state[i] & 0xFFFF0000 + (_rand_state[(i + 1) % 624] & 0xFFFF);
xA = x % 2 == 0 ? x >> 1 : (x >> 1) ^ 0x9908B0DF;
_rand_state[i] = _rand_state[(i + 397) % 624] ^ xA;
}
_rand_index = 0;
}
int rand(void) {
int y;
if(_rand_index > 624) {
_rand_init(_rand_seed);
}
if(_rand_index >= 624) {
_rand_twist();
}
y = _rand_state[_rand_index];
y ^= y >> 11;
y ^= (y << 7) & 0x9D2C5680;
y ^= (y << 15) & 0xEFC60000;
y ^= (y >> 18);
_rand_index++;
return y % RAND_MAX;
}
void srand(unsigned int seed) {
_rand_seed = seed;
_rand_index = 625;
}

36
string/strcat.c Normal file
View File

@ -0,0 +1,36 @@
#include <string.h>
/*
Is this...okay? Like, from what I can tell, it should be,
since s1 and s2 are restricted. s1 and s2 shouldn't be the
same object. But, like, I'm worried about what happens if
someone passes in the same pointer for s1 and s2. You'd get
an infinite loop of adding stuff that just eats up all
memory from there! But, like, I think musl does the same
thing? I really can't tell. For now, I'm gonna assume I've
done this acceptably.
For now.
-Kat
*/
/*
I'm pretty sure musl does the same thing, for the most part.
-Kat
*/
/*
Does weird stuff. Maybe don't use until we figure out what
the fuck is wrong with it.
-Kat
*/
char * strcat(char * restrict s1, const char * restrict s2) {
int i = 0, j;
for(i = 0; s1[i] != '\0'; i++){}
for(j = 0; s2[j] != '\0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\0';
return s1;
}

12
string/strcmp.c Normal file
View File

@ -0,0 +1,12 @@
#include <string.h>
int strcmp(const char * s1, const char * s2) {
size_t i = 0;
while(s1[i] != '\0' && s2[i] != '\0') {
if(s1[i] != s2[i]) {
return s1[i] - s2[i];
}
i++;
}
return s1[i] - s2[i];
}

22
string/strcspn.c Normal file
View File

@ -0,0 +1,22 @@
#include <string.h>
/*
This might need to be rewritten. It does grow as
O(m*n), where m and n are the number of characters
in s2 and s1. If I do, musl's implementation is
kinda cool. I could do something like that. But,
I think it'll do for now.
-Kat
*/
size_t strcspn(const char * s1, const char * s2) {
int i = 0;
for(i = 0; s1[i] != '\0'; i++) {
for(int j = 0; s2[j] != '\0'; j++) {
if(s1[i] == s2[j]) {
return i;
}
}
}
return i;
}

30
string/strtok.c Normal file
View File

@ -0,0 +1,30 @@
#include <string.h>
char * strtok(char * restrict str, char * restrict sep) {
static char * state;
char * search_str;
int i = 0, sep_size = strlen(sep);
if(str != NULL) {
search_str = str;
}
else {
search_str = state;
}
while(*search_str != '\0' && strcmp(search_str, sep) == 0) {
search_str += sep_size;
}
if(*search_str == '\0') {
return NULL;
}
while(search_str[i] != '\0' && strcmp(&(search_str[i]), sep) != 0) {
i++;
}
search_str[i] = '\0';
state = &(search_str[i + 1]);
return search_str;
}

39
time/time.c Normal file
View File

@ -0,0 +1,39 @@
#include <time.h>
#ifdef __is_libk
#include <kernel/cmos.h>
#endif
unsigned int get_days(unsigned int year, unsigned char month, unsigned char day) {
unsigned int tot_days = day - 1;
unsigned char month_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(unsigned int i = 1970; i < year; i++) {
if(i % 400 == 0 || (i % 100 != 0 && i % 4 == 0)) {
tot_days += 366;
}
else {
tot_days += 365;
}
}
for(int i = 0; i < (month - 1); i++) {
tot_days += month_days[i];
}
return tot_days;
}
time_t time(time_t * tloc) {
time_t ret_val;
#ifdef __is_libk
ret_val = read_rtc();
#else
/* TODO: Implement system calls for this stuff */
#endif
if(tloc != 0) {
*tloc = ret_val;
}
return ret_val;
}