Skip to content

Commit

Permalink
lib/fs/mkstemp/, src/: fmkomstemp(): Move function to separate file
Browse files Browse the repository at this point in the history
And make it inline.

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Dec 5, 2024
1 parent 90afe61 commit aa3d367
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
13 changes: 13 additions & 0 deletions lib/fs/mkstemp/fmkomstemp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include "fs/mkstemp/fmkomstemp.h"

#include <stdio.h>
#include <sys/types.h>


extern inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m);
46 changes: 46 additions & 0 deletions lib/fs/mkstemp/fmkomstemp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_FS_MKSTEMP_FMKOMSTEMP_H_
#define SHADOW_INCLUDE_LIB_FS_MKSTEMP_FMKOMSTEMP_H_


#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>


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
28 changes: 1 addition & 27 deletions src/useradd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

0 comments on commit aa3d367

Please sign in to comment.