diff --git a/smc.c b/smc.c index 0992962..83ac03b 100644 --- a/smc.c +++ b/smc.c @@ -26,8 +26,14 @@ #include "smc.h" +#if (MAC_OS_X_VERSION_MAX_ALLOWED < 120000) // Before macOS 12 Monterey + #define kIOMainPortDefault kIOMasterPortDefault +#endif + static io_connect_t conn; +UInt8 coreNums[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + UInt32 strtoulWithSize(const char *str, int size, int base) { UInt32 total = 0; int i; @@ -56,7 +62,7 @@ kern_return_t SMCOpen(void) { io_object_t device; CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC"); - result = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iterator); + result = IOServiceGetMatchingServices(kIOMainPortDefault, matchingDictionary, &iterator); if (result != kIOReturnSuccess) { printf("Error: IOServiceGetMatchingServices() = %08x\n", result); return 1; @@ -156,29 +162,29 @@ double SMCGetTemperature(char *key) { } int getTemperatureSMCKeySize(unsigned long core) { - return snprintf(NULL, 0, "%s%lu%c", SMC_CPU_CORE_TEMP_PREFIX, core, SMC_CPU_CORE_TEMP_SUFFIX_NEW); + return snprintf(NULL, 0, "%s%c%c", SMC_CPU_CORE_TEMP_PREFIX, coreNums[core], SMC_CPU_CORE_TEMP_SUFFIX_NEW); } void getOldSMCTemperatureKeyTemplate(char *key) { - sprintf(key, "%s%%lu%c", SMC_CPU_CORE_TEMP_PREFIX, SMC_CPU_CORE_TEMP_SUFFIX_OLD); + sprintf(key, "%s%%c%c", SMC_CPU_CORE_TEMP_PREFIX, SMC_CPU_CORE_TEMP_SUFFIX_OLD); } void getNewSMCTemperatureKeyTemplate(char *key) { - sprintf(key, "%s%%lu%c", SMC_CPU_CORE_TEMP_PREFIX, SMC_CPU_CORE_TEMP_SUFFIX_NEW); + sprintf(key, "%s%%c%c", SMC_CPU_CORE_TEMP_PREFIX, SMC_CPU_CORE_TEMP_SUFFIX_NEW); } double getTemperatureKeyTemplate(unsigned long core, char *templateKey) { getNewSMCTemperatureKeyTemplate(templateKey); char key[getTemperatureSMCKeySize(core)]; - sprintf(key, templateKey, core); + sprintf(key, templateKey, coreNums[core]); double temperature = SMCGetTemperature(key); if (temperature == 0) { // We must use the old key getOldSMCTemperatureKeyTemplate(templateKey); - sprintf(key, templateKey, core); + sprintf(key, templateKey, coreNums[core]); temperature = SMCGetTemperature(key); } @@ -322,7 +328,7 @@ int main(int argc, char *argv[]) { for (int i = 1; i < coreCount; ++i) { char key[getTemperatureSMCKeySize(coreList[i])]; - sprintf(key, templateKey, coreList[i]); + sprintf(key, templateKey, coreNums[coreList[i]]); double temperature = SMCGetTemperature(key); if (temperature == 0) {