From 1aa88f2b271c483d293b2b64d1846e9271690216 Mon Sep 17 00:00:00 2001 From: millert Date: Fri, 1 Mar 2024 21:30:40 +0000 Subject: [PATCH] Add mkdtemps(3), like mkdtemp(3) but with a suffix. OK deraadt@ tb@ --- include/stdlib.h | 3 ++- lib/libc/Symbols.list | 1 + lib/libc/hidden/stdlib.h | 3 ++- lib/libc/shlib_version | 2 +- lib/libc/stdlib/mkdtemp.c | 10 +++++++++- lib/libc/stdlib/mktemp.3 | 30 +++++++++++++++++++++++------- 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index ab8a2ae90c3..d1292e6d437 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -1,4 +1,4 @@ -/* $OpenBSD: stdlib.h,v 1.76 2019/05/10 15:03:24 otto Exp $ */ +/* $OpenBSD: stdlib.h,v 1.77 2024/03/01 21:30:40 millert Exp $ */ /* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */ /*- @@ -289,6 +289,7 @@ void setprogname(const char *); extern char *suboptarg; /* getsubopt(3) external variable */ +char * mkdtemps(char *, int); int mkstemps(char *, int); int mkostemps(char *, int, int); diff --git a/lib/libc/Symbols.list b/lib/libc/Symbols.list index a6bc65f5037..251760f812e 100644 --- a/lib/libc/Symbols.list +++ b/lib/libc/Symbols.list @@ -1410,6 +1410,7 @@ getw getwc getwchar mkdtemp +mkdtemps mkostemp mkostemps mkstemp diff --git a/lib/libc/hidden/stdlib.h b/lib/libc/hidden/stdlib.h index c890889dd47..027067eba74 100644 --- a/lib/libc/hidden/stdlib.h +++ b/lib/libc/hidden/stdlib.h @@ -1,4 +1,4 @@ -/* $OpenBSD: stdlib.h,v 1.19 2024/01/19 19:45:02 millert Exp $ */ +/* $OpenBSD: stdlib.h,v 1.20 2024/03/01 21:30:40 millert Exp $ */ /* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */ /*- @@ -116,6 +116,7 @@ PROTO_STD_DEPRECATED(mbstowcs); PROTO_STD_DEPRECATED(mbtowc); PROTO_DEPRECATED(mergesort); PROTO_DEPRECATED(mkdtemp); +PROTO_DEPRECATED(mkdtemps); PROTO_NORMAL(mkostemp); PROTO_DEPRECATED(mkostemps); PROTO_NORMAL(mkstemp); diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version index 211c24639f9..198973d46b3 100644 --- a/lib/libc/shlib_version +++ b/lib/libc/shlib_version @@ -1,4 +1,4 @@ -major=98 +major=99 minor=0 # note: If changes were made to include/thread_private.h or if system calls # were added/changed then librthread/shlib_version must also be updated. diff --git a/lib/libc/stdlib/mkdtemp.c b/lib/libc/stdlib/mkdtemp.c index c33c3b4e8bb..c11501f8933 100644 --- a/lib/libc/stdlib/mkdtemp.c +++ b/lib/libc/stdlib/mkdtemp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mkdtemp.c,v 1.1 2024/01/19 19:45:02 millert Exp $ */ +/* $OpenBSD: mkdtemp.c,v 1.2 2024/03/01 21:30:40 millert Exp $ */ /* * Copyright (c) 2024 Todd C. Miller * @@ -31,3 +31,11 @@ mkdtemp(char *path) return path; return NULL; } + +char * +mkdtemps(char *path, int slen) +{ + if (__mktemp4(path, slen, 0, mkdtemp_cb) == 0) + return path; + return NULL; +} diff --git a/lib/libc/stdlib/mktemp.3 b/lib/libc/stdlib/mktemp.3 index d4bd7bdc91b..83b7c9eb301 100644 --- a/lib/libc/stdlib/mktemp.3 +++ b/lib/libc/stdlib/mktemp.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: mktemp.3,v 1.1 2024/01/19 16:30:28 millert Exp $ +.\" $OpenBSD: mktemp.3,v 1.2 2024/03/01 21:30:40 millert Exp $ .\" .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -27,7 +27,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd $Mdocdate: January 19 2024 $ +.Dd $Mdocdate: March 1 2024 $ .Dt MKTEMP 3 .Os .Sh NAME @@ -36,7 +36,8 @@ .Nm mkostemp , .Nm mkstemps , .Nm mkostemps , -.Nm mkdtemp +.Nm mkdtemp , +.Nm mkdtemps .Nd make temporary file name (unique) .Sh SYNOPSIS .In stdlib.h @@ -48,6 +49,8 @@ .Fn mkstemps "char *template" "int suffixlen" .Ft char * .Fn mkdtemp "char *template" +.Ft char * +.Fn mkdtemps "char *template" "int suffixlen" .In stdlib.h .In fcntl.h .Ft int @@ -142,11 +145,19 @@ The function makes the same replacement to the template as in .Fn mktemp and creates the template directory, mode 0700. +The +.Fn mkdtemps +function acts the same as +.Fn mkdtemp , +except that it permits a suffix to exist in the template, +similar to +.Fn mkstemps . .Sh RETURN VALUES The -.Fn mktemp +.Fn mktemp , +.Fn mkdtemp , and -.Fn mkdtemp +.Fn mkdtemps functions return a pointer to the template on success and .Dv NULL on failure. @@ -356,9 +367,10 @@ as of it is no longer a part of the standard. .Pp The -.Fn mkstemps +.Fn mkstemps , +.Fn mkostemps , and -.Fn mkostemps +.Fn mkdtemps functions are non-standard and should not be used if portability is required. .Sh HISTORY A @@ -383,6 +395,10 @@ and .Fn mkostemps functions appeared in .Ox 5.7 . +The +.Fn mkdtemps +function appeared in +.Ox 7.5 . .Sh BUGS For .Fn mktemp -- 2.20.1