Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |