summaryrefslogtreecommitdiff
path: root/lib
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 /lib
parent549297b74aa6ed5be4b209caf0c1d41ab058d34a (diff)
add mkstemp(char *template, int suffixlen) interface
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdio/mktemp.335
-rw-r--r--lib/libc/stdio/mktemp.c46
2 files changed, 67 insertions, 14 deletions
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;
}
}