diff --git a/lib/libc/include/assert.h b/lib/libc/include/assert.h new file mode 100644 index 0000000..4b00daa --- /dev/null +++ b/lib/libc/include/assert.h @@ -0,0 +1,21 @@ +#ifndef ASSERT_H_ +#define ASSERT_H_ + +#define assert(cond) \ + do { \ + if (!(cond)) { \ + __libc_assert_failed( \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + #cond); \ + } \ + } while (0) + +extern void __libc_assert_failed( + const char *file, + int line, + const char *func, + const char *cond); + +#endif diff --git a/lib/libc/include/ctype.h b/lib/libc/include/ctype.h new file mode 100644 index 0000000..58dccde --- /dev/null +++ b/lib/libc/include/ctype.h @@ -0,0 +1,15 @@ +#ifndef CTYPE_H_ +#define CTYPE_H_ + +extern int isalpha(int c); +extern int isalnum(int c); +extern int isupper(int c); +extern int isspace(int c); +extern int ispunct(int c); +extern int isdigit(int c); +extern int isxdigit(int c); + +extern int tolower(int c); +extern int toupper(int c); + +#endif diff --git a/lib/libc/include/inttypes.h b/lib/libc/include/inttypes.h new file mode 100644 index 0000000..5790336 --- /dev/null +++ b/lib/libc/include/inttypes.h @@ -0,0 +1,192 @@ +#ifndef INTTYPES_H_ +#define INTTYPES_H_ + +#define __PRI_8_LENGTH_MODIFIER__ "hh" +#define __PRI_64_LENGTH_MODIFIER__ "ll" +#define __SCN_64_LENGTH_MODIFIER__ "ll" +#define __PRI_MAX_LENGTH_MODIFIER__ "j" +#define __SCN_MAX_LENGTH_MODIFIER__ "j" + +#define PRId8 __PRI_8_LENGTH_MODIFIER__ "d" +#define PRIi8 __PRI_8_LENGTH_MODIFIER__ "i" +#define PRIo8 __PRI_8_LENGTH_MODIFIER__ "o" +#define PRIu8 __PRI_8_LENGTH_MODIFIER__ "u" +#define PRIx8 __PRI_8_LENGTH_MODIFIER__ "x" +#define PRIX8 __PRI_8_LENGTH_MODIFIER__ "X" + +#define PRId16 "hd" +#define PRIi16 "hi" +#define PRIo16 "ho" +#define PRIu16 "hu" +#define PRIx16 "hx" +#define PRIX16 "hX" + +#define PRId32 "d" +#define PRIi32 "i" +#define PRIo32 "o" +#define PRIu32 "u" +#define PRIx32 "x" +#define PRIX32 "X" + +#define PRId64 __PRI_64_LENGTH_MODIFIER__ "d" +#define PRIi64 __PRI_64_LENGTH_MODIFIER__ "i" +#define PRIo64 __PRI_64_LENGTH_MODIFIER__ "o" +#define PRIu64 __PRI_64_LENGTH_MODIFIER__ "u" +#define PRIx64 __PRI_64_LENGTH_MODIFIER__ "x" +#define PRIX64 __PRI_64_LENGTH_MODIFIER__ "X" + +#define PRIdLEAST8 PRId8 +#define PRIiLEAST8 PRIi8 +#define PRIoLEAST8 PRIo8 +#define PRIuLEAST8 PRIu8 +#define PRIxLEAST8 PRIx8 +#define PRIXLEAST8 PRIX8 + +#define PRIdLEAST16 PRId16 +#define PRIiLEAST16 PRIi16 +#define PRIoLEAST16 PRIo16 +#define PRIuLEAST16 PRIu16 +#define PRIxLEAST16 PRIx16 +#define PRIXLEAST16 PRIX16 + +#define PRIdLEAST32 PRId32 +#define PRIiLEAST32 PRIi32 +#define PRIoLEAST32 PRIo32 +#define PRIuLEAST32 PRIu32 +#define PRIxLEAST32 PRIx32 +#define PRIXLEAST32 PRIX32 + +#define PRIdLEAST64 PRId64 +#define PRIiLEAST64 PRIi64 +#define PRIoLEAST64 PRIo64 +#define PRIuLEAST64 PRIu64 +#define PRIxLEAST64 PRIx64 +#define PRIXLEAST64 PRIX64 + +#define PRIdFAST8 PRId8 +#define PRIiFAST8 PRIi8 +#define PRIoFAST8 PRIo8 +#define PRIuFAST8 PRIu8 +#define PRIxFAST8 PRIx8 +#define PRIXFAST8 PRIX8 + +#define PRIdFAST16 PRId16 +#define PRIiFAST16 PRIi16 +#define PRIoFAST16 PRIo16 +#define PRIuFAST16 PRIu16 +#define PRIxFAST16 PRIx16 +#define PRIXFAST16 PRIX16 + +#define PRIdFAST32 PRId32 +#define PRIiFAST32 PRIi32 +#define PRIoFAST32 PRIo32 +#define PRIuFAST32 PRIu32 +#define PRIxFAST32 PRIx32 +#define PRIXFAST32 PRIX32 + +#define PRIdFAST64 PRId64 +#define PRIiFAST64 PRIi64 +#define PRIoFAST64 PRIo64 +#define PRIuFAST64 PRIu64 +#define PRIxFAST64 PRIx64 +#define PRIXFAST64 PRIX64 + +#define PRIdPTR "ld" +#define PRIiPTR "li" +#define PRIoPTR "lo" +#define PRIuPTR "lu" +#define PRIxPTR "lx" +#define PRIXPTR "lX" + +#define PRIdMAX __PRI_MAX_LENGTH_MODIFIER__ "d" +#define PRIiMAX __PRI_MAX_LENGTH_MODIFIER__ "i" +#define PRIoMAX __PRI_MAX_LENGTH_MODIFIER__ "o" +#define PRIuMAX __PRI_MAX_LENGTH_MODIFIER__ "u" +#define PRIxMAX __PRI_MAX_LENGTH_MODIFIER__ "x" +#define PRIXMAX __PRI_MAX_LENGTH_MODIFIER__ "X" + +#define SCNd8 __PRI_8_LENGTH_MODIFIER__ "d" +#define SCNi8 __PRI_8_LENGTH_MODIFIER__ "i" +#define SCNo8 __PRI_8_LENGTH_MODIFIER__ "o" +#define SCNu8 __PRI_8_LENGTH_MODIFIER__ "u" +#define SCNx8 __PRI_8_LENGTH_MODIFIER__ "x" + +#define SCNd16 "hd" +#define SCNi16 "hi" +#define SCNo16 "ho" +#define SCNu16 "hu" +#define SCNx16 "hx" + +#define SCNd32 "d" +#define SCNi32 "i" +#define SCNo32 "o" +#define SCNu32 "u" +#define SCNx32 "x" + +#define SCNd64 __SCN_64_LENGTH_MODIFIER__ "d" +#define SCNi64 __SCN_64_LENGTH_MODIFIER__ "i" +#define SCNo64 __SCN_64_LENGTH_MODIFIER__ "o" +#define SCNu64 __SCN_64_LENGTH_MODIFIER__ "u" +#define SCNx64 __SCN_64_LENGTH_MODIFIER__ "x" + +#define SCNdLEAST8 SCNd8 +#define SCNiLEAST8 SCNi8 +#define SCNoLEAST8 SCNo8 +#define SCNuLEAST8 SCNu8 +#define SCNxLEAST8 SCNx8 + +#define SCNdLEAST16 SCNd16 +#define SCNiLEAST16 SCNi16 +#define SCNoLEAST16 SCNo16 +#define SCNuLEAST16 SCNu16 +#define SCNxLEAST16 SCNx16 + +#define SCNdLEAST32 SCNd32 +#define SCNiLEAST32 SCNi32 +#define SCNoLEAST32 SCNo32 +#define SCNuLEAST32 SCNu32 +#define SCNxLEAST32 SCNx32 + +#define SCNdLEAST64 SCNd64 +#define SCNiLEAST64 SCNi64 +#define SCNoLEAST64 SCNo64 +#define SCNuLEAST64 SCNu64 +#define SCNxLEAST64 SCNx64 + +#define SCNdFAST8 SCNd8 +#define SCNiFAST8 SCNi8 +#define SCNoFAST8 SCNo8 +#define SCNuFAST8 SCNu8 +#define SCNxFAST8 SCNx8 + +#define SCNdFAST16 SCNd16 +#define SCNiFAST16 SCNi16 +#define SCNoFAST16 SCNo16 +#define SCNuFAST16 SCNu16 +#define SCNxFAST16 SCNx16 + +#define SCNdFAST32 SCNd32 +#define SCNiFAST32 SCNi32 +#define SCNoFAST32 SCNo32 +#define SCNuFAST32 SCNu32 +#define SCNxFAST32 SCNx32 + +#define SCNdFAST64 SCNd64 +#define SCNiFAST64 SCNi64 +#define SCNoFAST64 SCNo64 +#define SCNuFAST64 SCNu64 +#define SCNxFAST64 SCNx64 + +#define SCNdPTR "ld" +#define SCNiPTR "li" +#define SCNoPTR "lo" +#define SCNuPTR "lu" +#define SCNxPTR "lx" + +#define SCNdMAX __SCN_MAX_LENGTH_MODIFIER__ "d" +#define SCNiMAX __SCN_MAX_LENGTH_MODIFIER__ "i" +#define SCNoMAX __SCN_MAX_LENGTH_MODIFIER__ "o" +#define SCNuMAX __SCN_MAX_LENGTH_MODIFIER__ "u" +#define SCNxMAX __SCN_MAX_LENGTH_MODIFIER__ "x" + +#endif diff --git a/lib/libc/include/stdio.h b/lib/libc/include/stdio.h index 3a98218..d8327e3 100644 --- a/lib/libc/include/stdio.h +++ b/lib/libc/include/stdio.h @@ -3,8 +3,13 @@ #include #include +#include -#define EOF -1 +#define EOF -1 + +#define _IONBF 0 +#define _IOFBF 1 +#define _IOLBF 2 #ifdef __cplusplus extern "C" { @@ -13,6 +18,13 @@ extern "C" { struct __opaque_file; typedef struct __opaque_file FILE; +#define stdin (__libc_file_stdin()) +#define stdout (__libc_file_stdout()) +#define stderr (__libc_file_stderr()) + +extern int printf(const char *format, ...); +extern int vprintf(const char *format, va_list arg); +extern int fprintf(FILE *stream, const char *format, ...); extern int snprintf(char *buffer, size_t count, const char *format, ...); extern int vsnprintf( char *buffer, @@ -24,7 +36,7 @@ extern FILE *fopen(const char *path, const char *mode); extern int fclose(FILE *stream); extern int feof(FILE *stream); -extern int ferr(FILE *stream); +extern int ferror(FILE *stream); extern size_t fread(void *buf, size_t size, size_t count, FILE *stream); extern size_t fwrite(const void *buf, size_t size, size_t count, FILE *stream); @@ -44,6 +56,24 @@ static inline int putc(int c, FILE *stream) extern char *fgets(char *restrict str, int count, FILE *restrict stream); extern int fputs(const char *restrict str, FILE *restrict stream); +extern long ftell(FILE *stream); +extern int fseek(FILE *stream, long offset, int origin); + +extern int sscanf( + const char *restrict buffer, + const char *restrict format, + ...); + +extern int setvbuf( + FILE *restrict stream, + char *restrict buf, + int mode, + size_t size); + +extern FILE *__libc_file_stdin(void); +extern FILE *__libc_file_stdout(void); +extern FILE *__libc_file_stderr(void); + #ifdef __cplusplus } #endif diff --git a/lib/libc/include/stdlib.h b/lib/libc/include/stdlib.h index 9bab2c4..5ad820a 100644 --- a/lib/libc/include/stdlib.h +++ b/lib/libc/include/stdlib.h @@ -4,10 +4,27 @@ #include extern void abort(void); +extern void exit(int result); +extern int atexit(void (*func)(void)); extern void *malloc(size_t count); extern void *calloc(size_t count, size_t size); extern void *realloc(void *p, size_t count); extern void free(void *p); +extern long strtol(const char *restrict str, char **restrict str_end, int base); +extern long long strtoll( + const char *restrict str, + char **restrict str_end, + int base); + +extern unsigned long strtoul( + const char *restrict str, + char **restrict endptr, + int base); +extern unsigned long long strtoull( + const char *restrict str, + char **restrict str_end, + int base); + #endif diff --git a/lib/libc/include/string.h b/lib/libc/include/string.h index b4e2b44..22ae8ce 100644 --- a/lib/libc/include/string.h +++ b/lib/libc/include/string.h @@ -20,6 +20,8 @@ extern int strncpy(const char *s1, const char *s2, unsigned long n); extern void *memset(void *str, int c, size_t n); extern void *memcpy(void *dst, const void *src, size_t len); +extern int memcmp(const void *a, const void *b, size_t len); +extern void *memmove(void *dst, const void *src, size_t len); extern char *strdup(char *s); diff --git a/lib/libc/include/time.h b/lib/libc/include/time.h new file mode 100644 index 0000000..404abb3 --- /dev/null +++ b/lib/libc/include/time.h @@ -0,0 +1,4 @@ +#ifndef TIME_H_ +#define TIME_H_ + +#endif diff --git a/lib/libc/include/wctype.h b/lib/libc/include/wctype.h new file mode 100644 index 0000000..e0c2676 --- /dev/null +++ b/lib/libc/include/wctype.h @@ -0,0 +1,12 @@ +#ifndef WCTYPE_H_ +#define WCTYPE_H_ + +#include +#include + +extern int iswnumber(wchar_t c); +extern int iswxdigit(wchar_t c); +extern int iswspace(wchar_t c); +extern int iswpunct(wchar_t c); + +#endif