From 145e09d164cc7d0ab96d117deb4acdecb3b56582 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sun, 2 Oct 2022 01:50:24 +0800 Subject: [PATCH] OS: simplify macOS OS detection code; print BuildID on macOS ( to match neofetch ) --- CMakeLists.txt | 2 +- src/detection/os/{os_apple.c => os_apple.m} | 63 +++++---------------- src/modules/os.c | 8 +++ 3 files changed, 23 insertions(+), 50 deletions(-) rename src/detection/os/{os_apple.c => os_apple.m} (58%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bfbad5f0..ad108cd95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/detection/os/os_apple.c b/src/detection/os/os_apple.m similarity index 58% rename from src/detection/os/os_apple.c rename to src/detection/os/os_apple.m index a18870c81..d6a410506 100644 --- a/src/detection/os/os_apple.c +++ b/src/detection/os/os_apple.m @@ -1,62 +1,27 @@ #include "os.h" -#include "common/properties.h" #include "common/sysctl.h" #include #include - -typedef enum PListKey -{ - PLIST_KEY_NAME, - PLIST_KEY_VERSION, - PLIST_KEY_BUILD, - PLIST_KEY_OTHER -} PListKey; +#import 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, "", &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, "", &os->name); - else if(key == PLIST_KEY_VERSION) - ffParsePropLine(line, "", &os->version); - else if(key == PLIST_KEY_BUILD) - ffParsePropLine(line, "", &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) diff --git a/src/modules/os.c b/src/modules/os.c index 3d110f3a4..7d5a15842 100644 --- a/src/modules/os.c +++ b/src/modules/os.c @@ -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) {