Skip to content

Commit

Permalink
libs/libc/stdlib: Implement mkdtemp(3) syscall
Browse files Browse the repository at this point in the history
See the reference here:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdtemp.html

Change-Id: I49081ecafc011a843e6067b1118b53bf65d4418b
Signed-off-by: chao.an <anchao@xiaomi.com>
  • Loading branch information
anchao committed Sep 19, 2020
1 parent e220348 commit b85d7a4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/stdlib.h
Expand Up @@ -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 */

Expand Down
2 changes: 1 addition & 1 deletion libs/libc/stdlib/Make.defs
Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions 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 <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;
}

0 comments on commit b85d7a4

Please sign in to comment.