2

here is my code concerning libsensors. libraries:

#include <unistd.h>
#include <sensors/sensors.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

code concerning libsensors:

char sd[16384]="^\0",bf[1];
char buf2[8192]="^\0";


sensors_chip_name const* scn;
int c=0;
int t4=1;
while((scn=sensors_get_detected_chips(0,&c))!=0)
{
        sensors_feature const *fea;
        int f=0;
        strcat(sd,scn->prefix);
        printf("%s",scn->prefix);
        strcat(sd,":");
        strcat(sd,scn->path);
        strcat(sd,"(");
        while((fea=sensors_get_features(scn,&f))!=0)
        {

                strcat(sd,fea->name);
                strcat(sd,"(");
                sensors_subfeature const *sb;
                int s=0;
                while((sb=sensors_get_all_subfeatures(scn,fea,&s))!=0)
                {
                        t4++;
                        strcat(sd,sb->name);
                        strcat(sd,",");
                        int t3=-1;
                        int i=0;
                        char t8[sizeof(sb->number)];
                        memcpy(&t8,&(sb->number),sizeof(sb->number));
                        strcat(sd,t8);
                        strcat(sd,"!");
                }
                strcat(sd,")");
        }
        strcat(sd,")");

}

so when I try to print anything nothing happens. char array called sd returns empty. it simply seems that there are no sensors to be read.

when I run sensors from terminal it works perfectly fine. I see a couple of cores and chips temps.

I implemented this code from some post on here and to be frank I don't totally understand it.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    "nothing happens" is a very coarse error description. Be more specific and use a debugger to step your code. – too honest for this site Sep 29 '15 at 22:07
  • @Olaf I mean to say that functions return empty I guess. buffers are not with data without any visible errors, that is why I am posting here. – Ali Nuri Şeker Sep 29 '15 at 22:09
  • 1
    what are you trying to accomplish with `"^\0"` in the initializer for the char buffers? The usual initializer is `{'\0'}` which sets all the character contents of the buffer to 0x00. – user3629249 Sep 29 '15 at 22:47
  • 1
    is `scn->prefix` supposed to be a NUL terminated string? is `scn->path` supposed to be a NUL terminated string? – user3629249 Sep 29 '15 at 22:48
  • `fea` is a pointer, so it should never be `0`, however; it might be `NULL` – user3629249 Sep 29 '15 at 22:51
  • when indenting code, do not use tabs, because each word processor/editor has the tab stops/tab width set differently. Always use spaces. – user3629249 Sep 29 '15 at 22:53
  • Im using ubuntu linux 14.04. Where would I find the documentation for that libsensors? – user3629249 Sep 29 '15 at 22:57
  • @user3629249 libsensors is a library which comes with lm-sensors. http://linux.die.net/man/3/libsensors I will check your suggestions and work with them thank you very much for the pointers that you have given. – Ali Nuri Şeker Sep 29 '15 at 23:04
  • The variable `t4` gets set and gets incremented but never used. – user3629249 Sep 29 '15 at 23:08
  • 2
    the posted code seems to be missing the call to: `sensors_init()` – user3629249 Sep 29 '15 at 23:10
  • @user3629249 thank you for your help, I was not able to see the elephant in the room it seems if you can post it here I can mark that as the solution :). about t4, it is used later on. thank you again for your attention. – Ali Nuri Şeker Sep 29 '15 at 23:20

1 Answers1

1

Posting @user3629249 comment as a community answer.


It it required to first call sensors_init() otherwise the chips list will be empty.

This function expects a sensors configuration file as argument, or NULL to use the default one.

Also, you can find an usage example in this related question: Has anyone been able to use libsensors properly?

Delgan
  • 18,571
  • 11
  • 90
  • 141