Files
brightstone/Common/Std/libc.c
T
2026-10-12 21:52:52 +03:00

77 lines
2.2 KiB
C

#include "../std.h"
#include "any.c"
#include <assert.h>
#include <poll.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <tgmath.h>
void *com_std_alloc(void *ptr, size_t size) {
return size != 0 ? realloc(ptr, size) : (free(ptr), NULL);
}
double com_std_sqrt(double v) { return sqrt(v); }
double com_std_fabs(double v) { return fabs(v); }
double com_std_sin(double v) { return sin(v); }
double com_std_cos(double v) { return cos(v); }
com_std_fd com_std_open(char const *path, uint32_t flags) {
com_std_assert(flags != 0);
char *mode = flags & COM_STD_OPEN_READ && flags & COM_STD_OPEN_WRITE ? "r+"
: flags & COM_STD_OPEN_READ ? "r"
: "w";
return fopen(path, mode);
}
bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes) {
com_std_assert(data != NULL && bytes != 0);
bool s = fwrite(data, 1, bytes, fd) == bytes;
fflush(fd);
return s;
}
int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto) {
com_std_assert(data != NULL && upto != 0);
return fread(data, 1, upto, fd);
}
bool com_std_has_data(com_std_fd fd) {
struct pollfd pfd = {.fd = (intptr_t)fd, .events = POLLIN};
int ret = poll(&pfd, 1, 0);
if (ret > 0) {
return pfd.revents & POLLIN;
} else
return false;
}
bool com_std_sfwd(com_std_fd fd, uint32_t bytes) {
com_std_assert(fd != NULL && bytes != 0);
return fseek(fd, bytes, SEEK_CUR) == 0;
}
com_std_fd com_std_socket(bool isnetwork) {
if (!isnetwork) {
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
return fdopen(sockfd, "w+b");
} else
return NULL;
}
bool com_std_connect(com_std_fd fd, bool isnetwork, char const *address) {
com_std_assert(address != NULL);
if (!isnetwork) {
struct sockaddr_un addr = {.sun_family = AF_UNIX};
__builtin_memcpy(&addr.sun_path, address, com_str_ext_len(address));
return connect(fileno(fd), (struct sockaddr *)&addr, sizeof(addr)) == 0;
} else
return NULL;
}
void com_std_exit(uint32_t status) { exit(status); }
void com_std_close(com_std_fd fd) { fclose(fd); }