Adding console I/O via the `SHELL`. Demonstration of [http_tcp_demo.c](https://git.kolibrios.org/KolibriOS/kolibrios/src/branch/main/programs/develop/ktcc/libc.obj/samples/http_tcp_demo.c): <video src="attachments/847d7d19-3d67-480d-80c8-6d678af61a43" title="Запись экрана от 2026-03-07 13-55-50.mp4" controls></video> - [x] output - [x] multithread - [x] add mutex - [x] multithread tests Reviewed-on: #354 Reviewed-by: Mikhail Frolov <mixa.frolov2003@gmail.com> Reviewed-by: Kiril Lipatov <lipatov.kiril@gmail.com> Co-authored-by: Egor00f <y.yarin@inbox.ru>
47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include "../sys/_conio.h"
|
|
#include <sys/ksys.h>
|
|
#include <errno.h>
|
|
#include <shell_api.h>
|
|
|
|
size_t fwrite(const void* restrict ptr, size_t size, size_t nmemb, FILE* restrict stream)
|
|
{
|
|
unsigned bytes_written = 0;
|
|
unsigned bytes_count = size * nmemb;
|
|
|
|
if (!stream) {
|
|
errno = EBADF;
|
|
return 0;
|
|
}
|
|
|
|
if (size <= 0 || nmemb <= 0) {
|
|
errno = EINVAL;
|
|
stream->error = errno;
|
|
return 0;
|
|
}
|
|
|
|
if (stream == stdout) {
|
|
console_write((char*)ptr, bytes_count);
|
|
return nmemb;
|
|
}
|
|
|
|
if (stream == stderr) {
|
|
for (size_t i = 0; i < bytes_count; i++) {
|
|
char c = *(char*)(ptr + i);
|
|
_ksys_debug_putc(c);
|
|
}
|
|
return nmemb;
|
|
}
|
|
|
|
if (stream->mode != _FILEMODE_R) {
|
|
unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written);
|
|
if (status != KSYS_FS_ERR_SUCCESS) {
|
|
errno = EIO;
|
|
stream->error = errno;
|
|
return 0;
|
|
}
|
|
stream->position += bytes_written;
|
|
}
|
|
return bytes_written / size;
|
|
}
|