fileutils: differentiate xmkstemp and xfmkstemp
Let developer to choose, case by case, what sort of return value is best in her code. The xmkstemp() is for users who want file descriptor as return value of the function, xfmkstemp() will return FILE pointer. Proposed-By: Karel Zak <kzak@redhat.com> CC: Davidlohr Bueso <dave@gnu.org> Reference: http://marc.info/?l=util-linux-ng&m=133129570124003&w=2 Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
fd2e8c693e
commit
7961acce46
|
@ -1,6 +1,20 @@
|
||||||
#ifndef UTIL_LINUX_FILEUTILS
|
#ifndef UTIL_LINUX_FILEUTILS
|
||||||
#define UTIL_LINUX_FILEUTILS
|
#define UTIL_LINUX_FILEUTILS
|
||||||
|
|
||||||
extern FILE * xmkstemp(char **tmpname);
|
extern int xmkstemp(char **tmpname);
|
||||||
|
|
||||||
|
static inline FILE *xfmkstemp(char **tmpname)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
FILE *ret;
|
||||||
|
fd = xmkstemp(tmpname);
|
||||||
|
if (fd == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (!(ret = fdopen(fd, "w+"))) {
|
||||||
|
close(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -8,38 +8,35 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
|
#include "fileutils.h"
|
||||||
#include "pathnames.h"
|
#include "pathnames.h"
|
||||||
#include "xalloc.h"
|
#include "xalloc.h"
|
||||||
|
|
||||||
/* Create open temporary file in safe way. Please notice that the
|
/* Create open temporary file in safe way. Please notice that the
|
||||||
* file permissions are -rw------- by default. */
|
* file permissions are -rw------- by default. */
|
||||||
FILE *xmkstemp(char **tmpname)
|
int xmkstemp(char **tmpname)
|
||||||
{
|
{
|
||||||
char *localtmp;
|
char *localtmp;
|
||||||
char *tmpenv;
|
char *tmpenv;
|
||||||
mode_t old_mode;
|
mode_t old_mode;
|
||||||
int fd;
|
int fd;
|
||||||
FILE *ret;
|
|
||||||
|
|
||||||
tmpenv = getenv("TMPDIR");
|
tmpenv = getenv("TMPDIR");
|
||||||
if (tmpenv)
|
if (tmpenv)
|
||||||
xasprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
|
xasprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
|
||||||
program_invocation_short_name);
|
program_invocation_short_name);
|
||||||
else
|
else
|
||||||
xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
|
xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
|
||||||
program_invocation_short_name);
|
program_invocation_short_name);
|
||||||
old_mode = umask(077);
|
old_mode = umask(077);
|
||||||
fd = mkstemp(localtmp);
|
fd = mkstemp(localtmp);
|
||||||
umask(old_mode);
|
umask(old_mode);
|
||||||
if (fd == -1)
|
if (fd == -1) {
|
||||||
return NULL;
|
free(localtmp);
|
||||||
if (!(ret = fdopen(fd, "w+")))
|
localtmp = NULL;
|
||||||
goto err;
|
}
|
||||||
*tmpname = localtmp;
|
*tmpname = localtmp;
|
||||||
return ret;
|
return fd;
|
||||||
err:
|
|
||||||
close(fd);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TEST_PROGRAM
|
#ifdef TEST_PROGRAM
|
||||||
|
@ -47,7 +44,7 @@ int main(void)
|
||||||
{
|
{
|
||||||
FILE *f;
|
FILE *f;
|
||||||
char *tmpname;
|
char *tmpname;
|
||||||
f = xmkstemp(&tmpname);
|
f = xfmkstemp(&tmpname);
|
||||||
unlink(tmpname);
|
unlink(tmpname);
|
||||||
free(tmpname);
|
free(tmpname);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
|
@ -83,7 +83,7 @@ int setpwnam(struct passwd *pwd)
|
||||||
|
|
||||||
pw_init();
|
pw_init();
|
||||||
|
|
||||||
if ((fp = xmkstemp(&tmpname)) == NULL)
|
if ((fp = xfmkstemp(&tmpname)) == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* ptmp should be owned by root.root or root.wheel */
|
/* ptmp should be owned by root.root or root.wheel */
|
||||||
|
@ -140,7 +140,7 @@ int setpwnam(struct passwd *pwd)
|
||||||
fputs(linebuf, fp);
|
fputs(linebuf, fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* xmkstemp is too restrictive by default for passwd file */
|
/* xfmkstemp is too restrictive by default for passwd file */
|
||||||
if (fchmod(fileno(fp), 0644) < 0)
|
if (fchmod(fileno(fp), 0644) < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
rc = fclose(fp);
|
rc = fclose(fp);
|
||||||
|
|
|
@ -143,7 +143,7 @@ static FILE * pw_tmpfile(int lockfd)
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
char *tmpname = NULL;
|
char *tmpname = NULL;
|
||||||
|
|
||||||
if ((fd = xmkstemp(&tmpname)) == NULL) {
|
if ((fd = xfmkstemp(&tmpname)) == NULL) {
|
||||||
ulckpwdf();
|
ulckpwdf();
|
||||||
err(EXIT_FAILURE, _("can't open temporary file"));
|
err(EXIT_FAILURE, _("can't open temporary file"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -196,7 +196,7 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
|
||||||
line_max = sysconf(_SC_LINE_MAX);
|
line_max = sysconf(_SC_LINE_MAX);
|
||||||
lbuf = xmalloc(line_max);
|
lbuf = xmalloc(line_max);
|
||||||
|
|
||||||
if ((fp = xmkstemp(&tmpname)) == NULL)
|
if ((fp = xfmkstemp(&tmpname)) == NULL)
|
||||||
err(EXIT_FAILURE, _("can't open temporary file"));
|
err(EXIT_FAILURE, _("can't open temporary file"));
|
||||||
unlink(tmpname);
|
unlink(tmpname);
|
||||||
free(tmpname);
|
free(tmpname);
|
||||||
|
|
Loading…
Reference in New Issue