Skip to content

ext/standard/info.c: Various refactoring #15366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 24 additions & 31 deletions ext/standard/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static char* php_get_windows_name()
OSVERSIONINFOEX osvi = EG(windows_version_info);
SYSTEM_INFO si;
DWORD dwType;
char *major = NULL, *sub = NULL, *retval;
const char *major = NULL, *sub = NULL;

ZeroMemory(&si, sizeof(SYSTEM_INFO));

Expand Down Expand Up @@ -296,19 +296,19 @@ static char* php_get_windows_name()
}
} else if (VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && osvi.dwMajorVersion >= 6) {
if (osvi.dwMajorVersion == 6) {
if( osvi.dwMinorVersion == 0 ) {
if( osvi.wProductType == VER_NT_WORKSTATION ) {
if (osvi.dwMinorVersion == 0) {
if (osvi.wProductType == VER_NT_WORKSTATION) {
major = "Windows Vista";
} else {
major = "Windows Server 2008";
}
} else if ( osvi.dwMinorVersion == 1 ) {
if( osvi.wProductType == VER_NT_WORKSTATION ) {
} else if (osvi.dwMinorVersion == 1) {
if (osvi.wProductType == VER_NT_WORKSTATION) {
major = "Windows 7";
} else {
major = "Windows Server 2008 R2";
}
} else if ( osvi.dwMinorVersion == 2 ) {
} else if (osvi.dwMinorVersion == 2) {
/* could be Windows 8/Windows Server 2012, could be Windows 8.1/Windows Server 2012 R2 */
/* XXX and one more X - the above comment is true if no manifest is used for two cases:
- if the PHP build doesn't use the correct manifest
Expand All @@ -333,20 +333,20 @@ static char* php_get_windows_name()
VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR,
dwlConditionMask)) {
osvi.dwMinorVersion = 3; /* Windows 8.1/Windows Server 2012 R2 */
if( osvi.wProductType == VER_NT_WORKSTATION ) {
if (osvi.wProductType == VER_NT_WORKSTATION) {
major = "Windows 8.1";
} else {
major = "Windows Server 2012 R2";
}
} else {
if( osvi.wProductType == VER_NT_WORKSTATION ) {
if (osvi.wProductType == VER_NT_WORKSTATION) {
major = "Windows 8";
} else {
major = "Windows Server 2012";
}
}
} else if (osvi.dwMinorVersion == 3) {
if( osvi.wProductType == VER_NT_WORKSTATION ) {
if (osvi.wProductType == VER_NT_WORKSTATION) {
major = "Windows 8.1";
} else {
major = "Windows Server 2012 R2";
Expand Down Expand Up @@ -602,9 +602,10 @@ static char* php_get_windows_name()
}
}
} else {
return NULL;
ZEND_UNREACHABLE();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is fine given that this PR targets master.

}

char *retval;
spprintf(&retval, 0, "%s%s%s%s%s", major, sub?" ":"", sub?sub:"", osvi.szCSDVersion[0] != '\0'?" ":"", osvi.szCSDVersion);
return retval;
}
Expand Down Expand Up @@ -659,8 +660,8 @@ static void php_get_windows_cpu(char *buf, size_t bufsize)
PHPAPI zend_string *php_get_uname(char mode)
{
char *php_uname;
char tmp_uname[256];
#ifdef PHP_WIN32
char tmp_uname[256];
DWORD dwBuild=0;
DWORD dwVersion = GetVersion();
DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
Expand All @@ -673,21 +674,18 @@ PHPAPI zend_string *php_get_uname(char mode)
if (mode == 's') {
php_uname = "Windows NT";
} else if (mode == 'r') {
snprintf(tmp_uname, sizeof(tmp_uname), "%d.%d", dwWindowsMajorVersion, dwWindowsMinorVersion);
php_uname = tmp_uname;
return strpprintf(0, "%d.%d", dwWindowsMajorVersion, dwWindowsMinorVersion);
} else if (mode == 'n') {
php_uname = ComputerName;
} else if (mode == 'v') {
char *winver = php_get_windows_name();
dwBuild = (DWORD)(HIWORD(dwVersion));
if(winver == NULL) {
snprintf(tmp_uname, sizeof(tmp_uname), "build %d", dwBuild);
if (winver == NULL) {
return strpprintf(0, "build %d", dwBuild);
} else {
snprintf(tmp_uname, sizeof(tmp_uname), "build %d (%s)", dwBuild, winver);
}
php_uname = tmp_uname;
if(winver) {
zend_string *build_with_version = strpprintf(0, "build %d (%s)", dwBuild, winver);
efree(winver);
return build_with_version;
}
} else if (mode == 'm') {
php_get_windows_cpu(tmp_uname, sizeof(tmp_uname));
Expand All @@ -703,18 +701,16 @@ PHPAPI zend_string *php_get_uname(char mode)

/* Windows "version" 6.2 could be Windows 8/Windows Server 2012, but also Windows 8.1/Windows Server 2012 R2 */
if (dwWindowsMajorVersion == 6 && dwWindowsMinorVersion == 2) {
if (strncmp(winver, "Windows 8.1", 11) == 0 || strncmp(winver, "Windows Server 2012 R2", 22) == 0) {
if (strncmp(winver, "Windows 8.1", strlen("Windows 8.1")) == 0 || strncmp(winver, "Windows Server 2012 R2", strlen("Windows Server 2012 R2")) == 0) {
dwWindowsMinorVersion = 3;
}
}

snprintf(tmp_uname, sizeof(tmp_uname), "%s %s %d.%d build %d (%s) %s",
"Windows NT", ComputerName,
dwWindowsMajorVersion, dwWindowsMinorVersion, dwBuild, winver?winver:"unknown", wincpu);
if(winver) {
efree(winver);
}
php_uname = tmp_uname;
zend_string *build_with_all_info = strpprintf(0, "%s %s %d.%d build %d (%s) %s",
"Windows NT", ComputerName, dwWindowsMajorVersion, dwWindowsMinorVersion, dwBuild,
winver ? winver: "unknown", wincpu);
efree(winver);
return build_with_all_info;
}
#else
#ifdef HAVE_SYS_UTSNAME_H
Expand All @@ -733,10 +729,7 @@ PHPAPI zend_string *php_get_uname(char mode)
} else if (mode == 'm') {
php_uname = buf.machine;
} else { /* assume mode == 'a' */
snprintf(tmp_uname, sizeof(tmp_uname), "%s %s %s %s %s",
buf.sysname, buf.nodename, buf.release, buf.version,
buf.machine);
php_uname = tmp_uname;
return strpprintf(0, "%s %s %s %s %s", buf.sysname, buf.nodename, buf.release, buf.version, buf.machine);
}
}
#else
Expand Down
Loading