Skip to content

Commit afc5738

Browse files
authored
Show build provider and unify version information printing (php#14657)
* Show build provider information in "php -v" Vendors such as distributions can set the `PHP_BUILD_PROVIDER` variable, that gets printed in phpinfo. However, I find that users check `php -v` more often than phpinfo to see what PHP they're running. The problem with this is that it does not show that build provider information. This change makes the build provider information printed on an additional line of the version information. * Put on same line so it works with or without env var Unbreaks build without PHP_BUILD_PROVIDER set. * change wording in provider version text better grammatically; many different possibilities here though * Unify SAPI version printing This makes it so that all of the SAPIs share the same code for printing version information. This is useful in case of any future changes to the version information, such as i.e. adding build provider to the output. * Make include for php_print_version explicit * Preserve phpdbg version and output channel php_printf doesn't have same semantics, as phpdbg_out could be on a different output than stdout/err. Also add the phpdbg version (in case it differs from PHP's, to keep similar output before this PR) * remove size variables we don't use them and CI doesn't like unused variables * Fix format string insecurity
1 parent 225034d commit afc5738

File tree

9 files changed

+62
-47
lines changed

9 files changed

+62
-47
lines changed

main/main.c

+41
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,47 @@ PHPAPI unsigned int php_version_id(void)
108108
return PHP_VERSION_ID;
109109
}
110110

111+
PHPAPI char *php_get_version(sapi_module_struct *sapi_module)
112+
{
113+
char *version_info;
114+
spprintf(&version_info, 0, "PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s",
115+
PHP_VERSION, sapi_module->name, __DATE__, __TIME__,
116+
#ifdef ZTS
117+
"ZTS"
118+
#else
119+
"NTS"
120+
#endif
121+
#ifdef PHP_BUILD_COMPILER
122+
" " PHP_BUILD_COMPILER
123+
#endif
124+
#ifdef PHP_BUILD_ARCH
125+
" " PHP_BUILD_ARCH
126+
#endif
127+
#if ZEND_DEBUG
128+
" DEBUG"
129+
#endif
130+
#ifdef HAVE_GCOV
131+
" GCOV"
132+
#endif
133+
,
134+
#ifdef PHP_BUILD_PROVIDER
135+
"Built by " PHP_BUILD_PROVIDER "\n"
136+
#else
137+
""
138+
#endif
139+
,
140+
get_zend_version()
141+
);
142+
return version_info;
143+
}
144+
145+
PHPAPI void php_print_version(sapi_module_struct *sapi_module)
146+
{
147+
char *version_info = php_get_version(sapi_module);
148+
php_printf("%s", version_info);
149+
efree(version_info);
150+
}
151+
111152
/* {{{ PHP_INI_MH */
112153
static PHP_INI_MH(OnSetFacility)
113154
{

main/php_main.h

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ PHPAPI const char *php_version(void);
3636
*/
3737
PHPAPI unsigned int php_version_id(void);
3838

39+
/* Prints the PHP version string for the -v option. It's in main/ so that
40+
* it can be shared between SAPIs.
41+
*/
42+
PHPAPI char *php_get_version(sapi_module_struct *sapi_module);
43+
PHPAPI void php_print_version(sapi_module_struct *sapi_module);
44+
3945
PHPAPI zend_result php_request_startup(void);
4046
PHPAPI void php_request_shutdown(void *dummy);
4147
PHPAPI zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module);

sapi/cgi/cgi_main.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -2384,11 +2384,7 @@ consult the installation file that came with this distribution, or visit \n\
23842384
}
23852385
SG(headers_sent) = 1;
23862386
SG(request_info).no_headers = 1;
2387-
#if ZEND_DEBUG
2388-
php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
2389-
#else
2390-
php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
2391-
#endif
2387+
php_print_version(&cgi_sapi_module);
23922388
php_request_shutdown((void *) 0);
23932389
fcgi_shutdown();
23942390
exit_status = 0;

