2014-11-05 16:39:18 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdlib/lib_mkstemp.c
|
2014-11-05 16:39:18 +01:00
|
|
|
*
|
2020-03-21 17:20:03 +01:00
|
|
|
* 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
|
2014-11-05 16:39:18 +01:00
|
|
|
*
|
2020-03-21 17:20:03 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-05 16:39:18 +01:00
|
|
|
*
|
2020-03-21 17:20:03 +01:00
|
|
|
* 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.
|
2014-11-05 16:39:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
2020-09-17 10:03:38 +02:00
|
|
|
#include <unistd.h>
|
2014-11-05 16:39:18 +01:00
|
|
|
#include <stdlib.h>
|
2020-09-17 10:03:38 +02:00
|
|
|
#include <fcntl.h>
|
2014-11-05 16:39:18 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mkstemp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The mkstemp() function replaces the contents of the string pointed to
|
2014-11-06 14:00:23 +01:00
|
|
|
* 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().
|
2014-11-05 16:39:18 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2014-11-06 14:00:23 +01:00
|
|
|
* 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.
|
2014-11-05 16:39:18 +01:00
|
|
|
* This function will modify only the first XXXXXX characters within
|
|
|
|
* that full path.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-07-13 21:36:18 +02:00
|
|
|
* Upon successful completion, mkstemp() returns an open file descriptor.
|
|
|
|
* Otherwise, -1 is returned if no suitable file could be created.
|
2014-11-05 16:39:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-06 14:00:23 +01:00
|
|
|
int mkstemp(FAR char *path_template)
|
2014-11-05 16:39:18 +01:00
|
|
|
{
|
2020-09-17 10:03:38 +02:00
|
|
|
FAR char *path = mktemp(path_template);
|
|
|
|
int ret = ERROR;
|
2020-06-16 10:21:28 +02:00
|
|
|
|
2020-09-17 10:03:38 +02:00
|
|
|
if (path)
|
2014-11-05 16:39:18 +01:00
|
|
|
{
|
2020-09-17 10:03:38 +02:00
|
|
|
ret = open(path, O_RDWR | O_CREAT | O_EXCL, 0666);
|
2014-11-05 16:39:18 +01:00
|
|
|
}
|
|
|
|
|
2020-09-17 10:03:38 +02:00
|
|
|
return ret;
|
2014-11-05 16:39:18 +01:00
|
|
|
}
|