c program to extract system info

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster

c program to extract system info

 
0
  #1
Oct 7th, 2005
hi everybody.
i am trying to develop a program that will be able to read and export informations (hw, sw, kernel, netstats..ecc) from a linux computer.
my idea was to use the files in the /proc directory but i can't get the script to work - this is what i got so far

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct CpuInfo {
  5. char vendor_id[50];
  6. int family;
  7. char model[50];
  8. float freq;
  9. char cache[20];
  10. }
  11.  
  12. int main()
  13. {
  14. struct CpuInfo info = {"", 0, "", 0.0, ""};
  15.  
  16. FILE *cpuInfo;
  17.  
  18.  
  19. if( ( cpuInfo = fopen("/proc/cpuinfo", "rb") == NULL ) ) {
  20. printf("ERRORE! Impossibile aprire il file relativo alla CPU.");
  21. }
  22. else {
  23. while (!feof(cpuInfo)) {
  24. fread(&info, sizeof(struct CpuInfo), 1, cpuInfo);
  25.  
  26. if(info.family !=0) {
  27. printf("%s\n%d\n%s\n%.2f\n%s\n", info.vendor_id, info.family, info.model, info.freq, info.cache);
  28. }
  29. }
  30. }
  31. return 0;
  32. }

and this is the compiler output:


dude@gnixbox:~$ gcc -Wall -o cpuinfo cpuinfo.c
cpuinfo.c:13: error: two or more data types in declaration specifiers
cpuinfo.c:14: warning: return type of ‘main’ is not ‘int’
cpuinfo.c: In function ‘main’:
cpuinfo.c:20: warning: assignment makes pointer from integer without a cast
cpuinfo.c:32: warning: control reaches end of non-void function


anyone got ideas?suggestions?

thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: c program to extract system info

 
1
  #2
Oct 7th, 2005
First, end the structure definition with a semicolon:
struct CpuInfo {
	char vendor_id[50];
	int family;
	char model[50];
	float freq;
	char cache[20];
};
[Dani - here's where I wish I had the ability to make the size bigger.]

Here you need to carefully watch how you use parentheses:
if( ( cpuInfo = fopen("/proc/cpuinfo", "rb") == NULL ) ) {
if( ( cpuInfo = fopen("/proc/cpuinfo", "rb")) == NULL  ) {
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster

Re: c program to extract system info

 
0
  #3
Oct 8th, 2005
thanks!that worked.
but now my problem is how to deal with the input file, in my code i tryed to select the needed infos but the output looks pretty awful.
this is a /proc/cpuinfo formatting

processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 11
model name : Intel(R) Celeron(TM) CPU 1066MHz
stepping : 1
cpu MHz : 1063.599
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 2105.34
i want my software to copy the vendor_id, cpu family, model name, cpu Mhz , cache size and so i defined the struct
  1. struct CpuInfo {
  2. char vendor_id[50];
  3. int family;
  4. char model[50];
  5. float freq;
  6. char cache[20];
  7. };

but when i try to run the program this is what i get.seems obvious to me it is some kind of mistake with the chars&ints but i'm unable to solve it.

dude@gnixbox:~$ ./cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 11
model name : Intel(R) Celeron(TM) CPU 1066MHz
step\uffff\uffffH¿\uffff~\u07b7
1869417014
del : 11
model name : Intel(R) Celeron(TM) CPU 1066MHz
step\uffff\uffffH¿\uffff~\u07b7
0.00
1066MHz
step\uffff\uffffH¿\uffff~\u07b7
ing : 1
cpu MHz : 1063.599
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : \uffff\uffffH¿\uffff~\u07b7
1735746143
: no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : \uffff\uffffH¿\uffff~\u07b7
51996536886002927864281668446584832.00
es
fpu_exception : \uffff\uffffH¿\uffff~\u07b7
es
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
1668510752
msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
19680190460165560588342417948672.00
r sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
5.34

id level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
1668510752
msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7
19680190460165560588342417948672.00
r sse
bogomips : 21\uffff\uffffH¿\uffff~\u07b7


thanks again guys
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 5
Reputation: shric will become famous soon enough shric will become famous soon enough 
Solved Threads: 1
shric's Avatar
shric shric is offline Offline
Newbie Poster

Re: c program to extract system info

 
0
  #4
Oct 8th, 2005
You are not checking the return code of fread. In addition, you are checking for feof in the wrong spot. Your problem is similar to what's explained in this FAQ: http://www.eskimo.com/~scs/C-faq/q12.2.html
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster

Re: c program to extract system info

 
0
  #5
Oct 8th, 2005
Originally Posted by shric
You are not checking the return code of fread. In addition, you are checking for feof in the wrong spot. Your problem is similar to what's explained in this FAQ: http://www.eskimo.com/~scs/C-faq/q12.2.html
ok that solved that problem.
the thing i don't really get is how to tell it to read lines 2+3 then skip line 4 and read line 5 (ecc...)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC