summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorDavid E. O'Brien <obrien@FreeBSD.org>1999-11-18 09:08:39 +0000
committerDavid E. O'Brien <obrien@FreeBSD.org>1999-11-18 09:08:39 +0000
commitf0650fa2d06dcfa7c3103e50537235200d39a534 (patch)
treea8a0f3504bcb88701bc22ebadf391b7978805885 /lib/libc
parentf86a8cc599046977a20c3979a55722253ca3c1e9 (diff)
Notes
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/stdio/Makefile.inc2
-rw-r--r--lib/libc/stdio/mktemp.338
-rw-r--r--lib/libc/stdio/mktemp.c33
3 files changed, 56 insertions, 17 deletions
diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc
index be539d20d263..514ab31c15bc 100644
--- a/lib/libc/stdio/Makefile.inc
+++ b/lib/libc/stdio/Makefile.inc
@@ -31,7 +31,7 @@ MLINKS+=fseek.3 fgetpos.3 fseek.3 fseeko.3 fseek.3 fsetpos.3 fseek.3 ftell.3 \
fseek.3 ftello.3 fseek.3 rewind.3
MLINKS+=funopen.3 fropen.3 funopen.3 fwopen.3
MLINKS+=getc.3 fgetc.3 getc.3 getchar.3 getc.3 getw.3
-MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3
+MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3 mktemp.3 mkstemps.3
MLINKS+=printf.3 asprintf.3 printf.3 fprintf.3 \
printf.3 snprintf.3 printf.3 sprintf.3 \
printf.3 vasprintf.3 \
diff --git a/lib/libc/stdio/mktemp.3 b/lib/libc/stdio/mktemp.3
index 50d7de4cda3f..a6879c0988e4 100644
--- a/lib/libc/stdio/mktemp.3
+++ b/lib/libc/stdio/mktemp.3
@@ -30,6 +30,7 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
.\"
.Dd February 11, 1998
.Dt MKTEMP 3
@@ -43,6 +44,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
@@ -82,6 +85,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.
+.Pp
+The
.Fn mkdtemp
function makes the same replacement to the template as in
.Xr mktemp 3
@@ -96,13 +109,16 @@ functions return a pointer to the template on success and
on failure.
The
.Fn mkstemp
-function
-returns \-1 if no suitable file could be created.
+and
+.Fn mkstemps
+functions
+return \-1 if no suitable file could be created.
If either call fails an error code is placed in the global variable
.Va errno .
.Sh ERRORS
The
-.Fn mkstemp
+.Fn mkstemp ,
+.Fn mkstemps
and
.Fn mkdtemp
functions
@@ -115,7 +131,8 @@ The pathname portion of the template is not an existing directory.
.El
.Pp
The
-.Fn mkstemp
+.Fn mkstemp ,
+.Fn mkstemps
and
.Fn mkdtemp
functions
@@ -127,7 +144,9 @@ function.
.Pp
The
.Fn mkstemp
-function
+and
+.Fn mkstemps
+functions
may also set
.Va errno
to any value specified by the
@@ -146,14 +165,15 @@ function.
A common problem that results in a core dump is that the programmer
passes in a read-only string to
.Fn mktemp ,
-.Fn mkstemp
+.Fn mkstemp ,
+.Fn mkstemps
or
.Fn mkdtemp .
This is common with programs that were developed before
.St -ansiC
compilers were common.
For example, calling
-.Fn mkstemp
+.Fn mkstemp
with an argument of
.Qq /tmp/tempfile.XXXXXX
will result in a core dump due to
@@ -186,3 +206,7 @@ The
.Fn mkdtemp
function first appeared in
.Ox 2.2 .
+The
+.Fn mkstemps
+function first appeared in
+.Fx 3.4 .
diff --git a/lib/libc/stdio/mktemp.c b/lib/libc/stdio/mktemp.c
index 4a204f266c5b..af547c618ff8 100644
--- a/lib/libc/stdio/mktemp.c
+++ b/lib/libc/stdio/mktemp.c
@@ -50,7 +50,17 @@ static const char rcsid[] =
char *_mktemp __P((char *));
-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)
@@ -58,27 +68,25 @@ 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(path)
char *path;
{
- return(_gettemp(path, (int *)NULL, 0) ? path : (char *)NULL);
+ return(_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
}
-#ifdef UNSAFE_WARN
__warn_references(mktemp,
"warning: mktemp() possibly used unsafely; consider using mkstemp()");
-#endif
char *
mktemp(path)
@@ -88,12 +96,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;
@@ -105,7 +114,13 @@ _gettemp(path, doopen, domkdir)
pid = getpid();
for (trv = path; *trv; ++trv)
;
+ trv -= slen;
+ suffp = trv;
--trv;
+ if (trv < path) {
+ errno = EINVAL;
+ return (0);
+ }
while (*trv == 'X' && pid != 0) {
*trv-- = (pid % 10) + '0';
pid /= 10;
@@ -167,7 +182,7 @@ _gettemp(path, doopen, domkdir)
if (*trv == 'Z')
*trv++ = 'a';
else {
- if (isdigit(*trv))
+ if (isdigit((unsigned char)*trv))
*trv = 'a';
else if (*trv == 'z') /* inc from z to A */
*trv = 'A';