OS: simplify macOS OS detection code; print BuildID on macOS ( to match neofetch )

This commit is contained in:
Carter Li
2022-10-02 01:50:24 +08:00
parent 01dbd0c266
commit 145e09d164
3 changed files with 23 additions and 50 deletions
+1 -1
View File
@@ -297,7 +297,7 @@ if(APPLE)
src/util/apple/cf_helpers.c
src/util/apple/osascript.m
src/detection/host/host_apple.c
src/detection/os/os_apple.c
src/detection/os/os_apple.m
src/detection/cpu/cpu_apple.c
src/detection/gpu/gpu_apple.c
src/detection/battery/battery_apple.c
@@ -1,62 +1,27 @@
#include "os.h"
#include "common/properties.h"
#include "common/sysctl.h"
#include <stdlib.h>
#include <string.h>
typedef enum PListKey
{
PLIST_KEY_NAME,
PLIST_KEY_VERSION,
PLIST_KEY_BUILD,
PLIST_KEY_OTHER
} PListKey;
#import <Foundation/Foundation.h>
static void parseSystemVersion(FFOSResult* os)
{
FILE* plist = fopen("/System/Library/CoreServices/SystemVersion.plist", "r");
if(plist == NULL)
return;
NSError* error;
NSString* fileName = @"file:///System/Library/CoreServices/SystemVersion.plist";
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:fileName]
error:&error];
if(error)
return;
char* line = NULL;
size_t len = 0;
PListKey key = PLIST_KEY_OTHER;
NSString* value;
FFstrbuf keyBuffer;
ffStrbufInit(&keyBuffer);
while(getline(&line, &len, plist) != EOF)
{
if(ffParsePropLine(line, "<key>", &keyBuffer))
{
if(ffStrbufIgnCaseCompS(&keyBuffer, "ProductName") == 0)
key = PLIST_KEY_NAME;
else if(ffStrbufIgnCaseCompS(&keyBuffer, "ProductUserVisibleVersion") == 0)
key = PLIST_KEY_VERSION;
else if(ffStrbufIgnCaseCompS(&keyBuffer, "ProductBuildVersion") == 0)
key = PLIST_KEY_BUILD;
else
key = PLIST_KEY_OTHER;
ffStrbufClear(&keyBuffer);
continue;
}
if(key == PLIST_KEY_NAME)
ffParsePropLine(line, "<string>", &os->name);
else if(key == PLIST_KEY_VERSION)
ffParsePropLine(line, "<string>", &os->version);
else if(key == PLIST_KEY_BUILD)
ffParsePropLine(line, "<string>", &os->buildID);
}
ffStrbufDestroy(&keyBuffer);
if(line != NULL)
free(line);
fclose(plist);
if((value = [dict valueForKey:@"ProductName"]))
ffStrbufInitS(&os->name, value.UTF8String);
if((value = [dict valueForKey:@"ProductUserVisibleVersion"]))
ffStrbufInitS(&os->version, value.UTF8String);
if((value = [dict valueForKey:@"ProductBuildVersion"]))
ffStrbufInitS(&os->buildID, value.UTF8String);
}
void parseOSXSoftwareLicense(FFOSResult* os)
+8
View File
@@ -39,6 +39,14 @@ static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result)
ffStrbufAppend(result, &os->version);
}
#ifdef __APPLE__
if(os->buildID.length > 0)
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->buildID);
}
#endif
//Append variant if it is missing
if(os->variant.length > 0 && ffStrbufFirstIndex(result, &os->variant) == result->length)
{