mktemp improvements
- add -p dir, -t and -u. - fix dirname(tmp) check (did not check null terminator). the semantics of mktemp is now the same as OpenBSD mktemp.
This commit is contained in:
parent
590f34c4a9
commit
347f0828f3
22
mktemp.1
22
mktemp.1
|
@ -1,4 +1,4 @@
|
||||||
.Dd January 31, 2015
|
.Dd May 8, 2015
|
||||||
.Dt MKTEMP 1
|
.Dt MKTEMP 1
|
||||||
.Os sbase
|
.Os sbase
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
|
@ -6,7 +6,8 @@
|
||||||
.Nd create temporary file or directory
|
.Nd create temporary file or directory
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl dq
|
.Op Fl dqtu
|
||||||
|
.Op Fl p Ar directory
|
||||||
.Op Ar template
|
.Op Ar template
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
.Nm
|
.Nm
|
||||||
|
@ -20,8 +21,25 @@ tmpdir set to '/tmp' or, if set, the TMPDIR environment variable.
|
||||||
.Bl -tag -width Ds
|
.Bl -tag -width Ds
|
||||||
.It Fl d
|
.It Fl d
|
||||||
Create a temporary directory instead of a file.
|
Create a temporary directory instead of a file.
|
||||||
|
.It Fl p Ar directory
|
||||||
|
Use the specified
|
||||||
|
.Ar directory
|
||||||
|
as a prefix when generating the temporary filename. The directory will be
|
||||||
|
overridden by the user's
|
||||||
|
.Ev TMPDIR
|
||||||
|
environment variable if it is set. This option implies the
|
||||||
|
.Fl t
|
||||||
|
flag (see below).
|
||||||
.It Fl q
|
.It Fl q
|
||||||
Fail silently if an error occurs.
|
Fail silently if an error occurs.
|
||||||
|
.It Fl t
|
||||||
|
Generate a path rooted in a temporary directory.
|
||||||
|
.It Fl u
|
||||||
|
Unlink file before
|
||||||
|
.Nm
|
||||||
|
exits. This is slightly better than
|
||||||
|
.Xr mktemp 3
|
||||||
|
but still introduces a race condition. Use of this option is not encouraged.
|
||||||
.El
|
.El
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr mkdtemp 3 ,
|
.Xr mkdtemp 3 ,
|
||||||
|
|
47
mktemp.c
47
mktemp.c
|
@ -2,6 +2,7 @@
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -9,24 +10,34 @@
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-dq] [template]\n", argv0);
|
eprintf("usage: %s [-dqtu] [-p directory] [template]\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int dflag = 0, qflag = 0, fd;
|
int dflag = 0, pflag = 0, qflag = 0, tflag = 0, uflag = 0, fd;
|
||||||
char *template = "tmp.XXXXXXXXXX",
|
char *template = "tmp.XXXXXXXXXX", *tmpdir = "", *pdir,
|
||||||
*tmpdir = "/tmp", *p,
|
*p, path[PATH_MAX], tmp[PATH_MAX];
|
||||||
path[PATH_MAX], tmp[PATH_MAX];
|
size_t len;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'd':
|
case 'd':
|
||||||
dflag = 1;
|
dflag = 1;
|
||||||
break;
|
break;
|
||||||
|
case 'p':
|
||||||
|
pflag = 1;
|
||||||
|
pdir = EARGF(usage());
|
||||||
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
qflag = 1;
|
qflag = 1;
|
||||||
break;
|
break;
|
||||||
|
case 't':
|
||||||
|
tflag = 1;
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
uflag = 1;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
@ -36,18 +47,26 @@ main(int argc, char *argv[])
|
||||||
else if (argc == 1)
|
else if (argc == 1)
|
||||||
template = argv[0];
|
template = argv[0];
|
||||||
|
|
||||||
if ((p = getenv("TMPDIR")))
|
if (!argc || pflag || tflag) {
|
||||||
tmpdir = p;
|
if ((p = getenv("TMPDIR")))
|
||||||
|
tmpdir = p;
|
||||||
|
else if (pflag)
|
||||||
|
tmpdir = pdir;
|
||||||
|
else
|
||||||
|
tmpdir = "/tmp";
|
||||||
|
}
|
||||||
|
|
||||||
|
len = estrlcpy(path, tmpdir, sizeof(path));
|
||||||
|
if (path[0] && path[len - 1] != '/')
|
||||||
|
estrlcat(path, "/", sizeof(path));
|
||||||
|
|
||||||
estrlcpy(tmp, template, sizeof(tmp));
|
estrlcpy(tmp, template, sizeof(tmp));
|
||||||
p = dirname(tmp);
|
p = dirname(tmp);
|
||||||
if (p[0] != '.') {
|
if (!(p[0] == '.' && p[1] == '\0')) {
|
||||||
estrlcpy(path, template, sizeof(path));
|
if (tflag && !pflag)
|
||||||
} else {
|
eprintf("template must not contain directory separators in -t mode\n");
|
||||||
estrlcpy(path, tmpdir, sizeof(path));
|
|
||||||
estrlcat(path, "/", sizeof(path));
|
|
||||||
estrlcat(path, template, sizeof(path));
|
|
||||||
}
|
}
|
||||||
|
estrlcat(path, template, sizeof(path));
|
||||||
|
|
||||||
if (dflag) {
|
if (dflag) {
|
||||||
if (!mkdtemp(path)) {
|
if (!mkdtemp(path)) {
|
||||||
|
@ -64,6 +83,8 @@ main(int argc, char *argv[])
|
||||||
if (close(fd))
|
if (close(fd))
|
||||||
eprintf("close %s:", path);
|
eprintf("close %s:", path);
|
||||||
}
|
}
|
||||||
|
if (uflag)
|
||||||
|
unlink(path);
|
||||||
puts(path);
|
puts(path);
|
||||||
|
|
||||||
efshut(stdout, "<stdout>");
|
efshut(stdout, "<stdout>");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user