fix device_name() to get device name from host, not from termux

This commit is contained in:
Lizonghang 2025-01-24 16:56:23 +04:00
parent 1c0087e919
commit 4948b1004c

View file

@ -48,6 +48,20 @@
#include <dirent.h> #include <dirent.h>
static const char * get_uname_os() {
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("uname -o", "r"), pclose);
if (!pipe) {
return "Unknown";
}
static char buffer[16];
if (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) {
buffer[strcspn(buffer, "\n")] = '\0';
return buffer;
}
return "Unknown";
}
const char * device_name() { const char * device_name() {
static char device_name[256]; static char device_name[256];
@ -57,8 +71,20 @@ const char * device_name() {
strncpy(device_name, "Unknown Windows Device", sizeof(device_name)); strncpy(device_name, "Unknown Windows Device", sizeof(device_name));
} }
#elif defined(__linux__) #elif defined(__linux__)
if (gethostname(device_name, sizeof(device_name)) != 0) { const char * os = get_uname_os();
strncpy(device_name, "Unknown Linux Device", sizeof(device_name)); if (strstr(os, "Android") != nullptr) {
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("getprop ro.product.model", "r"), pclose);
if (pipe) {
if (fgets(device_name, sizeof(device_name), pipe.get()) != nullptr) {
device_name[strcspn(device_name, "\n")] = '\0';
return device_name;
}
}
strncpy(device_name, "Unknown Device", sizeof(device_name));
} else {
if (gethostname(device_name, sizeof(device_name)) != 0) {
strncpy(device_name, "Unknown Device", sizeof(device_name));
}
} }
#elif defined(__APPLE__) && defined(__MACH__) #elif defined(__APPLE__) && defined(__MACH__)
if (gethostname(device_name, sizeof(device_name)) != 0) { if (gethostname(device_name, sizeof(device_name)) != 0) {
@ -75,8 +101,9 @@ const char * device_os() {
#ifdef _WIN32 #ifdef _WIN32
return "Windows"; return "Windows";
#elif __linux__ #elif __linux__
if (std::getenv("ANDROID_ROOT") != nullptr && std::getenv("PREFIX") != nullptr) { const char * os = get_uname_os();
return "Android"; // Termux env in Android if (strstr(os, "Android") != nullptr) {
return "Android";
} }
return "Linux"; return "Linux";
#elif __APPLE__ || __MACH__ #elif __APPLE__ || __MACH__