From a89837dd81c502bed07a6ee6a6390c672bb5b929 Mon Sep 17 00:00:00 2001 From: turbocat Date: Tue, 22 Feb 2022 22:16:42 +0000 Subject: [PATCH] libc.obj: - Added pipe example; - Fixed Makefile. git-svn-id: svn://kolibrios.org@9725 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../ktcc/trunk/libc.obj/include/sys/ksys.h | 40 +++++++++++++++ .../ktcc/trunk/libc.obj/samples/Makefile | 3 +- .../ktcc/trunk/libc.obj/samples/build_all.sh | 5 +- .../ktcc/trunk/libc.obj/samples/pipe.c | 51 +++++++++++++++++++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 programs/develop/ktcc/trunk/libc.obj/samples/pipe.c diff --git a/programs/develop/ktcc/trunk/libc.obj/include/sys/ksys.h b/programs/develop/ktcc/trunk/libc.obj/include/sys/ksys.h index 7386da8d17..529d319a4f 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/sys/ksys.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/sys/ksys.h @@ -1435,6 +1435,46 @@ int _ksys_file_rename(const char *name, const char *new_name) return _ksys_work_files(&k); } +/*============= Function 77 - implements the POSIX subsystem. =============*/ + +static inline +int _ksys_posix_read(int pipefd, void* buff, int n) +{ + int count; + asm_inline( + "int $0x40" + :"=a"(count) + :"a"(77), "b"(10),"c"(pipefd), "d"(buff), "S"(n) + :"memory" + ); + return count; +} + +static inline +int _ksys_posix_write(int pipefd, void* buff, int n) +{ + int count; + asm_inline( + "int $0x40" + :"=a"(count) + :"a"(77), "b"(11),"c"(pipefd), "d"(buff), "S"(n) + :"memory" + ); + return count; +} + +static inline +int _ksys_posix_pipe2(int pipefd[2], int flags) +{ + int err; + asm_inline( + "int $0x40" + :"=a"(err) + :"a"(77), "b"(13),"c"(pipefd), "d"(flags) + :"memory" + ); + return err; +} /* ######### Old names of functions and structures. Do not use again! ##########*/ diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/Makefile b/programs/develop/ktcc/trunk/libc.obj/samples/Makefile index a7a06a520d..0e7d2605cb 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/Makefile +++ b/programs/develop/ktcc/trunk/libc.obj/samples/Makefile @@ -2,7 +2,7 @@ KTCC = kos32-tcc FASM = fasm KPACK = kpack -CFLAGS = -I../include -B../../kx -I../../../../../../contrib/sdk/sources/SDL-1.2.2_newlib/include +CFLAGS = -I../include -B../../bin -I../../../../../../contrib/sdk/sources/SDL-1.2.2_newlib/include LIBS = -lbox_lib -lshell -lSDL -lsound -lnetwork -lrasterworks -limg -ldialog -lmsgbox BIN = stdio_test.kex \ @@ -24,6 +24,7 @@ BIN = stdio_test.kex \ sdltest.kex \ shell_test.kex \ libc_test.kex \ + pipe.kex \ defgen.kex all: $(BIN) diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh b/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh index 711e09a6fc..70eb6315d0 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh +++ b/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh @@ -19,7 +19,8 @@ cp clayer/logo.png /tmp0/1/tcc_samples/logo.png ../tcc thread_work.c -o /tmp0/1/tcc_samples/thread_work ../tcc -I../include/SDL sdltest.c -o /tmp0/1/tcc_samples/sdltest -lSDL -lsound ../tcc shell_test.c -o /tmp0/1/tcc_samples/shell_test -lshell -../tcc libc_test.c -o /tmp0/1/tcc_samples/libc_test -../tcc defgen.c -o /tmp0/1/tcc_samples/defgen +../tcc libc_test.c -o /tmp0/1/tcc_samples/libc_test +../tcc defgen.c -o /tmp0/1/tcc_samples/defgen +../tcc pipe.c -o /tmp0/1/tcc_samples/pipe "/sys/File managers/Eolite" /tmp0/1/tcc_samples exit diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/pipe.c b/programs/develop/ktcc/trunk/libc.obj/samples/pipe.c new file mode 100644 index 0000000000..705ea2ce68 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc.obj/samples/pipe.c @@ -0,0 +1,51 @@ +/* + * This is an example program for sending a message through a "pipe". + * Created by turbocat (Maxim Logaev) 2022. +*/ + +#include +#include +#include +#include +#include + +#define TH_STACK_SIZE 1024 +#define MESSAGE_SIZE 12 + +ksys_colors_table_t sys_colors; +int pipefd[2]; +char *send_message = "HELLO PIPE!"; + +void tmain() { + char recv_message[MESSAGE_SIZE]; + _ksys_posix_read(pipefd[0], recv_message, MESSAGE_SIZE); + printf("RECV: %s\n", recv_message); + assert(!strcmp(recv_message, send_message)); + puts("Successful pipe test"); + exit(0); +} + +void create_thread(void){ + unsigned tid; // New thread ID + void *th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack + if (!th_stack) { + puts("Memory allocation error for thread!"); + return; + } + tid = _ksys_create_thread(tmain, th_stack+TH_STACK_SIZE); // Create new thread with entry "main" + if (tid == -1) { + puts("Unable to create a new thread!"); + return; + } + printf("New thread created (TID=%u)\n", tid); +} + +void main() { + if (_ksys_posix_pipe2(pipefd, 0)) { + puts("Pipe creation error!"); + return; + } + printf("SEND: %s\n", send_message); + _ksys_posix_write(pipefd[1], send_message, MESSAGE_SIZE); + create_thread(); +}