From 2b01cab2df7273dad7f95c4349929a28331338c2 Mon Sep 17 00:00:00 2001 From: Mosder Date: Tue, 13 Oct 2026 14:48:48 +0200 Subject: refactor: removed the use of mkstemp and replaced it with tmpfile + fileno -> have tp handle that somehow, maybe freopen or something? --- Makefile | 2 +- compat/compat.h | 6 +++--- include/moslib/safe.h | 3 --- src/safe.c | 10 ---------- src/tester.c | 32 ++++++++++++++------------------ tests/safe_tests.c | 11 ----------- 6 files changed, 18 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index 246d3f9..ae8a59e 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ endef PROBED:= \ $(call probe_fun,string.h,memmem) \ $(call probe_fun,string.h,strdup) \ - $(call probe_fun,stdlib.h,mkstemp) + $(call probe_fun,stdio.h,fileno) CFLAGS+=$(PROBED) TESTS_CFLAGS+=$(PROBED) diff --git a/compat/compat.h b/compat/compat.h index 0279eda..314ffcf 100644 --- a/compat/compat.h +++ b/compat/compat.h @@ -27,10 +27,10 @@ extern char *mos_strdup(const char *s); extern void *mos_memmem(const void *h, size_t h_size, const void *n, size_t n_size); #endif // NO_MEMMEM -#ifndef NO_MKSTEMP -#define mos_mkstemp mkstemp +#ifndef NO_FILENO +#define mos_fileno fileno #else -#error mkstemp fallback not implemented +#error fileno fallback not implemented #endif // NO_MEMMEM #endif // MOSLIB_COMPAT_H diff --git a/include/moslib/safe.h b/include/moslib/safe.h index 1fe9c56..a606f17 100644 --- a/include/moslib/safe.h +++ b/include/moslib/safe.h @@ -21,7 +21,6 @@ #define safe_dup2 mos_safe_dup2 #define safe_fork mos_safe_fork #define safe_pipe mos_safe_pipe -#define safe_mkstemp mos_safe_mkstemp #endif // MOS_FORCE_PREFIXES @@ -44,7 +43,6 @@ typedef struct { #define mos_safe_dup2(fd, fd2) mos_safe_dup2_fn(fd, fd2, mos_err_info) #define mos_safe_fork() mos_safe_fork_fn(mos_err_info) #define mos_safe_pipe(fd) mos_safe_pipe_fn(fd, mos_err_info) -#define mos_safe_mkstemp(path) mos_safe_mkstemp_fn(path, mos_err_info) // Function prototypes for macros extern void *mos_safe_malloc_fn(size_t size, MosSafeErrInfo info); @@ -56,6 +54,5 @@ extern int mos_safe_dup_fn(int fd, MosSafeErrInfo info); extern int mos_safe_dup2_fn(int fd, int fd2, MosSafeErrInfo info); extern pid_t mos_safe_fork_fn(MosSafeErrInfo info); extern int mos_safe_pipe_fn(int fd[2], MosSafeErrInfo info); -extern int mos_safe_mkstemp_fn(char *path, MosSafeErrInfo info); #endif // MOSLIB_SAFE_H diff --git a/src/safe.c b/src/safe.c index de49a58..fd7a1af 100644 --- a/src/safe.c +++ b/src/safe.c @@ -1,5 +1,3 @@ -#include "compat.h" - #define MOS_FORCE_PREFIXES #include "moslib/safe.h" @@ -90,11 +88,3 @@ int mos_safe_pipe_fn(int fd[2], MosSafeErrInfo info) { ERR_MESS("failed to pipe"); exit(EXIT_FAILURE); } - -int mos_safe_mkstemp_fn(char *path, MosSafeErrInfo info) { - int fd = mos_mkstemp(path); - if (fd != -1) - return fd; - ERR_MESS("failed to make temporary file"); - exit(EXIT_FAILURE); -} diff --git a/src/tester.c b/src/tester.c index becc659..b6f6fa8 100644 --- a/src/tester.c +++ b/src/tester.c @@ -1,3 +1,5 @@ +#include "compat.h" + #define MOS_FORCE_PREFIXES #include "moslib/tester.h" @@ -21,7 +23,7 @@ #define RENDER_WIDTH 80 static int failed = 0; -static int dev_null = -1; +static FILE *dev_null = NULL; typedef struct { int *outs; @@ -152,20 +154,17 @@ void mos_test_assert_out_fn( size_t n = strlen(output); char *out = mos_safe_calloc(n + 2, 1); - char f[128]; - strcpy(f, ".tmpXXXXXX"); - int tmp = mos_safe_mkstemp(f); - unlink(f); + FILE *tmp = mos_safe_tmpfile(); int og = mos_safe_dup(fd); fflush(NULL); - mos_safe_dup2(tmp, fd); + mos_safe_dup2(fileno(tmp), fd); function(); fflush(NULL); lseek(fd, 0, SEEK_SET); read(fd, out, n + 1); - close(tmp); + fclose(tmp); mos_safe_dup2(og, fd); close(og); @@ -190,23 +189,20 @@ void mos_suppress_output_fn(int fd) { suppressed.outs[suppressed.n_outs] = fd; suppressed.ogs[suppressed.n_outs++] = mos_safe_dup(fd); - if (dev_null == -1) { - char f[128]; - strcpy(f, ".dev_nullXXXXXX"); - dev_null = mos_safe_mkstemp(f); - unlink(f); + if (dev_null == NULL) { + dev_null = mos_safe_tmpfile(); } fflush(NULL); - mos_safe_dup2(dev_null, fd); + mos_safe_dup2(fileno(dev_null), fd); } void mos_unsuppress_outputs(void) { if (!suppressed.outs || !suppressed.ogs) return; - if (dev_null != -1) - fflush(NULL); + if (dev_null != NULL) + fflush(dev_null); for (size_t i = suppressed.n_outs; i-- > 0;) { mos_safe_dup2(suppressed.ogs[i], suppressed.outs[i]); @@ -291,9 +287,9 @@ void mos_free_tester(MosTester *tester) { free(tester); } mos_unsuppress_outputs(); - if (dev_null != -1) { - close(dev_null); - dev_null = -1; + if (dev_null != NULL) { + fclose(dev_null); + dev_null = NULL; } if (suppressed.outs) { free(suppressed.outs); diff --git a/tests/safe_tests.c b/tests/safe_tests.c index 25cd5f6..47b927d 100644 --- a/tests/safe_tests.c +++ b/tests/safe_tests.c @@ -117,18 +117,8 @@ TEST(test_safe_pipe) { close(fd[1]); } -TEST(test_safe_mkstemp) { - char f[128]; - strcpy(f, ".tmpXXXXXX"); - int fd = safe_mkstemp(f); - test_assert(fd != -1, "Failed to create a temporary file"); - unlink(f); - close(fd); -} - #define mos_safe_fork() mos_safe_fork_fn(mos_err_info) #define mos_safe_pipe(fd) mos_safe_pipe_fn(fd, mos_err_info) -#define mos_safe_mkstemp(path) mos_safe_mkstemp_fn(path, mos_err_info) void safe_tests(Tester *tester) { TestGroup *group = add_test_group(tester, "moslib/safe.h"); @@ -141,5 +131,4 @@ void safe_tests(Tester *tester) { add_test(group, test_safe_dup2); add_test(group, test_safe_fork); add_test(group, test_safe_pipe); - add_test(group, test_safe_mkstemp); } -- cgit v1.2.3