summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>1998-06-30 23:03:14 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>1998-06-30 23:03:14 +0000
commitf4a50b11295c183e4eca008e88cd12afcd88b1ab (patch)
tree5c5ac94af3b9f91d6c036f0af66f60a2346bea74
parent549297b74aa6ed5be4b209caf0c1d41ab058d34a (diff)
add mkstemp(char *template, int suffixlen) interface
-rw-r--r--include/unistd.h3
-rw-r--r--lib/libc/stdio/mktemp.335
-rw-r--r--lib/libc/stdio/mktemp.c46
3 files changed, 69 insertions, 15 deletions
diff --git a/include/unistd.h b/include/unistd.h
index d81cb6093ad..5f6e47ad7b3 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: unistd.h,v 1.21 1998/05/13 08:50:52 deraadt Exp $ */
+/* $OpenBSD: unistd.h,v 1.22 1998/06/30 23:03:09 deraadt Exp $ */
/* $NetBSD: unistd.h,v 1.26.4.1 1996/05/28 02:31:51 mrg Exp $ */
/*-
@@ -133,6 +133,7 @@ int iruserok __P((u_int32_t, int, const char *, const char *));
int lchown __P((const char *, uid_t, gid_t));
char *mkdtemp __P((char *));
int mkstemp __P((char *));
+int mkstemps __P((char *, int));
char *mktemp __P((char *));
int nfssvc __P((int, void *));
int nice __P((int));
diff --git a/lib/libc/stdio/mktemp.3 b/lib/libc/stdio/mktemp.3
index 5d450366031..3bed7904fdc 100644
--- a/lib/libc/stdio/mktemp.3
+++ b/lib/libc/stdio/mktemp.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: mktemp.3,v 1.10 1998/03/16 22:32:31 deraadt Exp $
+.\" $OpenBSD: mktemp.3,v 1.11 1998/06/30 23:03:11 deraadt Exp $
.\"
.\" Copyright (c) 1989, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -43,6 +43,8 @@
.Fn mktemp "char *template"
.Ft int
.Fn mkstemp "char *template"
+.Ft int
+.Fn mkstemps "char *template, int suffixlen"
.Ft char *
.Fn mkdtemp "char *template"
.Sh DESCRIPTION
@@ -85,6 +87,16 @@ This avoids the race between testing for a file's existence and opening it
for use.
.Pp
The
+.Fn mkstemps
+function acts the same as
+.Fn mkstemp ,
+except it permits a suffix to exist in the template. The template
+should be of the form
+.Pa /tmp/tmpXXXXXXsuffix .
+.Fn mkstemps
+is told the length of the suffix string, ie. strlen("suffix");
+.Pp
+The
.Fn mkdtemp
function makes the same replacement to the template as in
.Xr mktemp 3
@@ -138,6 +150,19 @@ to any value specified by the
function.
.Pp
The
+.Fn mkstemps
+function
+may also set
+.Va errno
+to any value specified by the
+.Xr open 2
+function or,
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The suffix length is longer than the template length.
+.El
+.Pp
+The
.Fn mkdtemp
function
may also set
@@ -177,7 +202,9 @@ will output a warning message whenever it links code that uses the
.Pp
The
.Fn mkdtemp
-function is nonstandard and should not be used if portability
+and
+.Fn mkstemps
+functions are nonstandard and should not be used if portability
is required.
.Sh SEE ALSO
.Xr chmod 2 ,
@@ -198,3 +225,7 @@ The
.Fn mkdtemp
function appeared in
.Ox 2.2 .
+The
+.Fn mkstemps
+function appeared in
+.Ox 2.3 .
diff --git a/lib/libc/stdio/mktemp.c b/lib/libc/stdio/mktemp.c
index 53b40b027f3..a992e24f164 100644
--- a/lib/libc/stdio/mktemp.c
+++ b/lib/libc/stdio/mktemp.c
@@ -32,7 +32,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: mktemp.c,v 1.12 1998/04/14 19:25:11 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: mktemp.c,v 1.13 1998/06/30 23:03:13 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@@ -44,7 +44,17 @@ static char rcsid[] = "$OpenBSD: mktemp.c,v 1.12 1998/04/14 19:25:11 deraadt Exp
#include <ctype.h>
#include <unistd.h>
-static int _gettemp __P((char *, int *, int));
+static int _gettemp __P((char *, int *, int, int));
+
+int
+mkstemps(path, slen)
+ char *path;
+ int slen;
+{
+ int fd;
+
+ return (_gettemp(path, &fd, 0, slen) ? fd : -1);
+}
int
mkstemp(path)
@@ -52,14 +62,14 @@ mkstemp(path)
{
int fd;
- return (_gettemp(path, &fd, 0) ? fd : -1);
+ return (_gettemp(path, &fd, 0, 0) ? fd : -1);
}
char *
mkdtemp(path)
char *path;
{
- return(_gettemp(path, (int *)NULL, 1) ? path : (char *)NULL);
+ return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
}
char *_mktemp __P((char *));
@@ -68,7 +78,7 @@ char *
_mktemp(path)
char *path;
{
- return(_gettemp(path, (int *)NULL, 0) ? path : (char *)NULL);
+ return(_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
}
__warn_references(mktemp,
@@ -83,12 +93,13 @@ mktemp(path)
static int
-_gettemp(path, doopen, domkdir)
+_gettemp(path, doopen, domkdir, slen)
char *path;
register int *doopen;
int domkdir;
+ int slen;
{
- register char *start, *trv;
+ register char *start, *trv, *suffp;
struct stat sbuf;
int pid, rval;
@@ -97,10 +108,16 @@ _gettemp(path, doopen, domkdir)
return(0);
}
- pid = getpid();
for (trv = path; *trv; ++trv)
;
+ trv -= slen;
+ suffp = trv;
--trv;
+ if (trv < path) {
+ errno = EINVAL;
+ return (0);
+ }
+ pid = getpid();
while (*trv == 'X' && pid != 0) {
*trv-- = (pid % 10) + '0';
pid /= 10;
@@ -158,16 +175,21 @@ _gettemp(path, doopen, domkdir)
/* tricky little algorithm for backward compatibility */
for (trv = start;;) {
if (!*trv)
- return(0);
- if (*trv == 'Z')
+ return (0);
+ if (*trv == 'Z') {
+ if (trv == suffp)
+ return (0);
*trv++ = 'a';
- else {
+ } else {
if (isdigit(*trv))
*trv = 'a';
else if (*trv == 'z') /* inc from z to A */
*trv = 'A';
- else
+ else {
+ if (trv == suffp)
+ return (0);
++*trv;
+ }
break;
}
}