Skip to content

Commit

Permalink
Libc: epoll and more libc function
Browse files Browse the repository at this point in the history
  • Loading branch information
coolyjg committed Jun 16, 2023
1 parent c304173 commit c606872
Show file tree
Hide file tree
Showing 24 changed files with 683 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/axio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ where
}

/// Struct for poll result.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct PollState {
/// Object can be read now.
pub readable: bool,
Expand Down
41 changes: 41 additions & 0 deletions ulib/c_libax/include/dirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef _DIRENT_H
#define _DIRENT_H

#include <sys/types.h>

struct __dirstream {
long long tell;
int fd;
int buf_pos;
int buf_end;
int lock[1];
char buf[2048];
};

typedef struct __dirstream DIR;

struct dirent {
ino_t d_ino;
off_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};

int closedir(DIR *);
DIR *fdopendir(int);
int dirfd(DIR *);

#define DT_UNKNOWN 0
#define DT_FIFO 1
#define DT_CHR 2
#define DT_DIR 4
#define DT_BLK 6
#define DT_REG 8
#define DT_LNK 10
#define DT_SOCK 12
#define DT_WHT 14
#define IFTODT(x) ((x) >> 12 & 017)
#define DTTOIF(x) ((x) << 12)

#endif //_DIRENT_H
15 changes: 15 additions & 0 deletions ulib/c_libax/include/libgen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef _LIBGEN_H
#define _LIBGEN_H

#ifdef __cplusplus
extern "C" {
#endif

char *dirname(char *);
char *basename(char *);

#ifdef __cplusplus
}
#endif

#endif
1 change: 1 addition & 0 deletions ulib/c_libax/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern FILE *const stderr;

#if defined(AX_CONFIG_ALLOC) && defined(AX_CONFIG_FS)
FILE *fopen(const char *filename, const char *mode);
char *fgets(char *__restrict, int, FILE *__restrict);
#endif

int fflush(FILE *);
Expand Down
5 changes: 5 additions & 0 deletions ulib/c_libax/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ void *realloc(void *memblock, size_t size);
_Noreturn void abort(void);
char *getenv(const char *name);

#ifdef AX_CONFIG_FP_SIMD
float strtof(const char *__restrict, char **__restrict);
double strtod(const char *__restrict, char **__restrict);
#endif

long strtol(const char *__restrict, char **__restrict, int);
unsigned long strtoul(const char *nptr, char **endptr, int base);
long long strtoll(const char *nptr, char **endptr, int base);
Expand Down
5 changes: 5 additions & 0 deletions ulib/c_libax/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ char *strncat(char *restrict d, const char *restrict s, size_t n);
int strcmp(const char *l, const char *r);
int strncmp(const char *l, const char *r, size_t n);

int strcoll(const char *, const char *);

size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s, const char *c);
char *strpbrk(const char *, const char *);

char *strchrnul(const char *, int);

char *strrchr(const char *str, int c);
char *strchr(const char *str, int c);
Expand Down
60 changes: 60 additions & 0 deletions ulib/c_libax/include/sys/epoll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef _SYS_EPOLL_H
#define _SYS_EPOLL_H

#ifdef __cplusplus
extern "C" {
#endif

#include <fcntl.h>
#include <stdint.h>

#define EPOLL_CLOEXEC O_CLOEXEC
#define EPOLL_NONBLOCK O_NONBLOCK

enum EPOLL_EVENTS { __EPOLL_DUMMY };
#define EPOLLIN 0x001
#define EPOLLPRI 0x002
#define EPOLLOUT 0x004
#define EPOLLRDNORM 0x040
#define EPOLLNVAL 0x020
#define EPOLLRDBAND 0x080
#define EPOLLWRNORM 0x100
#define EPOLLWRBAND 0x200
#define EPOLLMSG 0x400
#define EPOLLERR 0x008
#define EPOLLHUP 0x010
#define EPOLLRDHUP 0x2000
#define EPOLLEXCLUSIVE (1U << 28)
#define EPOLLWAKEUP (1U << 29)
#define EPOLLONESHOT (1U << 30)
#define EPOLLET (1U << 31)

#define EPOLL_CTL_ADD 1
#define EPOLL_CTL_DEL 2
#define EPOLL_CTL_MOD 3

typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;

struct epoll_event {
uint32_t events;
epoll_data_t data;
}
#ifdef __x86_64__
__attribute__((__packed__))
#endif
;

int epoll_create(int __size);
int epoll_ctl(int, int, int, struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, int);

#ifdef __cplusplus
}
#endif

#endif //_SYS_EPOLL_H
4 changes: 4 additions & 0 deletions ulib/c_libax/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ time_t time(time_t *t);
int clock_gettime(clockid_t _clk, struct timespec *ts);
int nanosleep(const struct timespec *requested_time, struct timespec *remaining);

