#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>

FILE *tf;
FILE *hf;

char *temp_file = "/sys/bus/iio/devices/iio:device0/in_temp_input";
char *hum_file = "/sys/bus/iio/devices/iio:device0/in_humidityrelative_input";

char buf[16];

double temp;
double hum;

int main(int argc,char *argv[]) {
    while (true) {
        if ((tf = fopen(temp_file,"r")) != NULL) {
            if (fgets(buf,sizeof(buf),tf) != NULL) {
                temp = atof(buf) / 1000.0;
                if ((hf = fopen(hum_file,"r")) != NULL) {
                    if (fgets(buf,sizeof(buf),hf) != NULL) {
                        hum = atof(buf)/ 1000.0;
                        fclose(hf);
                        break;
                    }
                    fclose(hf);
                }
            }
            fclose(tf);
        }
        usleep(500000);
    }

    printf("Temperature: %.1fC  %.1fF  Humidity: %.1f%%\n",
     temp,temp * 9.0 / 5.0 + 32.0 ,hum);

    return 0;
}