sapi/cgi/tests/001.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ echo "Done\n";
1717
--EXPECTF--
1818
string(%d) "PHP %s (cgi%s (built: %s
1919
Copyright (c) The PHP Group
20-
Zend Engine v%s, Copyright (c) Zend Technologies
20+
%AZend Engine v%s, Copyright (c) Zend Technologies
2121
"
2222
Done

sapi/cli/php_cli.c

+1-22
Original file line numberDiff line numberDiff line change
@@ -625,28 +625,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
625625
goto out;
626626

627627
case 'v': /* show php version & quit */
628-
php_printf("PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s",
629-
PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__,
630-
#ifdef ZTS
631-
"ZTS"
632-
#else
633-
"NTS"
634-
#endif
635-
#ifdef PHP_BUILD_COMPILER
636-
" " PHP_BUILD_COMPILER
637-
#endif
638-
#ifdef PHP_BUILD_ARCH
639-
" " PHP_BUILD_ARCH
640-
#endif
641-
#if ZEND_DEBUG
642-
" DEBUG"
643-
#endif
644-
#ifdef HAVE_GCOV
645-
" GCOV"
646-
#endif
647-
,
648-
get_zend_version()
649-
);
628+
php_print_version(&cli_sapi_module);
650629
sapi_deactivate();
651630
goto out;
652631

sapi/cli/tests/001.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ echo "Done\n";
1414
--EXPECTF--
1515
string(%d) "PHP %s (cli) (built: %s)%s
1616
Copyright (c) The PHP Group
17-
Zend Engine v%s, Copyright (c) Zend Technologies
17+
%AZend Engine v%s, Copyright (c) Zend Technologies
1818
"
1919
Done

sapi/fpm/fpm/fpm_main.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -1705,11 +1705,7 @@ int main(int argc, char *argv[])
17051705
SG(headers_sent) = 1;
17061706
SG(request_info).no_headers = 1;
17071707

1708-
#if ZEND_DEBUG
1709-
php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
1710-
#else
1711-
php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
1712-
#endif
1708+
php_print_version(&sapi_module);
17131709
php_request_shutdown((void *) 0);
17141710
fcgi_shutdown();
17151711
exit_status = FPM_EXIT_OK;

sapi/litespeed/lsapi_main.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -1272,11 +1272,7 @@ static int cli_main( int argc, char * argv[] )
12721272
break;
12731273
case 'v':
12741274
if (php_request_startup() != FAILURE) {
1275-
#if ZEND_DEBUG
1276-
php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
1277-
#else
1278-
php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
1279-
#endif
1275+
php_print_version(&sapi_module);
12801276
#ifdef PHP_OUTPUT_NEWAPI
12811277
php_output_end_all();
12821278
#else

sapi/phpdbg/phpdbg.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "phpdbg_arginfo.h"
3131
#include "zend_vm.h"
3232
#include "php_ini_builder.h"
33+
#include "php_main.h"
3334

3435
#include "ext/standard/basic_functions.h"
3536

@@ -1369,14 +1370,14 @@ int main(int argc, char **argv) /* {{{ */
13691370
if (show_help) {
13701371
phpdbg_do_help_cmd(exec);
13711372
} else if (show_version) {
1372-
phpdbg_out(
1373-
"phpdbg %s (built: %s %s)\nPHP %s, Copyright (c) The PHP Group\n%s",
1374-
PHPDBG_VERSION,
1375-
__DATE__,
1376-
__TIME__,
1377-
PHP_VERSION,
1378-
get_zend_version()
1379-
);
1373+
char *version_info = php_get_version(&phpdbg_sapi_module);
1374+
/* we also want to include phpdbg version */
1375+
char *prepended_version_info;
1376+
spprintf(&prepended_version_info, 0,
1377+
"phpdbg %s, %s", PHPDBG_VERSION, version_info);
1378+
phpdbg_out("%s", prepended_version_info);
1379+
efree(prepended_version_info);
1380+
efree(version_info);
13801381
}
13811382
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
13821383
php_module_shutdown();

0 commit comments

Comments
 (0)