Skip to content

Commit 851e051

Browse files
committed
fixed #8 chnage return type of filebin fucntion
1 parent 9a96533 commit 851e051

9 files changed

+28
-22
lines changed

README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ extension = /path/filebin.so
3838

3939
## Proto types:
4040
```php
41-
filebin (string path, int flag = MAGIC_NONE, string magic_path = MIGIC_FILE): string|NULL
42-
filebin (string path, string magic_path = MIGIC_FILE, int flag = MAGIC_NONE): string|NULL
41+
filebin (string path, int flag = MAGIC_NONE, string magic_path = MIGIC_FILE): string|false
42+
filebin (string path, string magic_path = MIGIC_FILE, int flag = MAGIC_NONE): string|false
4343
```
4444

4545
The 2th argument and the 3th argument can be swapped with each other.
4646

47+
**Return value :**
48+
if success, return mime strings.
49+
if failure, return false.
50+
51+
4752
## Constant
4853
Name | Value
4954
-- | --
@@ -87,7 +92,7 @@ Name | Description
8792
**Most common usage** :
8893
```php
8994
<?php
90-
if ( ($buf = filebin ('modules/filebin.so')) != null )
95+
if ( ($buf = filebin ('modules/filebin.so')) != false )
9196
echo "$buf\n";
9297
?>
9398

@@ -98,7 +103,7 @@ ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, Buil
98103
**Execute with custom MAGIC file** :
99104
```php
100105
<?php
101-
if ( ($buf = filebin ('modules/filebin.so', '/usr/share/misc/magic.mgc')) != null )
106+
if ( ($buf = filebin ('modules/filebin.so', '/usr/share/misc/magic.mgc')) != false )
102107
echo "$buf\n";
103108
?>
104109

@@ -108,7 +113,7 @@ ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, Buil
108113

109114
**Get only file mime type** :
110115
```php
111-
if ( ($buf = filebin ('modules/filebin.so', MAGIC_MIME_ENCODING)) != null )
116+
if ( ($buf = filebin ('modules/filebin.so', MAGIC_MIME_ENCODING)) != false )
112117
echo "$buf\n";
113118
?>
114119

@@ -118,7 +123,7 @@ binary
118123

119124
**Get only file mime type with custom MAGIC file** :
120125
```php
121-
if ( ($buf = filebin ('modules/filebin.so', MAGIC_MIME, '/usr/share/misc/magic.mgc')) != null )
126+
if ( ($buf = filebin ('modules/filebin.so', MAGIC_MIME, '/usr/share/misc/magic.mgc')) != false )
122127
echo "$buf\n";
123128
?>
124129

@@ -128,7 +133,7 @@ application/x-sharedlib; charset=binary
128133

