Browse Source

More libc for Python (almost done)

K. Lange 4 years ago
parent
commit
5cd281a1a6

+ 4 - 0
base/usr/include/assert.h

@@ -1 +1,5 @@
+#ifdef DEBUG
 #define assert(statement) ((statement) ? (void)0 : __assert_func(__FILE__, __LINE__, __FUNCTION__, #statement))
+#else
+#define assert(statement) ((void)0)
+#endif

+ 10 - 0
base/usr/include/fcntl.h

@@ -11,11 +11,21 @@
 #define O_EXCL       0x0800
 #define O_NOFOLLOW   0x1000
 #define O_PATH       0x2000
+#define O_NONBLOCK   0x4000
 
 #define F_OK 1
 #define R_OK 4
 #define W_OK 2
 #define X_OK 1
 
+#define F_GETFD 1
+#define F_SETFD 2
+
+#define F_GETFL 3
+#define F_SETFL 4
+
+#define FD_CLOEXEC (1 << 0)
+
 extern int open (const char *, int, ...);
 extern int chmod(const char *path, mode_t mode);
+extern int fcntl(int fd, int cmd, ...);

+ 1 - 0
base/usr/include/signal.h

@@ -12,3 +12,4 @@ typedef _sig_func_ptr sighandler_t;
 typedef int sig_atomic_t;
 
 extern sighandler_t signal(int signum, sighandler_t handler);
+extern int raise(int sig);

+ 3 - 2
base/usr/include/stdio.h

@@ -32,9 +32,10 @@ extern int fflush(FILE * stream);
 
 extern size_t vasprintf(char ** buf, const char *fmt, va_list args);
 extern int    sprintf(char *buf, const char *fmt, ...);
-extern int    fprintf(FILE *stream, char *fmt, ...);
-extern int    printf(char *fmt, ...);
+extern int    fprintf(FILE *stream, const char *fmt, ...);
+extern int    printf(const char *fmt, ...);
 extern int snprintf(char * buf, size_t size, const char * fmt, ...);
+extern int vsprintf(char * buf, const char *fmt, va_list args);
 
 extern int puts(const char *s);
 extern int fputs(const char *s, FILE *stream);

+ 1 - 0
base/usr/include/string.h

@@ -36,6 +36,7 @@ extern int atoi(const char * s);
 
 extern char * strcat(char *dest, const char *src);
 
+extern char * strtok(char * str, const char * delim);
 extern char * strtok_r(char * str, const char * delim, char ** saveptr);
 
 extern char * strncpy(char *dest, const char *src, size_t n);

+ 6 - 0
base/usr/include/wchar.h

@@ -3,3 +3,9 @@
 #include <stddef.h>
 
 extern int wcwidth(wchar_t c);
+extern wchar_t * wcsncpy(wchar_t * dest, const wchar_t * src, size_t n);
+extern size_t wcslen(const wchar_t * s);
+extern int wcscmp(const wchar_t *s1, const wchar_t *s2);
+extern wchar_t * wcscat(wchar_t *dest, const wchar_t *src);
+
+typedef unsigned int wint_t;

+ 6 - 0
libc/signal/raise.c

@@ -0,0 +1,6 @@
+#include <signal.h>
+#include <unistd.h>
+
+int raise(int sig) {
+	return kill(getpid(), sig);
+}

+ 6 - 2
libc/stdio/printf.c

@@ -208,7 +208,11 @@ size_t vasprintf(char ** buf, const char * fmt, va_list args) {
 	return xvasprintf(b, fmt, args);
 }
 
-int fprintf(FILE * device, char *fmt, ...) {
+int vsprintf(char * buf, const char *fmt, va_list args) {
+	return xvasprintf(buf, fmt, args);
+}
+
+int fprintf(FILE * device, const char *fmt, ...) {
 	va_list args;
 	va_start(args, fmt);
 	char * buffer;
@@ -220,7 +224,7 @@ int fprintf(FILE * device, char *fmt, ...) {
 	return out;
 }
 
-int printf(char *fmt, ...) {
+int printf(const char *fmt, ...) {
 	va_list args;
 	va_start(args, fmt);
 	char * buffer;

+ 30 - 0
libc/string/str.c

@@ -391,6 +391,28 @@ int atoi(const char * s) {
 	return neg ? n : -n;
 }
 
+long int strtol(const char * s, char **endptr, int base) {
+	int n = 0;
+	int neg = 0;
+	while (isspace(*s)) {
+		s++;
+	}
+	switch (*s) {
+		case '-':
+			neg = 1;
+			/* Fallthrough is intentional here */
+		case '+':
+			s++;
+	}
+	while (isdigit(*s)) {
+		n = 10*n - (*s++ - '0');
+	}
+	/* The sign order may look incorrect here but this is correct as n is calculated
+	 * as a negative number to avoid overflow on INT_MAX.
+	 */
+	return neg ? n : -n;
+}
+
 size_t lfind(const char * str, const char accept) {
 	return (size_t)strchr(str, accept);
 }
@@ -420,6 +442,14 @@ char * strtok_r(char * str, const char * delim, char ** saveptr) {
 	return token;
 }
 
+char * strtok(char * str, const char * delim) {
+	static char * saveptr = NULL;
+	if (str) {
+			saveptr = NULL;
+	}
+	return strtok_r(str, delim, &saveptr);
+}
+
 char * strcat(char *dest, const char *src) {
 	char * end = dest;
 	while (*end != '\0') {

+ 11 - 0
libc/unistd/fcntl.c

@@ -0,0 +1,11 @@
+#include <fcntl.h>
+
+int fcntl(int fd, int cmd, ...) {
+    switch (cmd) {
+        case F_GETFD:
+            return 0;
+        case F_SETFD:
+            return 0;
+    }
+    return -1;
+}

+ 15 - 0
libc/wchar/wcscat.c

@@ -0,0 +1,15 @@
+#include <wchar.h>
+
+wchar_t * wcscat(wchar_t *dest, const wchar_t *src) {
+	wchar_t * end = dest;
+	while (*end != 0) {
+		++end;
+	}
+	while (*src) {
+		*end = *src;
+		end++;
+		src++;
+	}
+	*end = 0;
+	return dest;
+}

+ 6 - 0
libc/wchar/wcscmp.c

@@ -0,0 +1,6 @@
+#include <wchar.h>
+
+int wcscmp(const wchar_t *l, const wchar_t *r) {
+	for (; *l == *r && *l; l++, r++);
+	return *(unsigned int *)l - *(unsigned int *)r;
+}

+ 7 - 0
libc/wchar/wcscpy.c

@@ -0,0 +1,7 @@
+#include <wchar.h>
+
+wchar_t * wcscpy(wchar_t * restrict dest, const wchar_t * restrict src) {
+	wchar_t * out = dest;
+	for (; (*dest=*src); src++, dest++);
+	return out;
+}

+ 10 - 0
libc/wchar/wcslen.c

@@ -0,0 +1,10 @@
+#include <wchar.h>
+
+size_t wcslen(const wchar_t * s) {
+	size_t out = 0;
+	while (*s) {
+		out++;
+		s++;
+	}
+	return out;
+}

+ 13 - 0
libc/wchar/wcsncpy.c

@@ -0,0 +1,13 @@
+#include <wchar.h>
+
+wchar_t * wcsncpy(wchar_t * dest, const wchar_t * src, size_t n) {
+	wchar_t * out = dest;
+	while (n > 0) {
+		if (!*src) break;
+		*dest = *src;
+		dest++;
+		src++;
+		n--;
+	}
+	return out;
+}