forked from nuttx/nuttx-update
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 <anchao@xiaomi.com>
This commit is contained in:
parent
4067a9f057
commit
216c33a5c7
3 changed files with 89 additions and 1 deletions
|
@ -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 */
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
87
libs/libc/stdlib/lib_mkdtemp.c
Normal file
87
libs/libc/stdlib/lib_mkdtemp.c
Normal file
|
@ -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 <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/****************************************************************************
|
||||
* 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;
|
||||
}
|
Loading…
Reference in a new issue