diff options
| author | Mosder <xmosder12@gmail.com> | 2026-08-21 14:41:26 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-08-21 14:41:26 +0200 |
| commit | 06954da1e847072f9e95df0f183d9327a0f05b5b (patch) | |
| tree | bd10a6e5e960dc73d11de0b91ae73d8b6fb1a1a0 /src/safe.c | |
| parent | b6b7a01987dca604a161c8c73a240ec4430baed0 (diff) | |
feat: added some for safe wrappers + added ability to capture and test whether output is what was expected
Diffstat (limited to 'src/safe.c')
| -rw-r--r-- | src/safe.c | 34 |
1 files changed, 33 insertions, 1 deletions
@@ -6,7 +6,7 @@ #include <stdlib.h> #include <unistd.h> -#define ERR_MESS(msg, ...) err_mess(info, msg, __VA_ARGS__) +#define ERR_MESS(...) err_mess(info, __VA_ARGS__) void err_mess(MosSafeErrInfo info, const char *msg, ...) { va_list args; @@ -40,3 +40,35 @@ void *mos_safe_realloc_fn(void *ptr, size_t size, MosSafeErrInfo info) { ERR_MESS("failed to realloc %p to %zu bytes", ptr, size); exit(EXIT_FAILURE); } + +FILE *mos_safe_fopen_fn(const char *path, const char *mode, MosSafeErrInfo info) { + FILE *p = fopen(path, mode); + if (p) + return p; + ERR_MESS("failed to fopen %s with mode %s", path, mode); + exit(EXIT_FAILURE); +} + +FILE *mos_safe_tmpfile_fn(MosSafeErrInfo info) { + FILE *p = tmpfile(); + if (p) + return p; + ERR_MESS("failed to create a tmpfile"); + exit(EXIT_FAILURE); +} + +int mos_safe_dup_fn(int fd, MosSafeErrInfo info) { + int res = dup(fd); + if (res != -1) + return res; + ERR_MESS("failed to dup %d", fd); + exit(EXIT_FAILURE); +} + +int mos_safe_dup2_fn(int fd, int fd2, MosSafeErrInfo info) { + int res = dup2(fd, fd2); + if (res != -1) + return res; + ERR_MESS("failed to dup %d 2 %d", fd, fd2); + exit(EXIT_FAILURE); +} |
