72 lines
2.1 KiB
C
72 lines
2.1 KiB
C
#include "../std.h"
|
|
#include "../str.h"
|
|
#include <assert.h>
|
|
#include <stdarg.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);
|
|
}
|
|
void com_std_assert(bool holds) { assert(holds); }
|
|
void com_std_printf(char const *fmt, ...) {
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
vprintf(fmt, args);
|
|
va_end(args);
|
|
}
|
|
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 *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, char *data, uint32_t bytes) {
|
|
com_std_assert(fd != NULL && 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, char *data, uint32_t upto) {
|
|
com_std_assert(fd != NULL && data != NULL && upto != 0);
|
|
return fread(data, 1, upto, fd);
|
|
}
|
|
|
|
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 *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); }
|