/*  Program to print out current temperature values as reported by PowerPC on-die
 *  temperature sensors.
 *
 *  Code below is based on an example posted by Jim Magee (jmagee@apple.com) to the
 *  darwin-development mailing list on February 5, 2002, archived here:
 *  http://lists.apple.com/archives/darwin-development/2002/Feb/05/makingprogressonhost_pro.003.txt
 *  
 *  Author: Eric Carlson, carl0240@tc.umn.edu
 *  
 *  This software is supplied 'as is', with no warranty. 
 *  You are free to use and/or redistribute this source, modified or unmodified,
 *  in any form.
 */

#include <mach/mach.h>
#include <mach/mach_error.h>

main(int argc, char **argv)
{
        host_t  host;
        unsigned int processor_count;
        int *temps;
        unsigned int temps_count;
        kern_return_t kr;
        int i;

        host = mach_host_self(); /* will just be the regular host access port */
        kr = host_processor_info(host, PROCESSOR_TEMPERATURE,
                                 &processor_count, &temps, &temps_count);
        if (kr != KERN_SUCCESS) {
                printf("host_processor_info: %s\n", mach_error_string(kr));
                exit(-1);
        }
        if (temps_count/processor_count != PROCESSOR_TEMPERATURE_COUNT) {
                printf("processor temp info: bad size: %d\n", temps_count);
                exit(-1);
        }

        for (i = 0; i < processor_count; i++) {
                printf("Temperature for processor %d: ", i);
                if (temps[i] < 0) {
                        printf("unsupported\n");
                } else {
                        printf("%d\'(C) ", temps[i]);
                        printf("%f\'(F) (uncalibrated)\n", ((temps[i]*(9.0/5.0))+32.0) );
                }
        }

        kr = vm_deallocate(mach_task_self(), (vm_address_t)temps,
                (vm_size_t)(temps_count * sizeof(int)));
        if (kr != KERN_SUCCESS) {
                printf("vm_deallocate: %s\n", mach_error_string(kr));
                exit(-1);
        }
}

