Compare commits

..

2 Commits

Author SHA1 Message Date
Randy-Jordan c6342f739a More try implementation progress. 2026-06-27 18:58:20 -05:00
Randy-Jordan 7a4ad7162a try implementation progress 2026-06-26 16:30:34 -05:00
+585 -62
View File
@@ -29,104 +29,383 @@ a robust way to handle errors.
#include "../include/platform/error/ci2_exception.h" // Exceptions #include "../include/platform/error/ci2_exception.h" // Exceptions
#include "../include/platform/try/ci2_try.h" #include "../include/platform/try/ci2_try.h"
#include <errno.h>
#include <string.h>
/* fcntl.h */ /* fcntl.h */
int int
ci2_try_creat(const char* path, mode_t mode); ci2_try_creat(const char* path, mode_t mode)
{
int res = creat(path, mode);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fcntl(int fd, int cmd, long args); ci2_try_fcntl(int fd, int cmd, long args);
int int
ci2_try_open(const char* pathname, int flags, mode_t mode); ci2_try_open(const char* path, int flags, mode_t mode)
{
int res = open(path, flags, mode);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
/* socket.h */ /* socket.h */
int int
ci2_try_accept(int, struct sockaddr* restrict, socklen_t* restrict); ci2_try_accept(int sockfd,
struct sockaddr* restrict addr,
socklen_t* restrict addrlen)
{
int res = accept(sockfd, addr, addrlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_bind(int, const struct sockaddr*, socklen_t); ci2_try_bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen)
{
int res = bind(sockfd, addr, addrlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_connect(int, const struct sockaddr*, socklen_t); ci2_try_connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen)
{
int res = connect(sockfd, addr, addrlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_getpeername(int, struct sockaddr* restrict, socklen_t* restrict); ci2_try_getpeername(int sockfd,
struct sockaddr* restrict addr,
socklen_t* restrict addrlen)
{
int res = getpeername(sockfd, addr, addrlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_getsockname(int, struct sockaddr* restrict, socklen_t* restrict); ci2_try_getsockname(int sockfd,
struct sockaddr* restrict addr,
socklen_t* restrict addrlen)
{
int res = getsockname(sockfd, addr, addrlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_getsockopt(int, int, int, void* restrict, socklen_t* restrict); ci2_try_getsockopt(int sockfd,
int level,
int optname,
void* restrict optval,
socklen_t* restrict optlen)
{
int res = getsockopt(sockfd, level, optname, optval, optlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_listen(int, int); ci2_try_listen(int sockfd, int backlog)
{
int res = listen(sockfd, backlog);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_recv(int, void*, size_t, int); ci2_try_recv(int sockfd, void* buf, size_t len, int flags)
{
ssize_t res = recv(sockfd, buf, len, flags);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_recvfrom(int, ci2_try_recvfrom(int sockfd,
void* restrict, void* restrict buf,
size_t, size_t len,
int, int flags,
struct sockaddr* restrict, struct sockaddr* restrict addr,
socklen_t* restrict); socklen_t* restrict addrlen)
{
ssize_t res = recvfrom(sockfd, buf, len, flags, addr, addrlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_recvmsg(int, struct msghdr*, int); ci2_try_recvmsg(int sockfd, struct msghdr* msg, int flags)
{
ssize_t res = recvmsg(sockfd, msg, flags);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_send(int, const void*, size_t, int); ci2_try_send(int sockfd, const void* buf, size_t len, int flags)
{
ssize_t res = send(sockfd, buf, len, flags);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_sendmsg(int, const struct msghdr*, int); ci2_try_sendmsg(int sockfd, const struct msghdr* msg, int flags)
{
ssize_t res = sendmsg(sockfd, msg, flags);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_sendto(int, ci2_try_sendto(int sockfd,
const void*, const void* buf,
size_t, size_t len,
int, int flags,
const struct sockaddr*, const struct sockaddr* addr,
socklen_t); socklen_t addrlen)
{
ssize_t res = sendto(sockfd, buf, len, flags, addr, addrlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_setsockopt(int, int, int, const void*, socklen_t); ci2_try_setsockopt(int sockfd,
int level,
int optname,
const void* optval,
socklen_t optlen)
{
int res = setsockopt(sockfd, level, optname, optval, optlen);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_shutdown(int, int); ci2_try_shutdown(int fd, int how)
{
int res = shutdown(fd, how);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_sockatmark(int); ci2_try_sockatmark(int sockfd)
{
int res = sockatmark(sockfd);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_socket(int, int, int); ci2_try_socket(int domain, int type, int protocol)
{
int res = socket(domain, type, protocol);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_socketpair(int, int, int, int[2]); ci2_try_socketpair(int domain, int type, int protocol, int sv[2])
{
int res = socketpair(domain, type, protocol, sv);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
/* sys/stat.h */ /* sys/stat.h */
int int
ci2_try_chmod(const char*, mode_t); ci2_try_chmod(const char* path, mode_t mode)
{
int res = chmod(path, mode);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fchmod(int, mode_t); ci2_try_fchmod(int fd, mode_t mode)
{
int res = fchmod(fd, mode);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fchmodat(int, const char*, mode_t, int); ci2_try_fchmodat(int dirfd, const char* path, mode_t mode, int flags)
{
int res = fchmodat(dirfd, path, mode, flags);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fstat(int, struct stat*); ci2_try_fstat(int fd, struct stat* buf)
{
int res = fstat(fd, buf);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fstatat(int, const char* restrict, struct stat* restrict, int); ci2_try_fstatat(int fd,
const char* restrict path,
struct stat* restrict buf,
int flag)
{
int res = fstatat(fd, path, buf, flag);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_futimens(int, const struct timespec[2]); ci2_try_futimens(int, const struct timespec[2]);
int int
ci2_try_lstat(const char* restrict, struct stat* restrict); ci2_try_lstat(const char* restrict path, struct stat* restrict buf)
{
int res = lstat(path, buf);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_mkdir(const char*, mode_t); ci2_try_mkdir(const char*, mode_t);
@@ -135,10 +414,30 @@ int
ci2_try_mkdirat(int, const char*, mode_t); ci2_try_mkdirat(int, const char*, mode_t);
int int
ci2_try_mkfifo(const char*, mode_t); ci2_try_mkfifo(const char* path, mode_t mode)
{
int res = mkfifo(path, mode);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_mkfifoat(int, const char*, mode_t); ci2_try_mkfifoat(int dirfd, const char* path, mode_t mode)
{
int res = mkfifoat(dirfd, path, mode);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_mknod(const char*, mode_t, dev_t); ci2_try_mknod(const char*, mode_t, dev_t);
@@ -168,7 +467,17 @@ int
ci2_try_chown(const char*, uid_t, gid_t); ci2_try_chown(const char*, uid_t, gid_t);
int int
ci2_try_close(int); ci2_try_close(int fd)
{
int res = close(fd);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
size_t size_t
ci2_try_confstr(int, char*, size_t); ci2_try_confstr(int, char*, size_t);
@@ -177,10 +486,30 @@ char*
ci2_try_crypt(const char*, const char*); ci2_try_crypt(const char*, const char*);
int int
ci2_try_dup(int); ci2_try_dup(int oldfd)
{
int res = dup(oldfd);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_dup2(int, int); ci2_try_dup2(int oldfd, int newfd)
{
int res = dup2(oldfd, newfd);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
void void
ci2_try_exit(int); ci2_try_exit(int);
@@ -198,13 +527,43 @@ int
ci2_try_execlp(const char*, const char*, ...); ci2_try_execlp(const char*, const char*, ...);
int int
ci2_try_execv(const char*, char* const[]); ci2_try_execv(const char* path, char* const argv[])
{
int res = execv(path, argv);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_execve(const char*, char* const[], char* const[]); ci2_try_execve(const char* path, char* const argv[], char* const envp[])
{
int res = execve(path, argv, envp);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_execvp(const char*, char* const[]); ci2_try_execvp(const char* file, char* const argv[])
{
int res = execvp(file, argv);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_faccessat(int, const char*, int, int); ci2_try_faccessat(int, const char*, int, int);
@@ -225,7 +584,17 @@ int
ci2_try_fexecve(int, char* const[], char* const[]); ci2_try_fexecve(int, char* const[], char* const[]);
pid_t pid_t
ci2_try_fork(void); ci2_try_fork(void)
{
pid_t res = fork();
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
long long
ci2_try_fpathconf(int, int); ci2_try_fpathconf(int, int);
@@ -310,16 +679,56 @@ int
ci2_try_pause(void); ci2_try_pause(void);
int int
ci2_try_pipe(int[2]); ci2_try_pipe(int fds[2])
{
int res = pipe(fds);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_pread(int, void*, size_t, off_t); ci2_try_pread(int fd, void* buf, size_t len, off_t off)
{
ssize_t res = pread(fd, buf, len, off);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_pwrite(int, const void*, size_t, off_t); ci2_try_pwrite(int fd, const void* buf, size_t len, off_t off)
{
ssize_t res = pwrite(fd, buf, len, off);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_read(int, void*, size_t); ci2_try_read(int fd, void* buf, size_t len)
{
ssize_t res = read(fd, buf, len);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
ssize_t ssize_t
ci2_try_readlink(const char* restrict, char* restrict, size_t); ci2_try_readlink(const char* restrict, char* restrict, size_t);
@@ -390,11 +799,24 @@ int
ci2_try_unlinkat(int, const char*, int); ci2_try_unlinkat(int, const char*, int);
ssize_t ssize_t
ci2_try_write(int, const void*, size_t); ci2_try_write(int fd, const void* buf, size_t len)
{
ssize_t res = write(fd, buf, len);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
/* stdio.h */ /* stdio.h */
void void
ci2_try_clearerr(FILE*); ci2_try_clearerr(FILE* fp)
{ // shouldn't fail.
clearerr(fp);
}
char* char*
ci2_try_ctermid(char*); ci2_try_ctermid(char*);
@@ -403,19 +825,55 @@ int
ci2_try_dprintf(int, const char* restrict, ...); ci2_try_dprintf(int, const char* restrict, ...);
int int
ci2_try_fclose(FILE*); ci2_try_fclose(FILE* fp)
{
int res = fclose(fp);
if (res != 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
FILE* FILE*
ci2_try_fdopen(int, const char*); ci2_try_fdopen(int fd, const char* mode)
{
FILE* res = fdopen(fd, mode);
if (res == NULL) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_feof(FILE*); ci2_try_feof(FILE* fp)
{ // shouldn't fail
return feof(fp);
}
int int
ci2_try_ferror(FILE*); ci2_try_ferror(FILE* fp)
{ // shouldn't fail
return ferror(fp);
}
int int
ci2_try_fflush(FILE*); ci2_try_fflush(FILE* fp)
{
int res = fflush(fp);
if (res != 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fgetc(FILE*); ci2_try_fgetc(FILE*);
@@ -436,22 +894,74 @@ FILE*
ci2_try_fmemopen(void* restrict, size_t, const char* restrict); ci2_try_fmemopen(void* restrict, size_t, const char* restrict);
FILE* FILE*
ci2_try_fopen(const char* restrict, const char* restrict); ci2_try_fopen(const char* restrict path, const char* restrict mode)
{
FILE* res = fopen(path, mode);
if (res == NULL) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fprintf(FILE* restrict, const char* restrict, ...); ci2_try_fprintf(FILE* restrict, const char* restrict, ...);
int int
ci2_try_fputc(int, FILE*); ci2_try_fputc(int c, FILE* fp)
{
int res = fputc(c, fp);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fputs(const char* restrict, FILE* restrict); ci2_try_fputs(const char* restrict s, FILE* restrict fp)
{
int res = fputs(s, fp);
if (res < 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
size_t size_t
ci2_try_fread(void* restrict, size_t, size_t, FILE* restrict); ci2_try_fread(void* restrict buf, size_t size, size_t count, FILE* restrict fp)
{
size_t res = fread(buf, size, count, fp);
if (res == 0 && ferror(fp)) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
FILE* FILE*
ci2_try_freopen(const char* restrict, const char* restrict, FILE* restrict); ci2_try_freopen(const char* restrict path,
const char* restrict mode,
FILE* restrict stream)
{
FILE* res = freopen(path, mode, stream);
if (res == NULL) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_fscanf(FILE* restrict, const char* restrict, ...); ci2_try_fscanf(FILE* restrict, const char* restrict, ...);
@@ -478,7 +988,20 @@ void
ci2_try_funlockfile(FILE*); ci2_try_funlockfile(FILE*);
size_t size_t
ci2_try_fwrite(const void* restrict, size_t, size_t, FILE* restrict); ci2_try_fwrite(const void* restrict buf,
size_t size,
size_t count,
FILE* restrict fp)
{
size_t res = fwrite(buf, size, count, fp);
if (res == 0) {
int err = errno;
char* err_str = strerror(err);
struct Exception e = { err_str };
CI2_RAISE(e);
}
return res;
}
int int
ci2_try_getc(FILE*); ci2_try_getc(FILE*);