Skip to content

Add suffix to tempnam() #575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2639,7 +2639,7 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
char buf[4096];
char *path;

tmp = php_open_temporary_file(NULL, NULL, &path TSRMLS_CC);
tmp = php_open_temporary_file(NULL, NULL, NULL, &path TSRMLS_CC);
if (tmp == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open temporary file");
RETURN_FALSE;
Expand Down
3 changes: 2 additions & 1 deletion ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_file, 0, 0, 1)
ZEND_ARG_INFO(0, context)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_tempnam, 0)
ZEND_BEGIN_ARG_INFO_EX(arginfo_tempnam, 0, 0, 2)
ZEND_ARG_INFO(0, dir)
ZEND_ARG_INFO(0, prefix)
ZEND_ARG_INFO(0, suffix)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_tmpfile, 0)
Expand Down
10 changes: 5 additions & 5 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,18 +799,18 @@ PHP_FUNCTION(file)
}
/* }}} */

/* {{{ proto string tempnam(string dir, string prefix)
/* {{{ proto string tempnam(string dir, string prefix [, string suffix])
Create a unique filename in a directory */
PHP_FUNCTION(tempnam)
{
char *dir, *prefix;
int dir_len, prefix_len;
char *dir, *prefix, *suffix = "";
int dir_len, prefix_len, suffix_len = 0;
size_t p_len;
char *opened_path;
char *p;
int fd;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ps", &dir, &dir_len, &prefix, &prefix_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ps|s", &dir, &dir_len, &prefix, &prefix_len, &suffix, &suffix_len) == FAILURE) {
return;
}

Expand All @@ -825,7 +825,7 @@ PHP_FUNCTION(tempnam)

RETVAL_FALSE;

if ((fd = php_open_temporary_fd_ex(dir, p, &opened_path, 1 TSRMLS_CC)) >= 0) {
if ((fd = php_open_temporary_fd_ex(dir, p, &opened_path, suffix, 1 TSRMLS_CC)) >= 0) {
close(fd);
RETVAL_STRING(opened_path, 0);
}
Expand Down
81 changes: 81 additions & 0 deletions ext/standard/tests/file/tempnam_suffix.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
--TEST--
Test tempnam() function: usage variations - specify a suffix
--SKIPIF--
<?php
if(substr(PHP_OS, 0, 3) == "WIN")
die("skip Do not run on Windows");
?>
--FILE--
<?php
/* Prototype: string tempnam ( string $dir, string $prefix, string $suffix );
Description: Create file with unique file name.
*/

/* Passing args for $prefix and $suffix */

echo "*** Testing tempnam() with suffixes ***\n";
$file_path = dirname(__FILE__)."/tempnamSuffix";
mkdir($file_path);

/* An array of suffixes */
$names_arr = array(
NULL,
"",
".png",
);

for( $i=0; $i<count($names_arr); $i++ ) {
echo "-- Iteration $i --\n";
$file_name = tempnam("$file_path", "prefix", $names_arr[$i]);

/* creating the files in existing dir */
if( file_exists($file_name) ) {
echo "File name is => ";
print($file_name);
echo "\n";

echo "File permissions are => ";
printf("%o", fileperms($file_name) );
echo "\n";

echo "File created in => ";
$file_dir = dirname($file_name);

if ($file_dir == sys_get_temp_dir()) {
echo "temp dir\n";
}
else if ($file_dir == $file_path) {
echo "directory specified\n";
}
else {
echo "unknown location\n";
}

}
else {
echo "-- File is not created --\n";
}

unlink($file_name);
}

rmdir($file_path);
echo "\n*** Done ***\n";
?>
--EXPECTF--
*** Testing tempnam() with suffixes ***
-- Iteration 0 --
File name is => %s/prefix%s
File permissions are => 100600
File created in => directory specified
-- Iteration 1 --
File name is => %s/prefix%s
File permissions are => 100600
File created in => directory specified
-- Iteration 2 --
File name is => %s/prefix%s.png
File permissions are => 100600
File created in => directory specified

*** Done ***

20 changes: 10 additions & 10 deletions main/php_open_temporary_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
* SUCH DAMAGE.
*/

static int php_do_open_temporary_file(const char *path, const char *pfx, char **opened_path_p TSRMLS_DC)
static int php_do_open_temporary_file(const char *path, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC)
{
char *trailing_slash;
char *opened_path;
Expand Down Expand Up @@ -138,7 +138,7 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
trailing_slash = "/";
}

if (spprintf(&opened_path, 0, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) {
if (spprintf(&opened_path, 0, "%s%s%sXXXXXX%s", new_state.cwd, trailing_slash, pfx, suffix) >= MAXPATHLEN) {
efree(opened_path);
efree(new_state.cwd);
return -1;
Expand All @@ -158,7 +158,7 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
}

#elif defined(HAVE_MKSTEMP)
fd = mkstemp(opened_path);
fd = mkstemps(opened_path, strlen(suffix));
#else
if (mktemp(opened_path)) {
fd = VCWD_OPEN(opened_path, open_flags);
Expand Down Expand Up @@ -264,7 +264,7 @@ PHPAPI const char* php_get_temporary_directory(TSRMLS_D)
* This function should do its best to return a file pointer to a newly created
* unique file, on every platform.
*/
PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, zend_bool open_basedir_check TSRMLS_DC)
PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, const char *suffix, zend_bool open_basedir_check TSRMLS_DC)
{
int fd;
const char *temp_dir;
Expand All @@ -281,30 +281,30 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **ope
temp_dir = php_get_temporary_directory(TSRMLS_C);

if (temp_dir && *temp_dir != '\0' && (!open_basedir_check || !php_check_open_basedir(temp_dir TSRMLS_CC))) {
return php_do_open_temporary_file(temp_dir, pfx, opened_path_p TSRMLS_CC);
return php_do_open_temporary_file(temp_dir, pfx, suffix, opened_path_p TSRMLS_CC);
} else {
return -1;
}
}

/* Try the directory given as parameter. */
fd = php_do_open_temporary_file(dir, pfx, opened_path_p TSRMLS_CC);
fd = php_do_open_temporary_file(dir, pfx, suffix, opened_path_p TSRMLS_CC);
if (fd == -1) {
/* Use default temporary directory. */
goto def_tmp;
}
return fd;
}

PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC)
PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC)
{
return php_open_temporary_fd_ex(dir, pfx, opened_path_p, 0 TSRMLS_CC);
return php_open_temporary_fd_ex(dir, pfx, opened_path_p, suffix, 0 TSRMLS_CC);
}

PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC)
PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC)
{
FILE *fp;
int fd = php_open_temporary_fd(dir, pfx, opened_path_p TSRMLS_CC);
int fd = php_open_temporary_fd(dir, pfx, suffix, opened_path_p TSRMLS_CC);

if (fd == -1) {
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions main/php_open_temporary_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#define PHP_OPEN_TEMPORARY_FILE_H

BEGIN_EXTERN_C()
PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC);
PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, zend_bool open_basedir_check TSRMLS_DC);
PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, char **opened_path_p TSRMLS_DC);
PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC);
PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, const char *suffix, zend_bool open_basedir_check TSRMLS_DC);
PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, const char *suffix, char **opened_path_p TSRMLS_DC);
PHPAPI const char *php_get_temporary_directory(TSRMLS_D);
PHPAPI void php_shutdown_temporary_directory(void);
END_EXTERN_C()
Expand Down
2 changes: 1 addition & 1 deletion main/rfc1867.c
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
/* in non-debug mode we have no problem with 0-length files */
{
#endif
fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, 1 TSRMLS_CC);
fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, "", 1 TSRMLS_CC);
upload_cnt--;
if (fd == -1) {
sapi_module.sapi_error(E_WARNING, "File upload error - unable to create a temporary file");
Expand Down
4 changes: 2 additions & 2 deletions main/streams/php_stream_plain_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STRE
PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC);
#define php_stream_fopen_tmpfile() _php_stream_fopen_tmpfile(0 STREAMS_CC TSRMLS_CC)

PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC);
#define php_stream_fopen_temporary_file(dir, pfx, opened_path) _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_CC TSRMLS_CC)
PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, const char *suffix, char **opened_path STREAMS_DC TSRMLS_DC);
#define php_stream_fopen_temporary_file(dir, pfx, opened_path, suffix) _php_stream_fopen_temporary_file((dir), (pfx), (suffix), (opened_path) STREAMS_CC TSRMLS_CC)

/* This is a utility API for extensions that are opening a stream, converting it
* to a FILE* and then closing it again. Be warned that fileno() on the result
Expand Down
6 changes: 3 additions & 3 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ static php_stream *_php_stream_fopen_from_file_int(FILE *file, const char *mode
return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
}

PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC)
PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, const char *suffix, char **opened_path STREAMS_DC TSRMLS_DC)
{
int fd = php_open_temporary_fd(dir, pfx, opened_path TSRMLS_CC);
int fd = php_open_temporary_fd(dir, pfx, suffix, opened_path TSRMLS_CC);

if (fd != -1) {
php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
Expand All @@ -204,7 +204,7 @@ PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char
PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC)
{
char *opened_path = NULL;
int fd = php_open_temporary_fd(NULL, "php", &opened_path TSRMLS_CC);
int fd = php_open_temporary_fd(NULL, "php", "", &opened_path TSRMLS_CC);

if (fd != -1) {
php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
Expand Down