#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buf[1024] = {0};
FILE *fp;
if((fp = popen("df -m|grep '/'", "r")) == NULL){
printf("popen error!\n");
exit(1);
}
fread(buf, sizeof(char), sizeof(buf), fp);
printf("result buffer:\n %s", buf);
int size, used, avail, percent;
size = used = avail = percent = 0;
char *ptr, *record;
record = buf;
printf("\nparsing content:\n");
while((ptr = strchr(record, ' ')) != NULL){
*ptr++ = 0;
if (*record == '\n')
record++;
sscanf(ptr, "%d%d%d%d", &size, &used, &avail, &percent);
printf("filesytem:%10s size:%6dMB used:%6dMB avail:%6dMB percent:%d%%\n",
record, size, used, avail, percent);
record = strchr(ptr, '\n');
}
printf("\n");
pclose(fp);
return 0;
}
|