The source code is as follows:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/vfs.h>//File system Information Structurestructfilesystem_info{Charfilesystem_format[8];Charfilesystem_total_capacity[ One];Charfilesystem_free_capacity[ One];Charfilesystem_permissions[3];};/ * Get file System Information * /intGet_filesystem_info (Const Char*filesystem_name,structFilesystem_info *fi);/*block to kbyte*/Static unsigned LongKscale (unsigned LongM_block,unsigned LongM_kbyte);/*convert size to GB MB kb*/Static Char*convert_size (floatM_size,Char*dest);intMain () {Charbuf[1024x768];structFilesystem_info Filesysinfo; Get_filesystem_info ("/", &filesysinfo);printf("%s\n", Filesysinfo.filesystem_format);printf("%s\n", filesysinfo.filesystem_free_capacity);printf("%s\n", filesysinfo.filesystem_total_capacity);printf("%s\n", filesysinfo.filesystem_permissions);return 0;}/ * Get file System Information * /intGet_filesystem_info (Const Char*filesystem_name,structFilesystem_info *fi) {structStatfs buf;floatFilesystem_total_size =0;floatFilesystem_free_size =0;if(Statfs (FILESYSTEM_NAME,&BUF)) {fprintf(stderr,"Statfs%s\n", Strerror (errno));return-1; }Switch(Buf.f_type) { Case 0xef51: Case 0xef53:sprintf(Fi->filesystem_format,"EXT"); Break; Case 0x4d44:sprintf(Fi->filesystem_format,"FAT"); Break; Case 0x5346544e:sprintf(Fi->filesystem_format,"NIFS"); Break;default:sprintf(Fi->filesystem_format,"Unknown"); Break; } bzero (&fi->filesystem_total_capacity,sizeof(fi->filesystem_total_capacity)); Bzero (&fi->filesystem_free_capacity,sizeof(fi->filesystem_free_capacity));printf("Blocks%ld\n", buf.f_blocks);printf("Bfree%ld\n", Buf.f_bfree);printf("Bsize%ld\n", buf.f_bsize); Filesystem_total_size = (float) (Kscale (Buf.f_blocks, buf.f_bsize)); Filesystem_free_size = (float) (Kscale (Buf.f_bfree, buf.f_bsize));printf("Total%f\n", filesystem_total_size);printf("Free%f\n", filesystem_free_size); Convert_size (filesystem_total_size,fi->filesystem_total_capacity); Convert_size (filesystem_free_size,fi->filesystem_free_capacity); Bzero (Fi->filesystem_permissions,sizeof(fi->filesystem_permissions));sprintf(Fi->filesystem_permissions,"RW");return 0;}/*block to kbyte*/Static unsigned LongKscale (unsigned LongM_block,unsigned LongM_kbyte) {return((unsigned Long Long) M_block * M_kbyte +1024x768/2) /1024x768;}/*convert size to GB MB kb*/Static Char*convert_size (floatM_size,Char*dest) {if((((M_size/1024.0) /1024.0)) >=1.0) {sprintf(Dest,"%0.2FGB", (m_size/1024.0)/1024.0); }Else if((M_size/1024.0) >=1.0) {sprintf(Dest,"%0.2FMB", (m_size/1024x768)); }Else{sprintf(Dest,"%0.2FKB", m_size); }returnDest;}
Summarize:
1, about struct STATFS structure information reference: http://blog.csdn.net/u011641885/article/details/46919027
2. For filesystem_total_size, float type is used for higher accuracy. The Fdisk source in Buysbox uses integer division, about 4 five in.
3. The Kscale function uses the unsigned long long type because M_block and M_byte are originally long, and the result of multiplying exceeds the long type, which can represent data bits.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux Get File System information (disk information)