Skip to content

Commit

Permalink
complete some libc function
Browse files Browse the repository at this point in the history
  • Loading branch information
coolyjg committed Apr 28, 2023
1 parent 939e63d commit a857300
Show file tree
Hide file tree
Showing 25 changed files with 883 additions and 42 deletions.
34 changes: 34 additions & 0 deletions ulib/c_libax/include/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef __LIMITS__
#define __LIMITS__

#define __LONG_MAX 0x7fffffffffffffffL
#define CHAR_BIT 8
#define SCHAR_MIN (-128)
#define SCHAR_MAX 127
#define UCHAR_MAX 255
#define SHRT_MIN (-1 - 0x7fff)
#define SHRT_MAX 0x7fff
#define USHRT_MAX 0xffff
#define INT_MIN (-1 - 0x7fffffff)
#define INT_MAX 0x7fffffff
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-LONG_MAX - 1)
#define LONG_MAX __LONG_MAX
#define ULONG_MAX (2UL * LONG_MAX + 1)
#define LLONG_MIN (-LLONG_MAX - 1)
#define LLONG_MAX 0x7fffffffffffffffLL
#define ULLONG_MAX (2ULL * LLONG_MAX + 1)

#define PTHREAD_STACK_MIN 2048

#define LOGIN_NAME_MAX 256
#ifndef NAME_MAX
#define NAME_MAX 255
#endif
#define TZNAME_MAX 6

#define PATH_MAX 4096
#define SSIZE_MAX LONG_MAX
#define CHAR_MAX 127

#endif
56 changes: 56 additions & 0 deletions ulib/c_libax/include/locale.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef _LOCALE_H
#define _LOCALE_H

#define LC_CTYPE 0
#define LC_NUMERIC 1
#define LC_TIME 2
#define LC_COLLATE 3
#define LC_MONETARY 4
#define LC_MESSAGES 5
#define LC_ALL 6
#define LOCALE_NAME_MAX 23

#include <stddef.h>

struct lconv {
char *decimal_point;
char *thousands_sep;
char *grouping;

char *int_curr_symbol;
char *currency_symbol;
char *mon_decimal_point;
char *mon_thousands_sep;
char *mon_grouping;
char *positive_sign;
char *negative_sign;
char int_frac_digits;
char frac_digits;
char p_cs_precedes;
char p_sep_by_space;
char n_cs_precedes;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
char int_p_cs_precedes;
char int_p_sep_by_space;
char int_n_cs_precedes;
char int_n_sep_by_space;
char int_p_sign_posn;
char int_n_sign_posn;
};

struct __locale_map {
const void *map;
size_t map_size;
char name[LOCALE_NAME_MAX + 1];
const struct __locale_map *next;
};

struct __locale_struct {
const struct __locale_map *cat[6];
};

typedef struct __locale_struct *locale_t;

#endif
4 changes: 2 additions & 2 deletions ulib/c_libax/include/stdbool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

/* Represents true-or-false values */
#ifndef __cplusplus
#define true 1
#define true 1
#define false 0
#define bool _Bool
#define bool _Bool
#endif

#endif // __STDBOOL_H__
3 changes: 3 additions & 0 deletions ulib/c_libax/include/stddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
typedef uintptr_t size_t;
typedef intptr_t ssize_t;
typedef ssize_t ptrdiff_t;
typedef int64_t off_t;

typedef int clockid_t;

#ifdef __cplusplus
#define NULL 0L
Expand Down
57 changes: 42 additions & 15 deletions ulib/c_libax/include/stdio.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
#ifndef __STDIO_H__
#define __STDIO_H__

#define stdin 0
#define stdout 1
#define stderr 2
#include <stdarg.h>
#include <stddef.h>

int getchar();
int putchar(int);
int puts(const char *s);
void fprintf(int f, const char *fmt, ...);
int fflush(int);
// TODO: complete this struct
struct IO_FILE {
int fd;
};

