nuttx/libs/libc/stdlib/lib_mkstemp.c

74 lines
2.9 KiB
C
Raw Normal View History

2014-11-05 16:39:18 +01:00
/****************************************************************************
* libs/libc/stdlib/lib_mkstemp.c
2014-11-05 16:39:18 +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
*
* http://www.apache.org/licenses/LICENSE-2.0
2014-11-05 16:39:18 +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>
#include <unistd.h>
2014-11-05 16:39:18 +01:00
#include <stdlib.h>
#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
* 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:
* 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:
* 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
*
****************************************************************************/
int mkstemp(FAR char *path_template)
2014-11-05 16:39:18 +01:00
{
FAR char *path = mktemp(path_template);
int ret = ERROR;
if (path)
2014-11-05 16:39:18 +01:00
{
ret = open(path, O_RDWR | O_CREAT | O_EXCL, 0666);
2014-11-05 16:39:18 +01:00
}
return ret;
2014-11-05 16:39:18 +01:00
}