This reduces the complexity of fmkomstemp().
Signed-off-by: Alejandro Colomar <alx@kernel.org>
fputsx.c \
fs/mkstemp/fmkomstemp.c \
fs/mkstemp/fmkomstemp.h \
+ fs/mkstemp/mkomstemp.c \
+ fs/mkstemp/mkomstemp.h \
fs/readlink/areadlink.c \
fs/readlink/areadlink.h \
fs/readlink/readlinknul.c \
#include <config.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
+#include "fs/mkstemp/mkomstemp.h"
+
inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m);
+// FILE make with-open(2)-flags with-mode secure temporary
inline FILE *
fmkomstemp(char *template, unsigned int flags, mode_t m)
{
int fd;
FILE *fp;
- fd = mkostemp(template, flags);
+ fd = mkomstemp(template, flags, m);
if (fd == -1)
return NULL;
- if (fchmod(fd, m) == -1)
- goto fail;
-
fp = fdopen(fd, "w");
if (fp == NULL)
goto fail;
--- /dev/null
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "fs/mkstemp/mkomstemp.h"
+
+#include <sys/types.h>
+
+
+extern inline int mkomstemp(char *template, unsigned int flags, mode_t m);
--- /dev/null
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_
+#define SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_
+
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+
+inline int mkomstemp(char *template, unsigned int flags, mode_t m);
+
+
+// make with-open(2)-like-flags with-mode secure temporary
+inline int
+mkomstemp(char *template, unsigned int flags, mode_t m)
+{
+ int fd;
+
+ fd = mkostemp(template, flags);
+ if (fd == -1)
+ return -1;
+
+ if (fchmod(fd, m) == -1)
+ goto fail;
+
+ return fd;
+fail:
+ close(fd);
+ unlink(template);
+ return -1;
+}
+
+
+#endif // include guard