Skip to content

Commit 5484b8d

Browse files
committed
Better SPL fscanf
1 parent 713dfd7 commit 5484b8d

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

ext/spl/spl_directory.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "php_ini.h"
2323
#include "ext/standard/info.h"
2424
#include "ext/standard/file.h"
25+
#include "ext/standard/scanf.h"
2526
#include "ext/standard/php_string.h"
2627
#include "zend_compile.h"
2728
#include "zend_exceptions.h"
@@ -2658,23 +2659,30 @@ PHP_METHOD(SplFileObject, fpassthru)
26582659
/* {{{ Implements a mostly ANSI compatible fscanf() */
26592660
PHP_METHOD(SplFileObject, fscanf)
26602661
{
2662+
int result, num_varargs = 0;
2663+
zend_string *format_str;
2664+
zval *varargs= NULL;
26612665
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
2662-
zend_function *func_ptr;
26632666

2664-
if(!intern->u.file.stream) {
2665-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2667+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S*", &format_str, &varargs, &num_varargs) == FAILURE) {
26662668
RETURN_THROWS();
26672669
}
26682670

2669-
spl_filesystem_file_free_line(intern);
2670-
intern->u.file.current_line_num++;
2671+
if (!intern->u.file.stream) {
2672+
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2673+
RETURN_THROWS();
2674+
}
26712675

2672-
func_ptr = (zend_function *)zend_hash_str_find_ptr(EG(function_table), "fscanf", sizeof("fscanf") - 1);
2673-
if (func_ptr == NULL) {
2674-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Internal error, function fscanf() not found. Please report");
2676+
/* Get next line */
2677+
if (spl_filesystem_file_read(intern, 0) == FAILURE) {
26752678
RETURN_THROWS();
2676-
}
2677-
spl_filesystem_file_call(intern, func_ptr, ZEND_NUM_ARGS(), return_value);
2679+
}
2680+
2681+
result = php_sscanf_internal(intern->u.file.current_line, ZSTR_VAL(format_str), num_varargs, varargs, 0, return_value);
2682+
2683+
if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
2684+
WRONG_PARAM_COUNT;
2685+
}
26782686
}
26792687
/* }}} */
26802688

@@ -2763,7 +2771,7 @@ PHP_METHOD(SplFileObject, fstat)
27632771
RETURN_THROWS();
27642772
}
27652773

2766-
zend_call_known_function(func_ptr, NULL, NULL, return_value, 0, NULL, NULL);
2774+
zend_call_known_function(func_ptr, NULL, NULL, return_value, 0, NULL, NULL);
27672775
}
27682776
/* }}} */
27692777

0 commit comments

Comments
 (0)