typedef struct IO_FILE FILE;

extern FILE *const stdin;
extern FILE *const stdout;
extern FILE *const stderr;

#define stdin (stdin)
#define stdout (stdout)
#define stderr (stderr)

#define EOF (-1)

#define printf(...) fprintf(stdout, __VA_ARGS__)
#define printf(...) fprintf(stdout->fd, __VA_ARGS__)

#define unimplemented(fmt, ...) \
printf("\x1b[33m%s%s:\x1b[0m " fmt "\n", "WARN: no ax_call implementation for ", __func__, \
##__VA_ARGS__)

struct _IO_FILE {
int fd;
};

typedef struct _IO_FILE FILE;

#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2

#define F_EOF 16
#define F_ERR 32
#define F_SVB 64
#define F_NORD 4
#define UNGET 8

#define FILENAME_MAX 4096

int getchar();
int putchar(int);
int puts(const char *s);
void fprintf(int f, const char *fmt, ...);

#ifdef AX_CONFIG_ALLOC
#ifdef AX_CONFIG_FS
FILE *fopen(const char *filename, const char *mode);
#endif
#endif

int fflush(FILE *);

int vsnprintf(char *__restrict, size_t, const char *__restrict, va_list);
int snprintf(char *__restrict, size_t, const char *__restrict, ...);
int vsprintf(char *__restrict, const char *__restrict, va_list);
int vfprintf(FILE *__restrict, const char *__restrict, va_list);
int sprintf(char *__restrict, const char *__restrict, ...);

#endif // __STDIO_H__
10 changes: 10 additions & 0 deletions ulib/c_libax/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ void srand(unsigned);
#ifdef AX_CONFIG_ALLOC
void *malloc(size_t size);
void free(void *addr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *memblock, size_t size);
#endif

_Noreturn void abort(void);
char *getenv(const char *name);

long strtol(const char *__restrict, char **__restrict, int);
long long atoll(const char *nptr);

void exit(int);

long long llabs(long long x);
int abs(int x);

#endif //__STDLIB_H__
2 changes: 2 additions & 0 deletions ulib/c_libax/include/sys/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct timespec {
long tv_nsec; /* nanoseconds */
};

typedef struct timespec timespec;

struct timezone {
int tz_minuteswest; /* (minutes west of Greenwich) */
int tz_dsttime; /* (type of DST correction) */
Expand Down
7 changes: 7 additions & 0 deletions ulib/c_libax/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
#define __TIME_H__

#include <stddef.h>
#include <sys/time.h>

typedef long time_t;

#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#define CLOCKS_PER_SEC 1000000L

struct tm {
int tm_sec; /* seconds of minute */
int tm_min; /* minutes of hour */
Expand All @@ -26,5 +31,7 @@ struct tm *gmtime(const time_t *timer);

struct tm *localtime(const time_t *timep);
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);

#endif
1 change: 1 addition & 0 deletions ulib/c_libax/include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ char *getcwd(char *buf, size_t size);
#endif

unsigned sleep(unsigned seconds);
int usleep(unsigned int useconds);

uid_t geteuid(void);
pid_t getpid(void);
Expand Down
2 changes: 1 addition & 1 deletion ulib/c_libax/src/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

_Noreturn void __assert_fail(const char *expr, const char *file, int line, const char *func)
{
fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
fprintf(stderr->fd, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
abort();
}
7 changes: 7 additions & 0 deletions ulib/c_libax/src/stderr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>

#undef stderr
FILE __stderr_FILE = {
.fd = 2,
};
FILE *const stderr = &__stderr_FILE;
8 changes: 8 additions & 0 deletions ulib/c_libax/src/stdin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>

#undef stdin
FILE __stdin_FILE = {
.fd = 0,
};

FILE *const stdin = &__stdin_FILE;
Loading

0 comments on commit a857300

Please sign in to comment.