Skip to content

Commit

Permalink
Try to future-proof macOS codename detection (#48) (#49)
Browse files Browse the repository at this point in the history
* Try to future-proof macOS codename detection

* Style changes
clang-format -i -style=file

* plat/darwin/detect: Fix sizeof

Co-authored-by: danyisill <[email protected]>
  • Loading branch information
woodruffw and danyisill authored Sep 22, 2020
1 parent 581ea19 commit 2d76746
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/disp.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void process_logo_only(char *distro[], unsigned short int num) {
void output_logo_only(char *distro) {
if (STREQ(distro, "Windows")) {
process_logo_only(windows_logo, 16);
} else if (strstr(distro, "OS X")) {
} else if (strstr(distro, "OS X") || strstr(distro, "Mac OS") || strstr(distro, "macOS")) {
process_logo_only(macosx_logo, 16);
} else if (STREQ(distro, "Arch Linux - Old")) {
process_logo_only(oldarch_logo, 18);
Expand Down Expand Up @@ -204,7 +204,7 @@ void main_ascii_output(char *data[], char *data_names[]) {
process_data(data, data_names, windows_modern_logo, 19, DETECTED_ARR_LEN, TLBL, TNRM, TLBL);
} else if (strstr(data[1], "Microsoft")) {
process_data(data, data_names, windows_logo, 16, DETECTED_ARR_LEN, TRED, TWHT, TRED);
} else if (strstr(data[1], "OS X")) {
} else if (strstr(data[1], "OS X") || strstr(data[1], "Mac OS") || strstr(data[1], "macOS")) {
process_data(data, data_names, macosx_logo, 16, DETECTED_ARR_LEN, TLBL, TNRM, TLBL);
} else if (STREQ(data[1], "Arch Linux - Old")) {
process_data(data, data_names, oldarch_logo, 18, DETECTED_ARR_LEN, TLBL, TNRM, TLBL);
Expand Down
34 changes: 26 additions & 8 deletions src/plat/darwin/detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
detects the computer's distribution (OS X release)
*/
void detect_distro(void) {
char *codenames[] = {"Cheetah", "Puma", "Jaguar", "Panther", "Tiger",
"Leopard", "Snow Leopard", "Lion", "Mountain Lion", "Mavericks",
"Yosemite", "El Capitan", "Sierra", "High Sierra", "Mojave"};
char *codenames[] = {"Cheetah", "Puma", "Jaguar", "Panther",
"Tiger", "Leopard", "Snow Leopard", "Lion",
"Mountain Lion", "Mavericks", "Yosemite", "El Capitan",
"Sierra", "High Sierra", "Mojave", "Catalina"};
CFArrayRef split = CFStringCreateArrayBySeparatingStrings(
NULL,
CFPreferencesCopyAppValue(CFSTR("ProductVersion"),
Expand All @@ -64,14 +65,31 @@ void detect_distro(void) {
CFSTR("/System/Library/CoreServices/SystemVersion")),
build_ver, 16, kCFStringEncodingUTF8);

char *codename = "Unknown";
char *codename = "Mac OS";
char buf[128];
if (min < sizeof(codenames) / sizeof(*codenames)) {
codename = codenames[min];
snprintf(buf, sizeof(buf), "%s %s", min < 8 ? "Mac OS X" : min < 12 ? "OS X" : "macOS",
codenames[min]);
codename = buf;
} else {
char *lookfor = "SOFTWARE LICENSE AGREEMENT FOR ";
FILE *fp = fopen("/System/Library/CoreServices/Setup "
"Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf",
"r");
if (fp != NULL) {
for (int i = 0; i < 50 && fgets(buf, sizeof(buf), fp); ++i) {
char *p = strstr(buf, lookfor);
if (p) {
codename = p + strlen(lookfor);
codename[strlen(p) - strlen(lookfor) - 1] = '\0';
break;
}
}
fclose(fp);
}
}

snprintf(distro_str, MAX_STRLEN, "Mac OS X %d.%d.%d (%s) \"%s\"", maj, min, fix, build_ver,
codename);

snprintf(distro_str, MAX_STRLEN, "%s %d.%d.%d (%s)", codename, maj, min, fix, build_ver);
safe_strncpy(host_color, TLBL, MAX_STRLEN);

return;
Expand Down

0 comments on commit 2d76746

Please sign in to comment.