Skip to content

Commit

Permalink
OS: prefer pretty names
Browse files Browse the repository at this point in the history
Hopefully we can end the controversy.

Fix #1483 #1038
  • Loading branch information
CarterLi committed Jan 6, 2025
1 parent 88ebeb2 commit 6429c20
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 54 deletions.
2 changes: 0 additions & 2 deletions src/detection/os/os_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ void ffDetectOSImpl(FFOSResult* os)
{
ffStrbufSetStatic(&os->name, "Android");

ffStrbufSetStatic(&os->prettyName, "Android");

ffStrbufSetStatic(&os->id, "android");

ffSettingsGetAndroidProperty("ro.build.version.release", &os->version);
Expand Down
24 changes: 11 additions & 13 deletions src/detection/os/os_apple.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ static bool detectOSCodeName(FFOSResult* os)
case 14: ffStrbufSetStatic(&os->codename, "Mojave"); return true;
case 13: ffStrbufSetStatic(&os->codename, "High Sierra"); return true;
case 12: ffStrbufSetStatic(&os->codename, "Sierra"); return true;
case 11: ffStrbufSetStatic(&os->codename, "El Capitan"); ffStrbufSetStatic(&os->prettyName, "OS X"); return true;
case 10: ffStrbufSetStatic(&os->codename, "Yosemite"); ffStrbufSetStatic(&os->prettyName, "OS X"); return true;
case 9: ffStrbufSetStatic(&os->codename, "Mavericks"); ffStrbufSetStatic(&os->prettyName, "OS X"); return true;
case 8: ffStrbufSetStatic(&os->codename, "Mountain Lion"); ffStrbufSetStatic(&os->prettyName, "OS X"); return true;
case 7: ffStrbufSetStatic(&os->codename, "Lion"); ffStrbufSetStatic(&os->prettyName, "Mac OS X"); return true;
case 6: ffStrbufSetStatic(&os->codename, "Snow Leopard"); ffStrbufSetStatic(&os->prettyName, "Mac OS X"); return true;
case 5: ffStrbufSetStatic(&os->codename, "Leopard"); ffStrbufSetStatic(&os->prettyName, "Mac OS X"); return true;
case 4: ffStrbufSetStatic(&os->codename, "Tiger"); ffStrbufSetStatic(&os->prettyName, "Mac OS X"); return true;
case 11: ffStrbufSetStatic(&os->codename, "El Capitan"); return true;
case 10: ffStrbufSetStatic(&os->codename, "Yosemite"); return true;
case 9: ffStrbufSetStatic(&os->codename, "Mavericks"); return true;
case 8: ffStrbufSetStatic(&os->codename, "Mountain Lion"); return true;
case 7: ffStrbufSetStatic(&os->codename, "Lion"); return true;
case 6: ffStrbufSetStatic(&os->codename, "Snow Leopard"); return true;
case 5: ffStrbufSetStatic(&os->codename, "Leopard"); return true;
case 4: ffStrbufSetStatic(&os->codename, "Tiger"); return true;
case 3: ffStrbufSetStatic(&os->codename, "Panther"); ffStrbufSetStatic(&os->prettyName, "Mac OS X"); return true;
case 2: ffStrbufSetStatic(&os->codename, "Jaguar"); ffStrbufSetStatic(&os->prettyName, "Mac OS X"); return true;
case 1: ffStrbufSetStatic(&os->codename, "Puma"); ffStrbufSetStatic(&os->prettyName, "Mac OS X"); return true;
case 0: ffStrbufSetStatic(&os->codename, "Cheetah"); ffStrbufSetStatic(&os->prettyName, "Mac OS X"); return true;
case 2: ffStrbufSetStatic(&os->codename, "Jaguar"); return true;
case 1: ffStrbufSetStatic(&os->codename, "Puma"); return true;
case 0: ffStrbufSetStatic(&os->codename, "Cheetah"); return true;
}
}
}
Expand Down Expand Up @@ -106,8 +106,6 @@ void ffDetectOSImpl(FFOSResult* os)
if(os->buildID.length == 0)
ffSysctlGetString("kern.osversion", &os->buildID);

if(os->prettyName.length == 0)
ffStrbufSetStatic(&os->prettyName, "macOS");
ffStrbufAppend(&os->versionID, &os->version);