#ifdef AX_CONFIG_FP_SIMD
double difftime(time_t, time_t);
#endif

#endif
48 changes: 48 additions & 0 deletions ulib/c_libax/src/dirent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#ifdef AX_CONFIG_ALLOC
int closedir(DIR *dir)
{
int ret = close(dir->fd);
free(dir);
return ret;
}

DIR *fdopendir(int fd)
{
DIR *dir;
struct stat st;

if (fstat(fd, &st) < 0) {
return 0;
}
if (fcntl(fd, F_GETFL) & O_PATH) {
errno = EBADF;
return 0;
}
if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return 0;
}
if (!(dir = calloc(1, sizeof(*dir)))) {
return 0;
}

fcntl(fd, F_SETFD, FD_CLOEXEC);
dir->fd = fd;
return dir;
}

#endif

int dirfd(DIR *d)
{
return d->fd;
}
14 changes: 14 additions & 0 deletions ulib/c_libax/src/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <string.h>
#include <unistd.h>

char **environ = 0;

char *getenv(const char *name)
{
size_t l = strchrnul(name, '=') - name;
if (l && !name[l] && environ)
for (char **e = environ; *e; e++)
if (!strncmp(name, *e, l) && l[*e] == '=')
return *e + l + 1;
return 0;
}
19 changes: 19 additions & 0 deletions ulib/c_libax/src/epoll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <sys/epoll.h>

#include <libax.h>

int epoll_create(int size)
{
return ax_epoll_create(size);
}

int epoll_ctl(int fd, int op, int fd2, struct epoll_event *ev)
{
return ax_epoll_ctl(fd, op, fd2, ev);
}

int epoll_wait(int fd, struct epoll_event *ev, int cnt, int to)
{
return ax_epoll_wait(fd, ev, cnt, to);
}
33 changes: 33 additions & 0 deletions ulib/c_libax/src/libgen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <libgen.h>
#include <string.h>

char *dirname(char *s)
{
size_t i;
if (!s || !*s)
return ".";
i = strlen(s) - 1;
for (; s[i] == '/'; i--)
if (!i)
return "/";
for (; s[i] != '/'; i--)
if (!i)
return ".";
for (; s[i] == '/'; i--)
if (!i)
return "/";
s[i + 1] = 0;
return s;
}

char *basename(char *s)
{
size_t i;
if (!s || !*s)
return ".";
i = strlen(s) - 1;
for (; i && s[i] == '/'; i--) s[i] = 0;
for (; i && s[i - 1] != '/'; i--)
;
return s + i;
}
3 changes: 2 additions & 1 deletion ulib/c_libax/src/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ double floor(double x)
return x + y - 1;
return x + y;
}
#endif

#endif
24 changes: 24 additions & 0 deletions ulib/c_libax/src/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,28 @@ FILE *fopen(const char *filename, const char *mode)
return f;
}

char *fgets(char *restrict s, int n, FILE *restrict f)
{
if (n == 0)
return NULL;
if (n == 1) {
*s = '\0';
return s;
}

int cnt = 0;
while (cnt < n - 1) {
char c;
if (ax_read(f->fd, (void *)&c, 1) > 0) {
if (c != '\n')
s[cnt++] = c;
else
break;
} else
break;
}
s[cnt] = '\0';
return s;
}

#endif
26 changes: 19 additions & 7 deletions ulib/c_libax/src/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <libax.h>

Expand Down Expand Up @@ -36,6 +37,9 @@ void *calloc(size_t m, size_t n)

void *realloc(void *memblock, size_t size)
{
if (!memblock)
return malloc(size);

size_t o_size = *(size_t *)(memblock - 8);

void *mem = ax_malloc(size);
Expand All @@ -49,6 +53,8 @@ void *realloc(void *memblock, size_t size)

void free(void *addr)
{
if (!addr)
return;
return ax_free(addr);
}

Expand All @@ -60,13 +66,6 @@ _Noreturn void abort(void)
__builtin_unreachable();
}

// TODO:
char *getenv(const char *name)
{
unimplemented();
return 0;
}

// TODO:
int __clzdi2(int a)
{
Expand Down Expand Up @@ -377,3 +376,16 @@ long long atoll(const char *s)
while (isdigit(*s)) n = 10 * n - (*s++ - '0');
return neg ? n : -n;
}

#ifdef AX_CONFIG_FP_SIMD
float strtof(const char *restrict s, char **restrict p)
{
return ax_strtof(s, p);
}

double strtod(const char *restrict s, char **restrict p)
{
return ax_strtod(s, p);
}

#endif
Loading

0 comments on commit c606872

Please sign in to comment.