mirror of
https://github.com/php/php-src.git
synced 2025-08-18 15:08:55 +02:00
Added sscanf() function.
This commit is contained in:
parent
d7ac0f2c9d
commit
0b7fd17c2b
7 changed files with 1413 additions and 2 deletions
|
@ -173,6 +173,8 @@ function_entry basic_functions[] = {
|
|||
PHP_FALIAS(strchr, strstr, NULL)
|
||||
PHP_NAMED_FE(sprintf, PHP_FN(user_sprintf), NULL)
|
||||
PHP_NAMED_FE(printf, PHP_FN(user_printf), NULL)
|
||||
PHP_FE(sscanf, NULL)
|
||||
PHP_FE(fscanf, NULL)
|
||||
|
||||
PHP_FE(parse_url, NULL)
|
||||
PHP_FE(urlencode, NULL)
|
||||
|
|
|
@ -86,6 +86,9 @@ extern int fclose();
|
|||
#endif
|
||||
|
||||
#include "php_realpath.h"
|
||||
#include "scanf.h"
|
||||
#include "zend_API.h"
|
||||
|
||||
|
||||
/* }}} */
|
||||
/* {{{ ZTS-stuff / Globals / Prototypes */
|
||||
|
@ -999,6 +1002,7 @@ PHP_FUNCTION(fgetc) {
|
|||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string fgetss(int fp, int length [, string allowable_tags])
|
||||
Get a line from file pointer and strip HTML tags */
|
||||
|
||||
|
@ -1060,6 +1064,74 @@ PHP_FUNCTION(fgetss)
|
|||
}
|
||||
|
||||
/* }}} */
|
||||
/* {{{ proto mixed fscanf(string str,string format, ...)
|
||||
implements a mostly ANSI compatible fscanf() . */
|
||||
PHP_FUNCTION(fscanf)
|
||||
{
|
||||
int result;
|
||||
pval **file_handle, **format_string;
|
||||
int len, type;
|
||||
char *buf;
|
||||
int issock=0;
|
||||
int socketd=0;
|
||||
void *what;
|
||||
|
||||
zval ***args;
|
||||
int argCount;
|
||||
PLS_FETCH();
|
||||
|
||||
argCount = ZEND_NUM_ARGS();
|
||||
if (argCount < 2) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
args = (zval ***)emalloc(argCount * sizeof(zval **));
|
||||
if (!args || (zend_get_parameters_array_ex(argCount,args) == FAILURE)) {
|
||||
efree( args );
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
|
||||
file_handle = args[0];
|
||||
format_string = args[1];
|
||||
|
||||
what = zend_fetch_resource(file_handle,-1,"File-Handle",&type,3,le_fopen,le_popen,le_socket);
|
||||
|
||||
/*
|
||||
* we can't do a ZEND_VERIFY_RESOURCE(what), otherwise we end up
|
||||
* with a leak if we have an invalid filehandle. This needs changing
|
||||
* if the code behind ZEND_VERIFY_RESOURCE changed. - cc
|
||||
*/
|
||||
if (!what) {
|
||||
efree(args);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
len = SCAN_MAX_FSCANF_BUFSIZE;
|
||||
|
||||
if (type == le_socket) {
|
||||
issock=1;
|
||||
socketd=*(int*)what;
|
||||
}
|
||||
buf = emalloc(sizeof(char) * (len + 1));
|
||||
/* needed because recv doesnt put a null at the end*/
|
||||
memset(buf,0,len+1);
|
||||
if (FP_FGETS(buf, len, socketd, (FILE*)what, issock) == NULL) {
|
||||
efree(buf);
|
||||
RETVAL_FALSE;
|
||||
} else {
|
||||
convert_to_string_ex( format_string );
|
||||
result = php_sscanf_internal( buf,(*format_string)->value.str.val,
|
||||
argCount,args, 2,&return_value);
|
||||
efree(args);
|
||||
efree(buf);
|
||||
if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
|
||||
WRONG_PARAM_COUNT
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto int fwrite(int fp, string str [, int length])
|
||||
Binary-safe file write */
|
||||
|
||||
|
@ -1858,6 +1930,8 @@ PHP_FUNCTION(realpath)
|
|||
}
|
||||
/* }}} */
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
|
||||
static fd_set readfd;
|
||||
|
|
|
@ -46,6 +46,7 @@ PHP_FUNCTION(feof);
|
|||
PHP_FUNCTION(fread);
|
||||
PHP_FUNCTION(fgetc);
|
||||
PHP_FUNCTION(fgets);
|
||||
PHP_FUNCTION(fscanf);
|
||||
PHP_FUNCTION(fgetss);
|
||||
PHP_FUNCTION(fgetcsv);
|
||||
PHP_FUNCTION(fwrite);
|
||||
|
|
|
@ -86,6 +86,7 @@ PHP_FUNCTION(substr_replace);
|
|||
PHP_FUNCTION(strnatcmp);
|
||||
PHP_FUNCTION(strnatcasecmp);
|
||||
PHP_FUNCTION(substr_count);
|
||||
PHP_FUNCTION(sscanf);
|
||||
|
||||
#define strnatcmp(a, b) \
|
||||
strnatcmp_ex(a, strlen(a), b, strlen(b), 0)
|
||||
|
|
1241
ext/standard/scanf.c
Normal file
1241
ext/standard/scanf.c
Normal file
File diff suppressed because it is too large
Load diff
48
ext/standard/scanf.h
Normal file
48
ext/standard/scanf.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP version 4.0 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.02 of the PHP license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available at through the world-wide-web at |
|
||||
| http://www.php.net/license/2_02.txt. |
|
||||
| If you did not receive a copy of the PHP license and are unable to |
|
||||
| obtain it through the world-wide-web, please send a note to |
|
||||
| license@php.net so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Authors: clayton collie <clcollie@mindspring.com> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
#ifndef _SCAN_H_
|
||||
#define _SCAN_H_
|
||||
|
||||
|
||||
#define SCAN_MAX_ARGS 0xFF /* Maximum number of variable which can be */
|
||||
/* passed to (f|s)scanf. This is an artifical */
|
||||
/* upper limit to keep resources in check and */
|
||||
/* minimize the possibility of exploits */
|
||||
|
||||
#define SCAN_MAX_FSCANF_BUFSIZE 512 /* Max input buffer allocated for fscanf */
|
||||
#define SCAN_SUCCESS SUCCESS
|
||||
#define SCAN_ERROR_EOF -1 /* indicates premature termination of scan */
|
||||
/* can be caused by bad parameters or format*/
|
||||
/* string. */
|
||||
#define SCAN_ERROR_INVALID_FORMAT (SCAN_ERROR_EOF - 1)
|
||||
#define SCAN_ERROR_VAR_PASSED_BYVAL (SCAN_ERROR_INVALID_FORMAT - 1)
|
||||
#define SCAN_ERROR_WRONG_PARAM_COUNT (SCAN_ERROR_VAR_PASSED_BYVAL - 1)
|
||||
#define SCAN_ERROR_INTERNAL (SCAN_ERROR_WRONG_PARAM_COUNT - 1)
|
||||
|
||||
|
||||
/*
|
||||
* The following are here solely for the benefit of the scanf type functions
|
||||
* e.g. fscanf
|
||||
*/
|
||||
PHPAPI int ValidateFormat(char *format, int numVars, int *totalVars);
|
||||
PHPAPI int php_sscanf_internal(char *string,char *format,int argCount,zval ***args,
|
||||
int varStart,pval **return_value);
|
||||
inline void scan_set_error_return(int numVars,pval **return_value);
|
||||
|
||||
|
||||
#endif /* ifndef _SCAN_PHP_ */
|
|
@ -30,6 +30,8 @@
|
|||
#ifdef HAVE_LOCALE_H
|
||||
# include <locale.h>
|
||||
#endif
|
||||
#include "scanf.h"
|
||||
#include "zend_API.h"
|
||||
#include "zend_execute.h"
|
||||
#include "php_globals.h"
|
||||
#include "basic_functions.h"
|
||||
|
@ -2562,6 +2564,48 @@ PHP_FUNCTION(substr_count)
|
|||
}
|
||||
/* }}} */
|
||||
|
||||
|
||||
/* {{{ proto mixed sscanf(string str,string format, ...)
|
||||
implements an ANSI compatible sscanf. */
|
||||
PHP_FUNCTION(sscanf)
|
||||
{
|
||||
zval **format;
|
||||
zval **literal;
|
||||
int result;
|
||||
zval ***args;
|
||||
int argCount;
|
||||
|
||||
|
||||
argCount = ZEND_NUM_ARGS();
|
||||
if (argCount < 2) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
args = (zval ***)emalloc(argCount * sizeof(zval **));
|
||||
if (!args || (zend_get_parameters_array_ex(argCount,args) == FAILURE)) {
|
||||
efree( args );
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
|
||||
literal = args[0];
|
||||
format = args[1];
|
||||
|
||||
convert_to_string_ex( format );
|
||||
convert_to_string_ex( literal );
|
||||
|
||||
result = php_sscanf_internal( (*literal)->value.str.val,
|
||||
(*format)->value.str.val,
|
||||
argCount,args,
|
||||
2,&return_value);
|
||||
efree(args);
|
||||
|
||||
if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
|
||||
WRONG_PARAM_COUNT
|
||||
}
|
||||
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue