From aa3d36764b2e566e3089c34b827aa454ea111b55 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 4 Dec 2024 13:33:58 +0100 Subject: [PATCH] lib/fs/mkstemp/, src/: fmkomstemp(): Move function to separate file And make it inline. Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 2 ++ lib/fs/mkstemp/fmkomstemp.c | 13 +++++++++++ lib/fs/mkstemp/fmkomstemp.h | 46 +++++++++++++++++++++++++++++++++++++ src/useradd.c | 28 +--------------------- 4 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 lib/fs/mkstemp/fmkomstemp.c create mode 100644 lib/fs/mkstemp/fmkomstemp.h diff --git a/lib/Makefile.am b/lib/Makefile.am index e76e7446a..84948f4a2 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -103,6 +103,8 @@ libshadow_la_SOURCES = \ find_new_sub_gids.c \ find_new_sub_uids.c \ fputsx.c \ + fs/mkstemp/fmkomstemp.c \ + fs/mkstemp/fmkomstemp.h \ fs/readlink/areadlink.c \ fs/readlink/areadlink.h \ fs/readlink/readlinknul.c \ diff --git a/lib/fs/mkstemp/fmkomstemp.c b/lib/fs/mkstemp/fmkomstemp.c new file mode 100644 index 000000000..a8b49d6e4 --- /dev/null +++ b/lib/fs/mkstemp/fmkomstemp.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "fs/mkstemp/fmkomstemp.h" + +#include +#include + + +extern inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m); diff --git a/lib/fs/mkstemp/fmkomstemp.h b/lib/fs/mkstemp/fmkomstemp.h new file mode 100644 index 000000000..4770bc92a --- /dev/null +++ b/lib/fs/mkstemp/fmkomstemp.h @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_FS_MKSTEMP_FMKOMSTEMP_H_ +#define SHADOW_INCLUDE_LIB_FS_MKSTEMP_FMKOMSTEMP_H_ + + +#include + +#include +#include +#include +#include +#include + + +inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m); + + +inline FILE * +fmkomstemp(char *template, unsigned int flags, mode_t m) +{ + int fd; + FILE *fp; + + fd = mkostemp(template, flags); + if (fd == -1) + return NULL; + + if (fchmod(fd, m) == -1) + goto fail; + + fp = fdopen(fd, "w"); + if (fp == NULL) + goto fail; + + return fp; +fail: + close(fd); + unlink(template); + return NULL; +} + + +#endif // include guard diff --git a/src/useradd.c b/src/useradd.c index 058b09d92..d8e5cb4be 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -42,6 +42,7 @@ #include "chkname.h" #include "defines.h" #include "faillog.h" +#include "fs/mkstemp/fmkomstemp.h" #include "getdef.h" #include "groupio.h" #include "nscd.h" @@ -243,8 +244,6 @@ static void create_home (void); static void create_mail (void); static void check_uid_range(int rflg, uid_t user_id); -static FILE *fmkomstemp(char *template, unsigned int flags, mode_t m); - /* * fail_exit - undo as much as possible @@ -2744,28 +2743,3 @@ int main (int argc, char **argv) return E_SUCCESS; } - - -static FILE * -fmkomstemp(char *template, unsigned int flags, mode_t m) -{ - int fd; - FILE *fp; - - fd = mkostemp(template, flags); - if (fd == -1) - return NULL; - - if (fchmod(fd, m) == -1) - goto fail; - - fp = fdopen(fd, "w"); - if (fp == NULL) - goto fail; - - return fp; -fail: - close(fd); - unlink(template); - return NULL; -}