Skip to content

Commit

Permalink
changes for v1.10.11
Browse files Browse the repository at this point in the history
  • Loading branch information
ashnazg committed Aug 10, 2021
1 parent 6f59118 commit 68d0d32
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 99 deletions.
248 changes: 150 additions & 98 deletions src/OS/Guess.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*
* PHP versions 4 and 5
*
* @category pear
* @package PEAR
* @author Stig Bakken <ssb@php.net>
* @author Gregory Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @link http://pear.php.net/package/PEAR
* @since File available since PEAR 0.1
* @category pear
* @package PEAR
* @author Stig Bakken <ssb@php.net>
* @author Gregory Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @link http://pear.php.net/package/PEAR
* @since File available since PEAR 0.1
*/

// {{{ uname examples
Expand Down Expand Up @@ -80,15 +80,15 @@
*
* This class uses php_uname() to grok information about the current OS
*
* @category pear
* @package PEAR
* @author Stig Bakken <ssb@php.net>
* @author Gregory Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/PEAR
* @since Class available since Release 0.1
* @category pear
* @package PEAR
* @author Stig Bakken <ssb@php.net>
* @author Gregory Beaver <cellog@php.net>
* @copyright 1997-2020 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/PEAR
* @since Class available since Release 0.1
*/
class OS_Guess
{
Expand Down Expand Up @@ -138,13 +138,9 @@ function parseSignature($uname = null)
$release = "$parts[3].$parts[2]";
break;
case 'Windows' :
switch ($parts[1]) {
case '95/98':
$release = '9x';
break;
default:
$release = $parts[1];
break;
$release = $parts[1];
if ($release == '95/98') {
$release = '9x';
}
$cpu = 'i386';
break;
Expand All @@ -157,18 +153,10 @@ function parseSignature($uname = null)
$sysname = 'darwin';
$nodename = $parts[2];
$release = $parts[3];
if ($cpu == 'Macintosh') {
if ($parts[$n - 2] == 'Power') {
$cpu = 'powerpc';
}
}
$cpu = $this->_determineIfPowerpc($cpu, $parts);
break;
case 'Darwin' :
if ($cpu == 'Macintosh') {
if ($parts[$n - 2] == 'Power') {
$cpu = 'powerpc';
}
}
$cpu = $this->_determineIfPowerpc($cpu, $parts);
$release = preg_replace('/^([0-9]+\.[0-9]+).*/', '\1', $parts[2]);
break;
default:
Expand All @@ -187,6 +175,15 @@ function parseSignature($uname = null)
return array($sysname, $release, $cpu, $extra, $nodename);
}

function _determineIfPowerpc($cpu, $parts)
{
$n = count($parts);
if ($cpu == 'Macintosh' && $parts[$n - 2] == 'Power') {
$cpu = 'powerpc';
}
return $cpu;
}

function _detectGlibcVersion()
{
static $glibc = false;
Expand All @@ -196,80 +193,131 @@ function _detectGlibcVersion()
$major = $minor = 0;
include_once "System.php";

if (@is_link('/lib64/libc.so.6')) {
// Let's try reading the libc.so.6 symlink
if (preg_match('/^libc-(.*)\.so$/', basename(readlink('/lib64/libc.so.6')), $matches)) {
list($major, $minor) = explode('.', $matches[1]);
}
} else if (@is_link('/lib/libc.so.6')) {
// Let's try reading the libc.so.6 symlink
if (preg_match('/^libc-(.*)\.so$/', basename(readlink('/lib/libc.so.6')), $matches)) {
list($major, $minor) = explode('.', $matches[1]);
// Let's try reading possible libc.so.6 symlinks
$libcs = array(
'/lib64/libc.so.6',
'/lib/libc.so.6',
'/lib/i386-linux-gnu/libc.so.6'
);
$versions = array();
foreach ($libcs as $file) {
$versions = $this->_readGlibCVersionFromSymlink($file);
if ($versions != []) {
list($major, $minor) = $versions;
break;
}
}

// Use glibc's <features.h> header file to
// get major and minor version number:
if (!($major && $minor) &&
@file_exists('/usr/include/features.h') &&
@is_readable('/usr/include/features.h')) {
if (!@file_exists('/usr/bin/cpp') || !@is_executable('/usr/bin/cpp')) {
$features_file = fopen('/usr/include/features.h', 'rb');
while (!feof($features_file)) {
$line = fgets($features_file, 8192);
if (!$line || (strpos($line, '#define') === false)) {
continue;
}
if (strpos($line, '__GLIBC__')) {
// major version number #define __GLIBC__ version
$line = preg_split('/\s+/', $line);
$glibc_major = trim($line[2]);
if (isset($glibc_minor)) {
break;
}
continue;
}

if (strpos($line, '__GLIBC_MINOR__')) {
// got the minor version number
// #define __GLIBC_MINOR__ version
$line = preg_split('/\s+/', $line);
$glibc_minor = trim($line[2]);
if (isset($glibc_major)) {
break;
}
continue;
}
}
fclose($features_file);
if (!isset($glibc_major) || !isset($glibc_minor)) {
return $glibc = '';
}
return $glibc = 'glibc' . trim($glibc_major) . "." . trim($glibc_minor) ;
} // no cpp

$tmpfile = System::mktemp("glibctest");
$fp = fopen($tmpfile, "w");
fwrite($fp, "#include <features.h>\n__GLIBC__ __GLIBC_MINOR__\n");
fclose($fp);
$cpp = popen("/usr/bin/cpp $tmpfile", "r");
while ($line = fgets($cpp, 1024)) {
if ($line[0] == '#' || trim($line) == '') {
continue;
if (!($major && $minor)) {
$versions = $this->_readGlibCVersionFromFeaturesHeaderFile();
}
if (is_array($versions) && $versions != []) {
list($major, $minor) = $versions;
}

if (!($major && $minor)) {
return $glibc = '';
}

return $glibc = "glibc{$major}.{$minor}";
}

function _readGlibCVersionFromSymlink($file)
{
$versions = array();
if (@is_link($file)
&& (preg_match('/^libc-(.*)\.so$/', basename(readlink($file)), $matches))
) {
$versions = explode('.', $matches[1]);
}
return $versions;
}


function _readGlibCVersionFromFeaturesHeaderFile()
{
$features_header_file = '/usr/include/features.h';
if (!(@file_exists($features_header_file)
&& @is_readable($features_header_file))
) {
return array();
}
if (!@file_exists('/usr/bin/cpp') || !@is_executable('/usr/bin/cpp')) {
return $this-_parseFeaturesHeaderFile($features_header_file);
} // no cpp

return $this->_fromGlibCTest();
}

function _parseFeaturesHeaderFile($features_header_file)
{
$features_file = fopen($features_header_file, 'rb');
while (!feof($features_file)) {
$line = fgets($features_file, 8192);
if (!$this->_IsADefinition($line)) {
continue;
}
if (strpos($line, '__GLIBC__')) {
// major version number #define __GLIBC__ version
$line = preg_split('/\s+/', $line);
$glibc_major = trim($line[2]);
if (isset($glibc_minor)) {
break;
}
continue;
}

if (list($major, $minor) = explode(' ', trim($line))) {
if (strpos($line, '__GLIBC_MINOR__')) {
// got the minor version number
// #define __GLIBC_MINOR__ version
$line = preg_split('/\s+/', $line);
$glibc_minor = trim($line[2]);
if (isset($glibc_major)) {
break;
}
}
pclose($cpp);
unlink($tmpfile);
} // features.h
}
fclose($features_file);
if (!isset($glibc_major) || !isset($glibc_minor)) {
return array();
}
return array(trim($glibc_major), trim($glibc_minor));
}

if (!($major && $minor)) {
return $glibc = '';
function _IsADefinition($line)
{
if ($line === false) {
return false;
}
return strpos(trim($line), '#define') !== false;
}

return $glibc = "glibc{$major}.{$minor}";
function _fromGlibCTest()
{
$major = null;
$minor = null;

$tmpfile = System::mktemp("glibctest");
$fp = fopen($tmpfile, "w");
fwrite($fp, "#include <features.h>\n__GLIBC__ __GLIBC_MINOR__\n");
fclose($fp);
$cpp = popen("/usr/bin/cpp $tmpfile", "r");
while ($line = fgets($cpp, 1024)) {
if ($line[0] == '#' || trim($line) == '') {
continue;
}

if (list($major, $minor) = explode(' ', trim($line))) {
break;
}
}
pclose($cpp);
unlink($tmpfile);
if ($major !== null && $minor !== null) {
return [$major, $minor];
}
}

function getSignature()
Expand Down Expand Up @@ -328,12 +376,16 @@ function matchSignature($match)
function _matchFragment($fragment, $value)
{
if (strcspn($fragment, '*?') < strlen($fragment)) {
$reg = '/^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '\\z/';
$expression = str_replace(
array('*', '?', '/'),
array('.*', '.', '\\/'),
$fragment
);
$reg = '/^' . $expression . '\\z/';
return preg_match($reg, $value);
}
return ($fragment == '*' || !strcasecmp($fragment, $value));
}

}
/*
* Local Variables:
Expand Down
4 changes: 3 additions & 1 deletion src/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ public static function which($program, $fallback = false)
// It's possible to run a .bat on Windows that is_executable
// would return false for. The is_executable check is meaningless...
if (OS_WINDOWS) {
return $file;
if (file_exists($file)) {
return $file;
}
} else {
if (is_executable($file)) {
return $file;
Expand Down

0 comments on commit 68d0d32

Please sign in to comment.