if(!detectOSCodeName(os))
Expand Down
7 changes: 3 additions & 4 deletions src/detection/os/os_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void ffDetectOSImpl(FFOSResult* os)
const wchar_t* rawName = BrandingFormatString(L"%WINDOWS_LONG%");
ffStrbufSetWS(&os->variant, rawName);
GlobalFree((HGLOBAL)rawName);
ffStrbufSet(&os->prettyName, &os->variant);
ffStrbufTrimRight(&os->variant, ' ');

//WMI returns the "Microsoft" prefix while BrandingFormatString doesn't. Make them consistent.
Expand All @@ -22,14 +23,12 @@ void ffDetectOSImpl(FFOSResult* os)
if(ffStrbufStartsWithS(&os->variant, "Windows "))
{
ffStrbufAppendS(&os->name, "Windows");
ffStrbufAppendS(&os->prettyName, "Windows");

ffStrbufSubstrAfter(&os->variant, strlen("Windows ") - 1);

if(ffStrbufStartsWithS(&os->variant, "Server "))
{
ffStrbufAppendS(&os->name, " Server");
ffStrbufAppendS(&os->prettyName, " Server");
ffStrbufSubstrAfter(&os->variant, strlen(" Server") - 1);
}

Expand All @@ -38,7 +37,7 @@ void ffDetectOSImpl(FFOSResult* os)
ffStrbufSubstrAfter(&os->variant, index);

// Windows Server 20xx Rx
if(ffStrbufEndsWithC(&os->prettyName, 'r'))
if(ffStrbufEndsWithC(&os->name, 'r'))
{
if(os->variant.chars[0] == 'R' &&
ffCharIsDigit(os->variant.chars[1]) &&
Expand All @@ -56,6 +55,6 @@ void ffDetectOSImpl(FFOSResult* os)
ffStrbufClear(&os->variant);
}

ffStrbufAppendF(&os->id, "%*s %*s", os->prettyName.length, os->prettyName.chars, os->version.length, os->version.chars);
ffStrbufAppendF(&os->id, "%s %s", os->name.chars, os->version.chars);
ffStrbufSetStatic(&os->idLike, "Windows");
}
45 changes: 10 additions & 35 deletions src/modules/os/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,6 @@ static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result)
ffStrbufAppend(result, &os->variantID);
ffStrbufAppendC(result, ')');
}

//Append architecture if it is missing
if(!ffStrbufContainIgnCase(result, &instance.state.platform.sysinfo.architecture))
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &instance.state.platform.sysinfo.architecture);
}
}

static void buildOutputNixOS(const FFOSResult* os, FFstrbuf* result)
{
ffStrbufAppendS(result, "NixOS");

if(os->buildID.length > 0)
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->buildID);
}

if(os->codename.length > 0)
{
ffStrbufAppendS(result, " (");
ffStrbufAppendC(result, (char) toupper(os->codename.chars[0]));
ffStrbufAppendS(result, os->codename.chars + 1);
ffStrbufAppendC(result, ')');
}

if(instance.state.platform.sysinfo.architecture.length > 0)
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &instance.state.platform.sysinfo.architecture);
}
}

void ffPrintOS(FFOSOptions* options)
Expand All @@ -99,11 +67,18 @@ void ffPrintOS(FFOSOptions* options)
{
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();

if(ffStrbufIgnCaseCompS(&os->id, "nixos") == 0)
buildOutputNixOS(os, &result);
if(os->prettyName.length > 0)
ffStrbufAppend(&result, &os->prettyName);
else
buildOutputDefault(os, &result);

//Append architecture if it is missing
if(!ffStrbufContainIgnCase(&result, &instance.state.platform.sysinfo.architecture))
{
ffStrbufAppendC(&result, ' ');
ffStrbufAppend(&result, &instance.state.platform.sysinfo.architecture);
}

ffPrintLogoAndKey(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
ffStrbufPutTo(&result, stdout);
}
Expand Down Expand Up @@ -195,7 +170,7 @@ static FFModuleBaseInfo ffModuleInfo = {
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Name of the kernel", "sysname"},
{"Name of the OS", "name"},
{"Pretty name of the OS", "pretty-name"},
{"Pretty name of the OS, if available", "pretty-name"},
{"ID of the OS", "id"},
{"ID like of the OS", "id-like"},
{"Variant of the OS", "variant"},
Expand Down

0 comments on commit 6429c20

Please sign in to comment.