74 lines
2.9 KiB
C
74 lines
2.9 KiB
C
/****************************************************************************
|
|
* libs/libc/stdlib/lib_mkstemp.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 <fcntl.h>
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: mkstemp
|
|
*
|
|
* Description:
|
|
* The mkstemp() function replaces the contents of the string pointed to
|
|
* by path_template by a unique filename, and returns a file descriptor
|
|
* for the file open for reading and writing. The function thus prevents
|
|
* any possible race condition between testing whether the file exists and
|
|
* opening it for use. The string in path_template should look like a
|
|
* filename with six trailing 'X' s; mkstemp() replaces each 'X' with a
|
|
* character from the portable filename character set. The characters are
|
|
* chosen such that the resulting name does not duplicate the name of an
|
|
* existing file at the time of a call to mkstemp().
|
|
*
|
|
* Input Parameters:
|
|
* path_template - The base file name that will be modified to produce
|
|
* the unique file 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, mkstemp() returns an open file descriptor.
|
|
* Otherwise, -1 is returned if no suitable file could be created.
|
|
*
|
|
****************************************************************************/
|
|
|
|
int mkstemp(FAR char *path_template)
|
|
{
|
|
FAR char *path = mktemp(path_template);
|
|
int ret = ERROR;
|
|
|
|
if (path)
|
|
{
|
|
ret = open(path, O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
}
|
|
|
|
return ret;
|
|
}
|