Skip to content

Commit 145e09d

Browse files
committed
OS: simplify macOS OS detection code; print BuildID on macOS ( to match neofetch )
1 parent 01dbd0c commit 145e09d

File tree

3 files changed

+23
-50
lines changed

3 files changed

+23
-50
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ if(APPLE)
297297
src/util/apple/cf_helpers.c
298298
src/util/apple/osascript.m
299299
src/detection/host/host_apple.c
300-
src/detection/os/os_apple.c
300+
src/detection/os/os_apple.m
301301
src/detection/cpu/cpu_apple.c
302302
src/detection/gpu/gpu_apple.c
303303
src/detection/battery/battery_apple.c
Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,27 @@
11
#include "os.h"
2-
#include "common/properties.h"
32
#include "common/sysctl.h"
43

54
#include <stdlib.h>
65
#include <string.h>
7-
8-
typedef enum PListKey
9-
{
10-
PLIST_KEY_NAME,
11-
PLIST_KEY_VERSION,
12-
PLIST_KEY_BUILD,
13-
PLIST_KEY_OTHER
14-
} PListKey;
6+
#import <Foundation/Foundation.h>
157

168
static void parseSystemVersion(FFOSResult* os)
179
{
18-
FILE* plist = fopen("/System/Library/CoreServices/SystemVersion.plist", "r");
19-
if(plist == NULL)
20-
return;
21-
22-
char* line = NULL;
23-
size_t len = 0;
24-
PListKey key = PLIST_KEY_OTHER;
25-
26-
FFstrbuf keyBuffer;
27-
ffStrbufInit(&keyBuffer);
28-
29-
while(getline(&line, &len, plist) != EOF)
30-
{
31-
if(ffParsePropLine(line, "<key>", &keyBuffer))
32-
{
33-
if(ffStrbufIgnCaseCompS(&keyBuffer, "ProductName") == 0)
34-
key = PLIST_KEY_NAME;
35-
else if(ffStrbufIgnCaseCompS(&keyBuffer, "ProductUserVisibleVersion") == 0)
36-
key = PLIST_KEY_VERSION;
37-
else if(ffStrbufIgnCaseCompS(&keyBuffer, "ProductBuildVersion") == 0)
38-
key = PLIST_KEY_BUILD;
39-
else
40-
key = PLIST_KEY_OTHER;
41-
42-
ffStrbufClear(&keyBuffer);
43-
continue;
44-
}
45-
46-
if(key == PLIST_KEY_NAME)
47-
ffParsePropLine(line, "<string>", &os->name);
48-
else if(key == PLIST_KEY_VERSION)
49-
ffParsePropLine(line, "<string>", &os->version);
50-
else if(key == PLIST_KEY_BUILD)
51-
ffParsePropLine(line, "<string>", &os->buildID);
52-
}
53-
54-
ffStrbufDestroy(&keyBuffer);
10+
NSError* error;
11+
NSString* fileName = @"file:///System/Library/CoreServices/SystemVersion.plist";
12+
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:fileName]
13+
error:&error];
14+
if(error)
15+
return;
5516

56-
if(line != NULL)
57-
free(line);
17+
NSString* value;
5818

59-
fclose(plist);
19+
if((value = [dict valueForKey:@"ProductName"]))
20+
ffStrbufInitS(&os->name, value.UTF8String);
21+
if((value = [dict valueForKey:@"ProductUserVisibleVersion"]))
22+
ffStrbufInitS(&os->version, value.UTF8String);
23+
if((value = [dict valueForKey:@"ProductBuildVersion"]))
24+
ffStrbufInitS(&os->buildID, value.UTF8String);
6025
}
6126

6227
void parseOSXSoftwareLicense(FFOSResult* os)

src/modules/os.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result)
3939
ffStrbufAppend(result, &os->version);
4040
}
4141

42+
#ifdef __APPLE__
43+
if(os->buildID.length > 0)
44+
{
45+
ffStrbufAppendC(result, ' ');
46+
ffStrbufAppend(result, &os->buildID);
47+
}
48+
#endif
49+
4250
//Append variant if it is missing
4351
if(os->variant.length > 0 && ffStrbufFirstIndex(result, &os->variant) == result->length)
4452
{

0 commit comments

Comments
 (0)