From edb7fc07bf93e4a1883cccf45ad7a4b2cbf390d9 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 28 Aug 2022 23:54:47 +0200 Subject: user/lua: prepare libc headers --- src/user/lib/include/ctype.h | 4 +++ src/user/lib/include/locale.h | 70 ++++++++++++++++++++++++++++++++++++++++ src/user/lib/include/math.h | 29 +++++++++++++++++ src/user/lib/include/setjmp.h | 16 +++++++-- src/user/lib/include/signal.h | 5 +-- src/user/lib/include/stdio.h | 12 +++++++ src/user/lib/include/stdlib.h | 4 +++ src/user/lib/include/string.h | 9 ++++++ src/user/lib/include/sys/types.h | 3 ++ src/user/lib/include/time.h | 29 +++++++++++++++++ src/user/lib/include/unistd.h | 1 - 11 files changed, 177 insertions(+), 5 deletions(-) create mode 100644 src/user/lib/include/locale.h create mode 100644 src/user/lib/include/math.h create mode 100644 src/user/lib/include/time.h (limited to 'src/user/lib/include') diff --git a/src/user/lib/include/ctype.h b/src/user/lib/include/ctype.h index cbba529..02d7179 100644 --- a/src/user/lib/include/ctype.h +++ b/src/user/lib/include/ctype.h @@ -2,10 +2,14 @@ int isalnum(int c); int isalpha(int c); +int iscntrl(int c); int isdigit(int c); +int isgraph(int c); int islower(int c); +int ispunct(int c); int isspace(int c); int isupper(int c); +int isxdigit(int c); int tolower(int c); int toupper(int c); diff --git a/src/user/lib/include/locale.h b/src/user/lib/include/locale.h new file mode 100644 index 0000000..263dfb5 --- /dev/null +++ b/src/user/lib/include/locale.h @@ -0,0 +1,70 @@ +#pragma once +#include + +#define LC_ALL 0 +#define LC_COLLATE 1 +#define LC_CTYPE 2 +#define LC_MESSAGES 3 +#define LC_MONETARY 4 +#define LC_NUMERIC 5 +#define LC_TIME 6 + +struct lconv { + char *decimal_point; + char *thousands_sep; + char *grouping; + char *mon_decimal_point; + char *mon_thousands_sep; + char *mon_grouping; + char *positive_sign; + char *negative_sign; + char *currency_symbol; + char frac_digits; + char p_cs_precedes; + char n_cs_precedes; + char p_sep_by_space; + char n_sep_by_space; + char p_sign_posn; + char n_sign_posn; + char *int_curr_symbol; + char int_frac_digits; + char int_p_cs_precedes; + char int_n_cs_precedes; + char int_p_sep_by_space; + char int_n_sep_by_space; + char int_p_sign_posn; + char int_n_sign_posn; +}; + +static inline struct lconv *localeconv(void) { + /* per Linux's lconv(3) */ + static struct lconv locale = (struct lconv){ + .decimal_point = ".", + .thousands_sep = "", + .grouping = "", + .mon_decimal_point = "", + .mon_thousands_sep = "", + .mon_grouping = "", + .positive_sign = "", + .negative_sign = "", + .currency_symbol = "", + .frac_digits = CHAR_MAX, + .p_cs_precedes = CHAR_MAX, + .n_cs_precedes = CHAR_MAX, + .p_sep_by_space = CHAR_MAX, + .n_sep_by_space = CHAR_MAX, + .p_sign_posn = CHAR_MAX, + .n_sign_posn = CHAR_MAX, + .int_curr_symbol = "", + .int_frac_digits = CHAR_MAX, + .int_p_cs_precedes = CHAR_MAX, + .int_n_cs_precedes = CHAR_MAX, + .int_p_sep_by_space = CHAR_MAX, + .int_n_sep_by_space = CHAR_MAX, + .int_p_sign_posn = CHAR_MAX, + .int_n_sign_posn = CHAR_MAX, + }; + return &locale; +} + +char *setlocale(int category, const char *locale); diff --git a/src/user/lib/include/math.h b/src/user/lib/include/math.h new file mode 100644 index 0000000..07ff81d --- /dev/null +++ b/src/user/lib/include/math.h @@ -0,0 +1,29 @@ +#pragma once + +#define INFINITY __builtin_inff() +#define HUGE_VAL ((double)INFINITY) + +int abs(int i); + +double acos(double x); +double asin(double x); +double atan2(double x, double y); +double cos(double x); +double cosh(double x); +double sin(double x); +double sinh(double x); +double tan(double x); +double tanh(double x); + +double fabs(double x); +double floor(double x); +double ceil(double x); +double log(double x); +double log2(double x); +double log10(double x); +double exp(double x); +double fmod(double x, double y); +double frexp(double num, int *exp); +double ldexp(double x, int exp); +double pow(double x, double y); +double sqrt(double x); diff --git a/src/user/lib/include/setjmp.h b/src/user/lib/include/setjmp.h index f90bae9..51c7fd2 100644 --- a/src/user/lib/include/setjmp.h +++ b/src/user/lib/include/setjmp.h @@ -1,13 +1,25 @@ #pragma once #include -typedef char sigjmp_buf; +typedef char jmp_buf[1]; +typedef char sigjmp_buf[1]; + +static inline int setjmp(jmp_buf env) { + (void)env; + return 0; +} + static inline int sigsetjmp(sigjmp_buf env, int savemask) { (void)env; (void)savemask; return 0; } -static inline void siglongjmp(sigjmp_buf env, int val) { +static inline _Noreturn void longjmp(jmp_buf env, int val) { + (void)env; (void)val; + __libc_panic("unimplemented"); +} + +static inline _Noreturn void siglongjmp(sigjmp_buf env, int val) { (void)env; (void)val; __libc_panic("unimplemented"); } diff --git a/src/user/lib/include/signal.h b/src/user/lib/include/signal.h index c279beb..b6e9677 100644 --- a/src/user/lib/include/signal.h +++ b/src/user/lib/include/signal.h @@ -3,10 +3,11 @@ #define SIGHUP 0 #define SIGINT 0 -#define SIGWINCH 0 #define SIGQUIT 0 -#define SIG_IGN 0 +#define SIGWINCH 0 +#define SIG_DFL 0 #define SIG_ERR 0 +#define SIG_IGN 0 typedef int sig_atomic_t; diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index 5ad07e0..84fb515 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -14,8 +14,12 @@ #define SEEK_END 3 #define _IONBF 0 +#define _IOFBF 0 #define _IOLBF 1 +/* size of file buffers. not that we have any */ +#define BUFSIZ 1024 + /* stop fread() from trying to fill the entire buffer before returning * i.e. it will call _syscall_read() exactly once */ #define FEXT_NOFILL 1 @@ -37,6 +41,7 @@ FILE *fdopen(int fd, const char *mode); FILE *file_clone(const FILE *, const char *mode); FILE *popen(const char *cmd, const char *mode); int pclose(FILE *f); +FILE *tmpfile(void); int fextflags(FILE *, int extflags); int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size); @@ -51,6 +56,7 @@ int fgetc(FILE *f); int getc(FILE *f); int fputc(int c, FILE *f); int putc(int c, FILE *f); +int ungetc(int c, FILE *stream); int fseek(FILE *f, long offset, int whence); int fseeko(FILE *f, off_t offset, int whence); @@ -67,3 +73,9 @@ int getchar(void); int putchar(int c); off_t lseek(int fd, off_t off, int whence); + +int remove(const char *path); +int rename(const char *old, const char *new); + +#define L_tmpnam (5 + 16 + 1) +char *tmpnam(char *s); diff --git a/src/user/lib/include/stdlib.h b/src/user/lib/include/stdlib.h index e2e3996..8c827e3 100644 --- a/src/user/lib/include/stdlib.h +++ b/src/user/lib/include/stdlib.h @@ -6,7 +6,11 @@ #include #endif +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 + _Noreturn void abort(void); +_Noreturn void exit(int); int mkstemp(char *template); char *getenv(const char *name); diff --git a/src/user/lib/include/string.h b/src/user/lib/include/string.h index 7235da9..cf621d5 100644 --- a/src/user/lib/include/string.h +++ b/src/user/lib/include/string.h @@ -2,12 +2,21 @@ #include long strtol(const char *restrict s, char **restrict end, int base); +double strtod(const char *restrict s, char **restrict end); char *strchr(const char *s, int c); size_t strspn(const char *s, const char *accept); size_t strcspn(const char *s, const char *reject); +char *strpbrk(const char *s1, const char *s2); char *strtok(char *restrict s, const char *restrict sep); char *strtok_r(char *restrict s, const char *restrict sep, char **restrict state); int strncmp(const char *s1, const char *s2, size_t n); +int strcoll(const char *s1, const char *s2); + +char *strstr(const char *s1, const char *s2); + +char *strcpy(char *restrict s1, const char *restrict s2); + +char *strerror(int errnum); diff --git a/src/user/lib/include/sys/types.h b/src/user/lib/include/sys/types.h index 3bc3f7f..3b1772b 100644 --- a/src/user/lib/include/sys/types.h +++ b/src/user/lib/include/sys/types.h @@ -1,4 +1,7 @@ #pragma once #include +#include typedef long long off_t; +typedef int64_t time_t; +typedef uint64_t clock_t; diff --git a/src/user/lib/include/time.h b/src/user/lib/include/time.h new file mode 100644 index 0000000..067c35f --- /dev/null +++ b/src/user/lib/include/time.h @@ -0,0 +1,29 @@ +#pragma once +#include + +#define CLOCKS_PER_SEC 1000000 + +struct tm { + int tm_sec; /* Seconds [0,60]. */ + int tm_min; /* Minutes [0,59]. */ + int tm_hour; /* Hour [0,23]. */ + int tm_mday; /* Day of month [1,31]. */ + int tm_mon; /* Month of year [0,11]. */ + int tm_year; /* Years since 1900. */ + int tm_wday; /* Day of week [0,6] (Sunday =0). */ + int tm_yday; /* Day of year [0,365]. */ + int tm_isdst; /* Daylight Savings flag. */ +}; + +time_t time(time_t *tloc); +clock_t clock(void); + +struct tm *gmtime(const time_t *timer); +struct tm *localtime(const time_t *timer); +time_t mktime(struct tm *timeptr); + +double difftime(time_t time1, time_t time0); + +size_t strftime( + char *restrict s, size_t maxsize, + const char *restrict format, const struct tm *restrict timeptr); diff --git a/src/user/lib/include/unistd.h b/src/user/lib/include/unistd.h index 59af24b..f49c337 100644 --- a/src/user/lib/include/unistd.h +++ b/src/user/lib/include/unistd.h @@ -4,7 +4,6 @@ int fork(void); int close(handle_t h); -_Noreturn void exit(int); _Noreturn void _exit(int); int unlink(const char *path); -- cgit v1.2.3