Obtain the file system information (Disk information) in linux)

Source: Internet
Author: User
Tags integer division

Obtain the file system information (Disk information) in linux)

The source code is as follows:

# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
// File system information structure struct fileSystem_info {char fileSystem_format [8]; char fileSystem_total_capacity [11]; char fileSystem_free_capacity [11]; char fileSystem_permissions [3];}; /* Get File System information */int get_fileSystem_info (const char * fileSystem_name, struct fileSystem_info * fi);/* block to kbyte */static unsigned long kscale (unsigned long m_block, unsigned long m_kbyte);/* convert size to gb mb kb */static char * convert_size (float m_size, char * dest); int main () {char buf [1024]; struct fileSystem_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 */int get_fileSystem_info (const char * fileSystem_name, struct fileSystem_info * fi) {struct statfs buf; float fileSystem_total_size = 0; float fileSystem_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 0x5425544e: 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-> bytes ); convert_size (fileSystem_free_size, fi-> packages); bzero (fi-> fileSystem_permissions, sizeof (fi-> fileSystem_permissions); sprintf (fi-> fileSystem_permissions, "rw "); return 0;}/* block to kbyte */static unsigned long kscale (unsigned long m_block, unsigned long m_kbyte) {return (unsigned long) m_block * m_kbyte + 1024/2) /1024;}/* convert size to gb mb kb */static char * convert_size (float m_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/1024);} else {sprintf (dest, "% 0.2fKB", m_size);} return dest ;}
     
    
   
  

Summary:
1. struct statfs struct information reference: http://blog.csdn.net/u011641885/article/details/46919027
2. Use the float type for fileSystem_total_size to ensure higher accuracy. The fdisk source code in buysbox uses integer division, which is about 4 homes and 5 inputs.
3. the unsigned long type is used in the kscale function because m_block and m_byte are originally in the long type. The result of multiplication exceeds the long type and can represent the data bit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.