129134
**Can enable to swap 2th and 3th argument each other** :
130135
```php
131-
if ( ($buf = filebin ('modules/filebin.so', MAGIC_MIME, '/usr/share/misc/magic.mgc')) != null )
136+
if ( ($buf = filebin ('modules/filebin.so', MAGIC_MIME, '/usr/share/misc/magic.mgc')) != false )
132137
echo "$buf\n";
133138
?>
134139

filebin.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ PHP_MINIT_FUNCTION(filebin)
151151
}
152152
/* }}} */
153153

154-
/* {{{ proto (int|null) filebin(string path, array args_arr, int argc)
154+
/* {{{ proto filebin(string path[, int flag[, string magic-path]]): string|false
155+
* proto filebin(string path[, string magic-path[, int flag]]): string|false
155156
*/
156157
PHP_FUNCTION(filebin) {
157158
zval * zflag;
@@ -183,12 +184,12 @@ PHP_FUNCTION(filebin) {
183184

184185
if ( ZSTR_LEN (path) == 0 ) {
185186
php_error (E_WARNING, "The value of 1st argument was empty.");
186-
RETURN_NULL ();
187+
RETURN_FALSE;
187188
}
188189

189190
if ( stat (ZSTR_VAL (path), &filestat) != 0 ) {
190191
php_error (E_WARNING, "%s file not found.", ZSTR_VAL (path));
191-
RETURN_NULL ();
192+
RETURN_FALSE;
192193
}
193194

194195
if ( fargs == 2 ) {
@@ -204,7 +205,7 @@ PHP_FUNCTION(filebin) {
204205
break;
205206
default :
206207
php_error (E_WARNING, "2th argument is only available for integer(flag) or MAGIC file path.");
207-
RETURN_NULL ();
208+
RETURN_FALSE;
208209
}
209210
} else if ( fargs == 3 ) {
210211
if ( Z_TYPE_P (zflag) == IS_LONG && Z_TYPE_P(zpath) == IS_STRING ) {
@@ -215,7 +216,7 @@ PHP_FUNCTION(filebin) {
215216
mpath = Z_STRLEN_P (zflag) ? Z_STRVAL_P (zflag) : MAGIC;
216217
} else {
217218
php_error (E_WARNING, "The 2th and 3th argument can only be integer or strings.");
218-
RETURN_NULL ();
219+
RETURN_FALSE;
219220
}
220221
}
221222

@@ -225,19 +226,19 @@ PHP_FUNCTION(filebin) {
225226
magic = magic_open (flag);
226227
if ( magic == NULL ) {
227228
php_error (E_WARNING, strerror (errno));
228-
RETURN_NULL ();
229+
RETURN_FALSE;
229230
}
230231

231232
if ( magic_load (magic, mpath) == -1 ) {
232233
php_error (E_WARNING, magic_error (magic));
233234
magic_close (magic);
234-
RETURN_NULL ();
235+
RETURN_FALSE;
235236
}
236237

237238
if ( (type = magic_file (magic, ZSTR_VAL (path))) == NULL ) {
238239
php_error (E_WARNING, magic_error(magic));
239240
magic_close (magic);
240-
RETURN_NULL ();
241+
RETURN_FALSE;
241242
}
242243

243244
RETVAL_STRING (type);

filebin.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
/** @generate-function-entries **/
44

5-
function filebin(string $path, int|string $flag = MAGIC_NONE, string|int $magic_path = MAGIC_FILE): string|null { }
5+
function filebin(string $path, int|string $flag = MAGIC_NONE, string|int $magic_path = MAGIC_FILE): string|false { }

filebin_arginfo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Stub hash: d70564e573467b0001913faa6501b6bfbbf066a2 */
33

44
#if PHP_VERSION_ID >= 80000
5-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filebin, 0, 1, MAY_BE_STRING|MAY_BE_NULL)
5+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filebin, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
66
ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 0)
77
ZEND_ARG_TYPE_MASK(0, flag, MAY_BE_LONG|MAY_BE_STRING, "MAGIC_NONE or MAGIC_FILE")
88
ZEND_ARG_TYPE_MASK(0, magic_path, MAY_BE_STRING|MAY_BE_LONG, "MAGIC_FILE or MAGIC_NONE")

tests/002-1arg.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if ( ! extension_loaded ('filebin') ) {
1212
--FILE--
1313
<?php
1414

15-
if ( filebin ('modules/filebin.so') == null )
15+
if ( filebin ('modules/filebin.so') == false )
1616
echo 'skip';
1717
else
1818
echo "filebin ('modules/filebin.so') success";

tests/003-user-magicfile.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if ( ! extension_loaded ('filebin') ) {
1111
--INI--
1212
--FILE--
1313
<?php
14-
if ( filebin ('modules/filebin.so', '/usr/share/misc/magic.mgc') == null )
14+
if ( filebin ('modules/filebin.so', '/usr/share/misc/magic.mgc') == false )
1515
echo 'skip';
1616
else
1717
echo "using user magic file success";

tests/004-with-flag.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if ( ! extension_loaded ('filebin') ) {
1212
--FILE--
1313
<?php
1414

15-
if ( filebin ('modules/filebin.so', MAGIC_MIME_ENCODING) == null )
15+
if ( filebin ('modules/filebin.so', MAGIC_MIME_ENCODING) == false )
1616
echo 'skip';
1717
else
1818
echo "using with MAGIC_MIME_ENCODING flag success";

tests/005-flag-magicfile.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if ( ! extension_loaded ('filebin') ) {
1212
--FILE--
1313
<?php
1414

15-
if ( filebin ('modules/filebin.so', MAGIC_MIME, '/usr/share/misc/magic.mgc') == null )
15+
if ( filebin ('modules/filebin.so', MAGIC_MIME, '/usr/share/misc/magic.mgc') == false )
1616
echo 'skip';
1717
else
1818
echo "success";

tests/006-magicfile-flag.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if ( ! extension_loaded ('filebin') ) {
1212
--FILE--
1313
<?php
1414

15-
if ( filebin ('modules/filebin.so', '/usr/share/misc/magic.mgc', MAGIC_MIME) == null )
15+
if ( filebin ('modules/filebin.so', '/usr/share/misc/magic.mgc', MAGIC_MIME) == false )
1616
echo 'skip';
1717
else
1818
echo "success";

0 commit comments

Comments
 (0)