My Tech notes: Get Disk Details Values in C function Nice one
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Tuesday, October 14, 2008

Get Disk Details Values in C function Nice one

I want to get the disk details in the format like type, size, used and available space;
The output should be
* disk_details[0] contains mounted FS type
* disk_details[1] contains mounted FS size
* disk_details[2] contains mounted FS used space
* disk_details[3] contains mounted FS Available space
* disk_details[4] contains mounted FS % used space
where FS is file system
Basically i want the output of $df -h | grep /mnt in a c function,
here is the c function which takes double dimension array as in input; updates it with disk details.
int get_disk_details(char disk_details[][50])
{
FILE *fp;
char df_buf[90];
char *name1,*result;
int i;
char *tmp;

system("df -h | grep /mnt | awk '{print $6,$2,$3,$4,$5}' > /var/temp2.txt");
fp= fopen("/var/temp2.txt","r");
i=0;
while(fgets(df_buf,80,fp))
{
result = strtok(df_buf, " ");
while(result != NULL)
{
if(strncmp(result,"/mnt/",5) == 0)
{
name1 = result;
name1 = name1+5;
tmp = strdup(name1);
strcpy(disk_details[i],tmp);
i++;
}
else
{
tmp = strdup(result);
strcpy(disk_details[i],tmp);
i++;
}
result = strtok(NULL," ");
}
}
fclose(fp);

return 0;
}

usage could be
declare
char df_details[5][50];
call from main;
get_disk_details(df_details);

No comments:

Post a Comment