From 216c33a5c714619b07772c38a816d92cf3040dee Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Sat, 29 Aug 2020 12:18:13 +0800 Subject: [PATCH] libs/libc/stdlib: Implement mkdtemp(3) syscall See the reference here: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdtemp.html Change-Id: I49081ecafc011a843e6067b1118b53bf65d4418b Signed-off-by: chao.an --- include/stdlib.h | 1 + libs/libc/stdlib/Make.defs | 2 +- libs/libc/stdlib/lib_mkdtemp.c | 87 ++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 libs/libc/stdlib/lib_mkdtemp.c diff --git a/include/stdlib.h b/include/stdlib.h index feab8a02a3..3eaf0e282c 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -237,6 +237,7 @@ lldiv_t lldiv(long long number, long long denom); FAR char *mktemp(FAR char *path_template); int mkstemp(FAR char *path_template); +FAR char *mkdtemp(FAR char *path_template); /* Sorting */ diff --git a/libs/libc/stdlib/Make.defs b/libs/libc/stdlib/Make.defs index 22e33d9b7a..2a5bcb761a 100644 --- a/libs/libc/stdlib/Make.defs +++ b/libs/libc/stdlib/Make.defs @@ -25,7 +25,7 @@ CSRCS += lib_atol.c lib_atoll.c lib_div.c lib_ldiv.c lib_lldiv.c lib_Exit.c CSRCS += lib_itoa.c lib_labs.c lib_llabs.c lib_realpath.c lib_bsearch.c CSRCS += lib_rand.c lib_posix_memalign.c lib_qsort.c lib_srand.c lib_strtol.c CSRCS += lib_strtoll.c lib_strtoul.c lib_strtoull.c lib_strtod.c lib_strtof.c -CSRCS += lib_strtold.c lib_checkbase.c lib_mktemp.c lib_mkstemp.c +CSRCS += lib_strtold.c lib_checkbase.c lib_mktemp.c lib_mkstemp.c lib_mkdtemp.c ifeq ($(CONFIG_LIBC_WCHAR),y) CSRCS += lib_mblen.c lib_mbtowc.c lib_wctomb.c diff --git a/libs/libc/stdlib/lib_mkdtemp.c b/libs/libc/stdlib/lib_mkdtemp.c new file mode 100644 index 0000000000..59e7a146fc --- /dev/null +++ b/libs/libc/stdlib/lib_mkdtemp.c @@ -0,0 +1,87 @@ +/**************************************************************************** + * libs/libc/stdlib/lib_mkdtemp.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include + +#include +#include + +/**************************************************************************** + * Pre-processor definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mkdtemp + * + * Description: + * The mkdtemp() function shall create a directory with a unique name + * derived from template. The application shall ensure that the string + * provided in template is a pathname ending with at least six trailing + * 'X' characters. The mkdtemp() function shall modify the contents of + * template by replacing six or more 'X' characters at the end of the + * pathname with the same number of characters from the portable filename + * character set. The characters shall be chosen such that the resulting + * pathname does not duplicate the name of an existing file at the time + * of the call to mkdtemp(). The mkdtemp() function shall use the + * resulting pathname to create the new directory as if by a call to: + * + * Input Parameters: + * template - The base directory name that will be modified to produce + * the unique name. This must be a full path beginning with /tmp. + * This function will modify only the first XXXXXX characters within + * that full path. + * + * Returned Value: + * Upon successful completion, the mkdtemp() function shall return the + * value of template. Otherwise, it shall return a null pointer and + * shall set errno to indicate the error. + * + ****************************************************************************/ + +FAR char *mkdtemp(FAR char *path_template) +{ + FAR char *path = mktemp(path_template); + + if (path) + { + if (mkdir(path, S_IRWXU) < 0) + { + path = NULL; + } + } + + return path; +}