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 29, 2023
1 parent 939e63d commit 69419b8
Show file tree
Hide file tree
Showing 22 changed files with 1,097 additions and 45 deletions.
20 changes: 20 additions & 0 deletions ulib/c_libax/include/ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _CTYPE_H
#define _CTYPE_H

int tolower(int __c);
int toupper(int __c);

int isprint(int);
int isalpha(int c);
int isalnum(int);
int isupper(int c);
int islower(int c);

int isxdigit(int);
int isgraph(int);
int iscntrl(int);
int ispunct(int);

int isascii(int);

#endif
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 @@ -2,12 +2,15 @@
#define __STDDEF_H__

#include <stdint.h>
#include <sys/types.h>

/* size_t is used for memory object sizes */
typedef uintptr_t size_t;
typedef intptr_t ssize_t;
typedef ssize_t ptrdiff_t;

typedef int clockid_t;

#ifdef __cplusplus
#define NULL 0L
#else
Expand Down
55 changes: 40 additions & 15 deletions ulib/c_libax/include/stdio.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
#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, ...);

#if defined(AX_CONFIG_ALLOC) && defined(AX_CONFIG_FS)
FILE *fopen(const char *filename, const char *mode);
#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__
14 changes: 14 additions & 0 deletions ulib/c_libax/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,23 @@ 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);
unsigned long strtoul(const char *nptr, char **endptr, int base);
long long strtoll(const char *nptr, char **endptr, int base);
unsigned long long strtoull(const char *nptr, char **endptr, int base);

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
9 changes: 9 additions & 0 deletions ulib/c_libax/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
#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

#define ax_time_usec_to_nsec(usec) ((usec)*1000UL)

struct tm {
int tm_sec; /* seconds of minute */
int tm_min; /* minutes of hour */
Expand All @@ -26,5 +33,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
2 changes: 1 addition & 1 deletion ulib/c_libax/include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <stddef.h>
#include <sys/stat.h>
#include <sys/types.h>

#define _SC_PAGESIZE 30

Expand All @@ -27,6 +26,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();
}
67 changes: 67 additions & 0 deletions ulib/c_libax/src/ctype.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int isupper(int c)
{
return (unsigned)c - 'A' < 26;
}

int tolower(int c)
{
if (isupper(c))
return c | 32;
return c;
}

int islower(int c)
{
return (unsigned)c - 'a' < 26;
}

int toupper(int c)
{
if (islower(c))
return c & 0x5f;
return c;
}

int isgraph(int c)
{
return (unsigned)c - 0x21 < 0x5e;
}

int isalpha(int c)
{
return ((unsigned)c | 32) - 'a' < 26;
}

int isprint(int c)
{
return (unsigned)c - 0x20 < 0x5f;
}

int isalnum(int c)
{
return isalpha(c) || isdigit(c);
}

int iscntrl(int c)
{
return (unsigned)c < 0x20 || c == 0x7f;
}

int ispunct(int c)
{
return isgraph(c) && !isalnum(c);
}

int isxdigit(int c)
{
return isdigit(c) || ((unsigned)c | 32) - 'a' < 6;
}

int isascii(int c)
{
return !(c & ~0x7f);
}
1 change: 0 additions & 1 deletion ulib/c_libax/src/mmap.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>

// TODO:
void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
Expand Down
Loading

0 comments on commit 69419b8

Please sign in to comment.