From 8a0c10fcb9d6e0804d365ec3e2ffb54d047c7193 Mon Sep 17 00:00:00 2001 From: Ben Gras Date: Wed, 14 Jul 2010 22:45:28 +0000 Subject: [PATCH] lib: mkdtemp(), contributed by by Gautam Tirumala --- include/stdlib.h | 1 + lib/libc/stdio/mktemp.c | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index e195ee9df..c6f677afc 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -101,6 +101,7 @@ extern char *optarg; extern int optind, opterr, optopt; _PROTOTYPE(size_t shquote, (const char *arg, char *buf, size_t bufsize)); +_PROTOTYPE(char *mkdtemp, (char *path)); #endif /* _MINIX */ diff --git a/lib/libc/stdio/mktemp.c b/lib/libc/stdio/mktemp.c index e63a47b30..267a76508 100644 --- a/lib/libc/stdio/mktemp.c +++ b/lib/libc/stdio/mktemp.c @@ -44,7 +44,7 @@ static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93"; #include #include -static int _gettemp(char*,int*); +static int _gettemp(char*,int*,int); int mkstemp(path) @@ -52,20 +52,28 @@ mkstemp(path) { int fd; - return (_gettemp(path, &fd) ? fd : -1); + return (_gettemp(path, &fd, 0) ? fd : -1); } char * mktemp(path) char *path; { - return(_gettemp(path, (int *)NULL) ? path : (char *)NULL); + return(_gettemp(path, (int *)NULL, 0) ? path : (char *)NULL); +} + +char * +mkdtemp(path) + char *path; +{ + return(_gettemp(path, (int *)NULL, 1) ? path : (char *)NULL); } static int -_gettemp(path, doopen) +_gettemp(path, doopen, domkdir) char *path; register int *doopen; + int domkdir; { extern int errno; register char *start, *trv; @@ -132,8 +140,12 @@ _gettemp(path, doopen) if (errno != EEXIST) { return(0); } - } - else if (stat(path, &sbuf)) { + } else if(domkdir) { + if (mkdir(path, 0700) >= 0) + return (1); + if (errno != EEXIST) + return (0); + } else if (stat(path, &sbuf)) { return(errno == ENOENT ? 1 : 0); } -- 2.44.0