summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-10-03 15:08:45 +0200
committerAnton Kling <anton@kling.gg>2024-10-03 15:08:45 +0200
commit226d861c9ebb7f09f95665d07d9ab5c6b7ed7d6f (patch)
treed900c3437d3d65ee7bd88474586dc6dd947b14ce
parent64b0b49a1f23da9faa680f8158cb8a12cd4d118c (diff)
libc: small changes
-rw-r--r--userland/libc/Makefile4
-rw-r--r--userland/libc/include/math.h1
-rw-r--r--userland/libc/include/signal.h55
-rw-r--r--userland/libc/include/sys/mman.h1
-rw-r--r--userland/libc/include/sys/time.h2
-rw-r--r--userland/libc/include/sys/ucontext.h1
-rw-r--r--userland/libc/include/syscall.h3
-rw-r--r--userland/libc/include/tb/sb.h4
-rw-r--r--userland/libc/include/tb/sv.h1
-rw-r--r--userland/libc/include/time.h37
-rw-r--r--userland/libc/malloc/malloc.c11
-rw-r--r--userland/libc/stdio/fclose.c6
-rw-r--r--userland/libc/stdio/fopen.c1
-rw-r--r--userland/libc/stdio/fwrite.c1
-rw-r--r--userland/libc/stdio/vdprintf.c11
-rw-r--r--userland/libc/stdlib/realpath.c2
-rw-r--r--userland/libc/string/memcpy.c1
-rw-r--r--userland/libc/tb/sb.c22
-rw-r--r--userland/libc/tb/sv.c21
19 files changed, 73 insertions, 112 deletions
diff --git a/userland/libc/Makefile b/userland/libc/Makefile
index 936bde6..34e9016 100644
--- a/userland/libc/Makefile
+++ b/userland/libc/Makefile
@@ -1,8 +1,8 @@
CC="i686-sb-gcc"
AR="i686-sb-ar"
AS="i686-sb-as"
-CFLAGS = -ggdb -ffreestanding -nostdlib -O0 -Wall -Wextra -pedantic -Werror -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter
-OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o sys/mman/munmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o systemcall.o sys/random/randomfill.o fcntl/open.o unistd/write.o unistd/pwrite.o fcntl/open_process.o tb/sb.o tb/sv.o string/memchr.o stdlib/atol.o stdlib/atoll.o stdlib/strtoll.o sys/stat/fstat.o unistd/lseek.o ctype/isupper.o ctype/islower.o ctype/isblank.o ctype/isgraph.o ctype/iscntrl.o math/ldexp.o sys/socket/connect.o sys/socket/setsockopt.o arpa/inet/ntohl.o arpa/inet/ntohs.o netdb/getaddrinfo.o tb/sha1.o sys/socket/getpeername.o fcntl/fcntl.o
+CFLAGS = -ggdb -ffreestanding -nostdlib -Ofast -Wall -Wextra -pedantic -Werror -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter
+OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o sys/mman/munmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o systemcall.o sys/random/randomfill.o fcntl/open.o unistd/write.o unistd/pwrite.o fcntl/open_process.o tb/sb.o tb/sv.o string/memchr.o stdlib/atol.o stdlib/atoll.o stdlib/strtoll.o sys/stat/fstat.o unistd/lseek.o ctype/isupper.o ctype/islower.o ctype/isblank.o ctype/isgraph.o ctype/iscntrl.o math/ldexp.o sys/socket/connect.o sys/socket/setsockopt.o arpa/inet/ntohl.o arpa/inet/ntohs.o netdb/getaddrinfo.o tb/sha1.o sys/socket/getpeername.o fcntl/fcntl.o queue.o
all: libc.a
%.o: %.c
diff --git a/userland/libc/include/math.h b/userland/libc/include/math.h
index d85b3a0..7168a5d 100644
--- a/userland/libc/include/math.h
+++ b/userland/libc/include/math.h
@@ -14,3 +14,4 @@
#define HUGE_VALL ((long double)INFINITY)
double ldexp(double x, int exp);
+double fmin(double x, double y);
diff --git a/userland/libc/include/signal.h b/userland/libc/include/signal.h
deleted file mode 100644
index e5f8d8b..0000000
--- a/userland/libc/include/signal.h
+++ /dev/null
@@ -1,55 +0,0 @@
-#ifndef SIGNAL_H
-#define SIGNAL_H
-#include <sys/types.h>
-#define SIGHUP 0
-#define SIGINT 1
-#define SIGWINCH 2
-#define SIGQUIT 3
-#define SIG_IGN 4
-#define SIGSEGV 5
-#define SIGILL 6
-#define SIGABRT 7
-#define SIGBUS 8
-#define SIGKILL 9
-#define SIGFPE 10
-#define SIGTERM 15
-#define SIG_DFL 16
-
-typedef int pid_t;
-typedef int sigset_t;
-
-union sigval {
- int sival_int; // Integer signal value.
- void *sival_ptr; // Pointer signal value.
-};
-
-struct siginfo {
- int si_signo; // Signal number.
- int si_code; // Signal code.
- int si_errno; // If non-zero, an errno value associated with
- // this signal, as described in <errno.h>.
- pid_t si_pid; // Sending process ID.
- uid_t si_uid; // Real user ID of sending process.
- void *si_addr; // Address of faulting instruction.
- int si_status; // Exit value or signal.
- long si_band; // Band event for SIGPOL9
- union sigval si_value; // Signal value.
-};
-
-typedef struct siginfo siginfo_t;
-
-int kill(int fd, int sig);
-int kill_fd(int fd, int sig);
-
-struct sigaction {
- void (*sa_handler)(int); // Pointer to a signal-catching function or one of
- // the macros SIG_IGN or SIG_DFL.
- sigset_t sa_mask; // Additional set of signals to be blocked during execution
- // of signal-catching function.
- int sa_flags; // Special flags to affect behavior of signal.
- void (*sa_sigaction)(int, siginfo_t *,
- void *); // Pointer to a signal-catching function.
-};
-
-int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
-#endif // SIGNAL_H
diff --git a/userland/libc/include/sys/mman.h b/userland/libc/include/sys/mman.h
index d60997d..91ee816 100644
--- a/userland/libc/include/sys/mman.h
+++ b/userland/libc/include/sys/mman.h
@@ -7,6 +7,7 @@
#define PROT_READ (1 << 0)
#define PROT_WRITE (1 << 1)
+#define PROT_EXEC (1 << 2)
#define MAP_PRIVATE (1 << 0)
#define MAP_ANONYMOUS (1 << 1)
diff --git a/userland/libc/include/sys/time.h b/userland/libc/include/sys/time.h
index a675d9e..4f5cbd7 100644
--- a/userland/libc/include/sys/time.h
+++ b/userland/libc/include/sys/time.h
@@ -2,9 +2,9 @@
#define TIME_H
#include <stddef.h>
#include <sys/types.h>
-#include <time.h>
#define CLOCK_REALTIME 0
+#define CLOCK_MONOTONIC_RAW 1
struct tm {
int tm_sec;
diff --git a/userland/libc/include/sys/ucontext.h b/userland/libc/include/sys/ucontext.h
index e69de29..5fdbd63 100644
--- a/userland/libc/include/sys/ucontext.h
+++ b/userland/libc/include/sys/ucontext.h
@@ -0,0 +1 @@
+#include <ucontext.h>
diff --git a/userland/libc/include/syscall.h b/userland/libc/include/syscall.h
index 0396dce..87622c5 100644
--- a/userland/libc/include/syscall.h
+++ b/userland/libc/include/syscall.h
@@ -48,6 +48,9 @@
#define SYS_GETPEERNAME 40
#define SYS_FCNTL 41
#define SYS_CLOCK_GETTIME 42
+#define SYS_QUEUE_CREATE 43
+#define SYS_QUEUE_MOD_ENTRIES 44
+#define SYS_QUEUE_WAIT 45
int syscall(uint32_t eax, uint32_t ebx, uint32_t ecx, uint32_t edx,
uint32_t esi, uint32_t edi);
diff --git a/userland/libc/include/tb/sb.h b/userland/libc/include/tb/sb.h
index 84f8161..06aa084 100644
--- a/userland/libc/include/tb/sb.h
+++ b/userland/libc/include/tb/sb.h
@@ -11,13 +11,15 @@ struct sb {
struct sv;
-void sb_init(struct sb *ctx);
+void sb_init(struct sb *ctx, size_t starting_capacity);
void sb_free(struct sb *ctx);
void sb_reset(struct sb *ctx);
int sb_isempty(const struct sb *ctx);
void sb_append_char(struct sb *ctx, char c);
int sb_delete_right(struct sb *ctx, size_t n);
void sb_append(struct sb *ctx, const char *s);
+void sb_append_buffer(struct sb *ctx, const char *buffer,
+ size_t length);
void sb_append_sv(struct sb *ctx, struct sv sv);
void sb_prepend_sv(struct sb *ctx, struct sv sv);
#endif
diff --git a/userland/libc/include/tb/sv.h b/userland/libc/include/tb/sv.h
index 54ec218..3d4f9fb 100644
--- a/userland/libc/include/tb/sv.h
+++ b/userland/libc/include/tb/sv.h
@@ -25,6 +25,7 @@ struct sv sv_skip_chars(const struct sv input, const char *chars);
struct sv sv_split_function(const struct sv input, struct sv *rest,
int (*function)(int));
struct sv sv_take(struct sv s, struct sv *rest, size_t n);
+struct sv sv_take_end(struct sv s, struct sv *rest, size_t n);
struct sv sv_next(struct sv s);
int sv_isempty(struct sv s);
char sv_peek(struct sv s);
diff --git a/userland/libc/include/time.h b/userland/libc/include/time.h
index b494f6c..18a03a5 100644
--- a/userland/libc/include/time.h
+++ b/userland/libc/include/time.h
@@ -1,36 +1 @@
-#ifndef TIME_H
-#define TIME_H
-#include <sys/types.h>
-#include <stddef.h>
-
-#define CLOCK_REALTIME 0
-#define CLOCK_MONOTONIC_RAW 1
-
-struct tm {
- int tm_sec;
- int tm_min;
- int tm_hour;
- int tm_mday;
- int tm_mon;
- int tm_year;
- int tm_wday;
- int tm_yday;
- int tm_isdst;
- long __tm_gmtoff;
- const char *__tm_zone;
-};
-
-typedef int clockid_t;
-struct timespec {
- time_t tv_sec; // Seconds.
- long tv_nsec; // Nanoseconds.
-};
-
-time_t time(time_t *tloc);
-int clock_gettime(clockid_t clock_id, struct timespec *tp);
-struct tm *localtime(const time_t *timer);
-struct tm *gmtime(const time_t *timer);
-size_t strftime(char *restrict s, size_t maxsize,
- const char *restrict format, const struct tm *restrict timeptr);
-char *ctime_r(const time_t *clock, char *buf);
-#endif
+#include <sys/time.h>
diff --git a/userland/libc/malloc/malloc.c b/userland/libc/malloc/malloc.c
index e368edd..bb7361d 100644
--- a/userland/libc/malloc/malloc.c
+++ b/userland/libc/malloc/malloc.c
@@ -1,4 +1,5 @@
#include <assert.h>
+#include <fcntl.h>
#include <math.h>
#include <stdarg.h>
#include <stddef.h>
@@ -8,7 +9,7 @@
#include <sys/random.h>
#include <typedefs.h>
#include <unistd.h>
-#define NEW_ALLOC_SIZE 0x5000
+#define NEW_ALLOC_SIZE 0x20000
#define IS_FREE (1 << 0)
#define IS_FINAL (1 << 1)
@@ -37,6 +38,12 @@ int debug_vprintf(const char *fmt, va_list ap) {
return -1;
}
write(1, buffer, rc);
+
+ static int serial_fd = -1;
+ if (-1 == serial_fd) {
+ serial_fd = open("/dev/serial", O_RDWR);
+ }
+ write(serial_fd, buffer, rc);
return rc;
}
@@ -259,6 +266,8 @@ void free(void *p) {
assert(h->magic == 0xdde51ab9410268b1);
assert(!(h->flags & IS_FREE));
+ randomfill(p, h->size);
+
h->flags |= IS_FREE;
merge_headers(h);
}
diff --git a/userland/libc/stdio/fclose.c b/userland/libc/stdio/fclose.c
index 01bdb22..eb92387 100644
--- a/userland/libc/stdio/fclose.c
+++ b/userland/libc/stdio/fclose.c
@@ -4,8 +4,12 @@
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html
// FIXME: Do some actual error checking.
int fclose(FILE *stream) {
- if (stream)
+ if (stream) {
+ if (stream->fflush) {
+ stream->fflush(stream);
+ }
free(stream->cookie);
+ }
free(stream);
return 0;
}
diff --git a/userland/libc/stdio/fopen.c b/userland/libc/stdio/fopen.c
index 71d14db..52e78d1 100644
--- a/userland/libc/stdio/fopen.c
+++ b/userland/libc/stdio/fopen.c
@@ -56,5 +56,6 @@ FILE *fopen(const char *pathname, const char *mode) {
r->fd = fd;
r->read_buffer = NULL;
r->read_buffer_stored = 0;
+ r->fflush = fflush_fd;
return r;
}
diff --git a/userland/libc/stdio/fwrite.c b/userland/libc/stdio/fwrite.c
index 6065465..6499529 100644
--- a/userland/libc/stdio/fwrite.c
+++ b/userland/libc/stdio/fwrite.c
@@ -8,5 +8,6 @@ size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
// On success, fwrite() return the number of items
// written.
rc /= size;
+ fflush(stream);
return rc;
}
diff --git a/userland/libc/stdio/vdprintf.c b/userland/libc/stdio/vdprintf.c
index 2724c2e..01727c4 100644
--- a/userland/libc/stdio/vdprintf.c
+++ b/userland/libc/stdio/vdprintf.c
@@ -1,9 +1,12 @@
#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-size_t min(size_t a, size_t b) { return (a < b) ? a : b; }
+size_t min(size_t a, size_t b) {
+ return (a < b) ? a : b;
+}
int vdprintf(int fd, const char *format, va_list ap) {
FILE f = {
@@ -11,6 +14,10 @@ int vdprintf(int fd, const char *format, va_list ap) {
.fd = fd,
.fflush = fflush_fd,
.write_buffer = NULL,
+ .read_buffer = NULL,
};
- return vfprintf(&f, format, ap);
+ int rc = vfprintf(&f, format, ap);
+ free(f.write_buffer);
+ free(f.read_buffer);
+ return rc;
}
diff --git a/userland/libc/stdlib/realpath.c b/userland/libc/stdlib/realpath.c
index d9e2387..7ce0f6c 100644
--- a/userland/libc/stdlib/realpath.c
+++ b/userland/libc/stdlib/realpath.c
@@ -16,7 +16,7 @@ char *realpath(const char *filename, char *resolvedname) {
strcat(cwd, filename); // FIXME: bounds check
struct sb string;
- sb_init(&string);
+ sb_init(&string, 512);
struct sv path = C_TO_SV(cwd);
diff --git a/userland/libc/string/memcpy.c b/userland/libc/string/memcpy.c
index 7c86b3a..7a9f01e 100644
--- a/userland/libc/string/memcpy.c
+++ b/userland/libc/string/memcpy.c
@@ -12,7 +12,6 @@ void *memcpy(void *dest, const void *src, uint32_t n) {
for (; n >= 2; n -= 2, d += 2, s += 2)
*(uint16_t *)d = *(uint16_t *)s;
-
for (; n; n--)
*d++ = *s++;
return dest;
diff --git a/userland/libc/tb/sb.c b/userland/libc/tb/sb.c
index c3fbc64..f4e10f8 100644
--- a/userland/libc/tb/sb.c
+++ b/userland/libc/tb/sb.c
@@ -1,12 +1,12 @@
-#include <tb/sb.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
+#include <tb/sb.h>
-void sb_init(struct sb *ctx) {
- ctx->string = malloc(512);
+void sb_init(struct sb *ctx, size_t starting_capacity) {
+ ctx->string = malloc(starting_capacity);
ctx->length = 0;
- ctx->capacity = 512;
+ ctx->capacity = starting_capacity;
}
void sb_free(struct sb *ctx) {
@@ -59,11 +59,15 @@ void sb_prepend_sv(struct sb *ctx, struct sv sv) {
ctx->length += sv.length;
}
-void sb_append_sv(struct sb *ctx, struct sv sv) {
- if (sv.length > ctx->capacity - ctx->length) {
- ctx->capacity += sv.length;
+void sb_append_buffer(struct sb *ctx, const char *buffer, size_t length) {
+ if (length > ctx->capacity - ctx->length) {
+ ctx->capacity += length;
ctx->string = realloc(ctx->string, ctx->capacity);
}
- memcpy(ctx->string + ctx->length, sv.s, sv.length);
- ctx->length += sv.length;
+ memcpy(ctx->string + ctx->length, buffer, length);
+ ctx->length += length;
+}
+
+void sb_append_sv(struct sb *ctx, struct sv sv) {
+ sb_append_buffer(ctx, sv.s, sv.length);
}
diff --git a/userland/libc/tb/sv.c b/userland/libc/tb/sv.c
index ac1c1e5..3f947ef 100644
--- a/userland/libc/tb/sv.c
+++ b/userland/libc/tb/sv.c
@@ -155,8 +155,25 @@ struct sv sv_take(struct sv s, struct sv *rest, size_t n) {
return s;
}
s.length = n;
- rest->length -= n;
- rest->s += n;
+ if (rest) {
+ rest->length -= n;
+ rest->s += n;
+ }
+ return s;
+}
+
+struct sv sv_take_end(struct sv s, struct sv *rest, size_t n) {
+ if (s.length < n) {
+ if (rest) {
+ rest->length = 0;
+ }
+ return s;
+ }
+ if (rest) {
+ rest->length = s.length - n;
+ }
+ s.s += (s.length - n);
+ s.length = n;
return s;
}