diff options
author | Anton Kling <anton@kling.gg> | 2024-06-26 01:41:28 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-06-26 01:41:28 +0200 |
commit | 33e1b11555d3557a36bd69d63f5bf0c290b5d462 (patch) | |
tree | 7690db8310fc26f70b3cb96c8d2241969303cfe7 | |
parent | 3bed68592da11f2d013f76534220275739dd7556 (diff) |
LibC: Fix printf formatting
Previously %02x would not print out padding if the given value was zero.
-rw-r--r-- | include/sys/socket.h | 2 | ||||
-rw-r--r-- | kernel/cpu/syscall.c | 61 | ||||
-rw-r--r-- | userland/libc/Makefile | 2 | ||||
-rw-r--r-- | userland/libc/include/syscall.h | 1 | ||||
-rw-r--r-- | userland/libc/stdio/vfprintf.c | 6 | ||||
-rw-r--r-- | userland/libc/sys/socket/getpeername.c | 7 | ||||
-rw-r--r-- | userland/test/test.c | 2 |
7 files changed, 54 insertions, 27 deletions
diff --git a/include/sys/socket.h b/include/sys/socket.h index f67b6a0..5af84fc 100644 --- a/include/sys/socket.h +++ b/include/sys/socket.h @@ -79,5 +79,7 @@ int listen(int socket, int backlog); int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len); +int getpeername(int sockfd, struct sockaddr *restrict addr, + socklen_t *restrict addrlen); #endif // KERNEL #endif // SYS_SOCKET_H diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c index 2e7bd61..b981d5b 100644 --- a/kernel/cpu/syscall.c +++ b/kernel/cpu/syscall.c @@ -13,6 +13,7 @@ #include <string.h> #include <syscalls.h> #include <typedefs.h> +#include <network/tcp.h> #pragma GCC diagnostic ignored "-Wpedantic" @@ -150,28 +151,46 @@ int syscall_setsockopt(int socket, int level, int option_name, return setsockopt(socket, level, option_name, option_value, option_len); } +int tcp_connect(vfs_fd_t *fd, const struct sockaddr *addr, socklen_t addrlen); +int syscall_getpeername(int sockfd, struct sockaddr *restrict addr, + socklen_t *restrict addrlen) { + if (addr) { + return -EINVAL; + } + + vfs_fd_t *fd = get_vfs_fd(sockfd, NULL); + // FIXME: BAD + assert(fd->inode->connect == tcp_connect); + struct TcpConnection *con = fd->inode->internal_object; + if (TCP_STATE_ESTABLISHED != con->state) { + return -ENOTCONN; + } + + return 0; +} + int (*syscall_functions[])() = { - (void(*))syscall_open, (void(*))syscall_mread, - (void(*))syscall_write, (void(*))syscall_pread, - (void(*))syscall_pwrite, (void(*))syscall_fork, - (void(*))syscall_exec, (void(*))syscall_getpid, - (void(*))syscall_exit, (void(*))syscall_wait, - (void(*))syscall_brk, (void(*))syscall_sbrk, - (void(*))syscall_pipe, (void(*))syscall_dup2, - (void(*))syscall_close, (void(*))syscall_openpty, - (void(*))syscall_poll, (void(*))syscall_mmap, - (void(*))syscall_accept, (void(*))syscall_bind, - (void(*))syscall_socket, (void(*))syscall_shm_open, - (void(*))syscall_ftruncate, (void(*))syscall_fstat, - (void(*))syscall_msleep, (void(*))syscall_uptime, - (void(*))syscall_mkdir, (void(*))syscall_recvfrom, - (void(*))syscall_sendto, (void(*))syscall_kill, - (void(*))syscall_sigaction, (void(*))syscall_chdir, - (void(*))syscall_getcwd, (void(*))syscall_isatty, - (void(*))syscall_randomfill, - (void(*))syscall_munmap, (void(*))syscall_open_process, - (void(*))syscall_lseek, (void(*))syscall_connect, - (void(*))syscall_setsockopt, + (void(*))syscall_open, (void(*))syscall_mread, + (void(*))syscall_write, (void(*))syscall_pread, + (void(*))syscall_pwrite, (void(*))syscall_fork, + (void(*))syscall_exec, (void(*))syscall_getpid, + (void(*))syscall_exit, (void(*))syscall_wait, + (void(*))syscall_brk, (void(*))syscall_sbrk, + (void(*))syscall_pipe, (void(*))syscall_dup2, + (void(*))syscall_close, (void(*))syscall_openpty, + (void(*))syscall_poll, (void(*))syscall_mmap, + (void(*))syscall_accept, (void(*))syscall_bind, + (void(*))syscall_socket, (void(*))syscall_shm_open, + (void(*))syscall_ftruncate, (void(*))syscall_fstat, + (void(*))syscall_msleep, (void(*))syscall_uptime, + (void(*))syscall_mkdir, (void(*))syscall_recvfrom, + (void(*))syscall_sendto, (void(*))syscall_kill, + (void(*))syscall_sigaction, (void(*))syscall_chdir, + (void(*))syscall_getcwd, (void(*))syscall_isatty, + (void(*))syscall_randomfill, (void(*))syscall_munmap, + (void(*))syscall_open_process, (void(*))syscall_lseek, + (void(*))syscall_connect, (void(*))syscall_setsockopt, + (void(*))syscall_getpeername, }; void int_syscall(reg_t *r); diff --git a/userland/libc/Makefile b/userland/libc/Makefile index 2be101e..8db5a16 100644 --- a/userland/libc/Makefile +++ b/userland/libc/Makefile @@ -2,7 +2,7 @@ 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 +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 all: libc.a %.o: %.c diff --git a/userland/libc/include/syscall.h b/userland/libc/include/syscall.h index 1f68b60..fee2e43 100644 --- a/userland/libc/include/syscall.h +++ b/userland/libc/include/syscall.h @@ -45,6 +45,7 @@ #define SYS_LSEEK 37 #define SYS_CONNECT 38 #define SYS_SETSOCKOPT 39 +#define SYS_GETPEERNAME 40 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/stdio/vfprintf.c b/userland/libc/stdio/vfprintf.c index 7006578..2ba3e2c 100644 --- a/userland/libc/stdio/vfprintf.c +++ b/userland/libc/stdio/vfprintf.c @@ -10,7 +10,7 @@ const char HEX_SET[0x10] = {'0', '1', '2', '3', '4', '5', '6', '7', #define FILE_WRITE(_f, _s, _l, _r) \ { \ size_t _rc = _f->write(_f, (const unsigned char *)_s, _l); \ - if ((size_t)-1 == _rc) \ + if ((size_t) - 1 == _rc) \ assert(0); \ *(int *)(_r) += _rc; \ } @@ -18,10 +18,6 @@ const char HEX_SET[0x10] = {'0', '1', '2', '3', '4', '5', '6', '7', int fprint_num(FILE *f, long long n, int base, char *char_set, int prefix, int zero_padding, int right_padding) { int c = 0; - if (0 == n) { - zero_padding = 1; - prefix = 1; - } char str[32]; int i = 0; diff --git a/userland/libc/sys/socket/getpeername.c b/userland/libc/sys/socket/getpeername.c new file mode 100644 index 0000000..25a56b1 --- /dev/null +++ b/userland/libc/sys/socket/getpeername.c @@ -0,0 +1,7 @@ +#include <sys/socket.h> +#include <syscall.h> + +int getpeername(int sockfd, struct sockaddr *restrict addr, + socklen_t *restrict addrlen) { + RC_ERRNO(syscall(SYS_GETPEERNAME, sockfd, addr, addrlen, 0, 0)); +} diff --git a/userland/test/test.c b/userland/test/test.c index 9013f1e..29aa34c 100644 --- a/userland/test/test.c +++ b/userland/test/test.c @@ -578,6 +578,8 @@ void printf_test(void) { EXP("hex: 1"); buf_n = sprintf(buf, "hex: %x", 0x622933f2); EXP("hex: 622933f2"); + buf_n = sprintf(buf, "hex: %02x", 0); + EXP("hex: 00"); buf_n = sprintf(buf, "oct: %02o", 1); EXP("oct: 01"); |