Go to: refer to the source code of a simple DF command written by DF code.

Source: Internet
Author: User

Refer to the source code of a simple DF command written by DF code.

 

Finishing reprinted: http://blog.csdn.net/fjb2080/article/details/5990355

Author: feikong jingdu

The DF command can list the information of the attached disk or various files:

The following provides the source code of a simple DF command. The source code is rewritten based on the source code of DF, which is very simple.

#include <stdio.h>  #include <mntent.h>  #include <string.h>  #include <sys/vfs.h>  static const unsigned long long G = 1024*1024*1024ull;  static const unsigned long long M = 1024*1024;  static const unsigned long long K = 1024;  static char str[20];  char* kscale(unsigned long b, unsigned long bs)  {      unsigned long long size = b * (unsigned long long)bs;      if (size > G)      {          sprintf(str, "%0.2f GB", size/(G*1.0));          return str;      }      else if (size > M)      {          sprintf(str, "%0.2f MB", size/(1.0*M));          return str;      }      else if (size > K)      {          sprintf(str, "%0.2f K", size/(1.0*K));          return str;      }      else      {          sprintf(str, "%0.2f B", size*1.0);          return str;      }  }int main(int argc, char *argv[])  {    FILE* mount_table;      struct mntent *mount_entry;      struct statfs s;      unsigned long blocks_used;      unsigned blocks_percent_used;      const char *disp_units_hdr = NULL;      mount_table = NULL;      mount_table = setmntent("/etc/mtab", "r");      if (!mount_table)      {          fprintf(stderr, "set mount entry error\n");          return -1;      }      disp_units_hdr = "     Size";      printf("Filesystem           %-15sUsed Available %s Mounted on\n",              disp_units_hdr, "Use%");      while (1) {          const char *device;          const char *mount_point;          if (mount_table) {              mount_entry = getmntent(mount_table);              if (!mount_entry) {                  endmntent(mount_table);                  break;              }          }           else              continue;          device = mount_entry->mnt_fsname;                                                                                                      mount_point = mount_entry->mnt_dir;          //fprintf(stderr, "mount info: device=%s mountpoint=%s\n", device, mount_point);          if (statfs(mount_point, &s) != 0)           {              fprintf(stderr, "statfs failed!\n");                      continue;          }          if ((s.f_blocks > 0) || !mount_table )           {              blocks_used = s.f_blocks - s.f_bfree;              blocks_percent_used = 0;              if (blocks_used + s.f_bavail)               {                  blocks_percent_used = (blocks_used * 100ULL                         + (blocks_used + s.f_bavail)/2                        ) / (blocks_used + s.f_bavail);              }              /* GNU coreutils 6.10 skips certain mounts, try to be compatible.  */              if (strcmp(device, "rootfs") == 0)                  continue;              if (printf("\n%-20s" + 1, device) > 20)                  printf("\n%-20s", "");              char s1[20];              char s2[20];              char s3[20];              strcpy(s1, kscale(s.f_blocks, s.f_bsize));              strcpy(s2, kscale(s.f_blocks - s.f_bfree, s.f_bsize));              strcpy(s3, kscale(s.f_bavail, s.f_bsize));              printf(" %9s %9s %9s %3u%% %s\n", s1, s2, s3,                          blocks_percent_used, mount_point);          }      }      return 0;  }  

Compile: G ++-g-Wall Main. cpp

You can generate a. Out file,

The output comparison of running a. Out and DF-H is as follows:

$/Tmp $ DF-H
File System capacity used available % mount point
/Dev/sda7 9.4 GB 6.5g 2.5g 73%/
None 1.6g 300 K 1.6g 1%/dev
None 1.6g 212 K 1.6g 1%/dev/SHM
None 1.6g 296 K 1.6g 1%/var/run
None 1.6g 0 1.6g 0%/var/lock
None 1.6g 0 1.6g 0%/lib/init/RW
/Dev/sda6 113G 87g 26G 77%/Media/work _
/Dev/sda9 26G 23g 2.0g 92%/home

 

/Tmp $./A. Out
Filesystem size used available use % mounted on
/Dev/sda7 9.39 GB 6.45 GB 2.46 GB 72%/
None 1.59 GB 300.00 K 1.59 GB 0%/dev
None 1.60 GB 1020.00 K 1.59 GB 0%/dev/SHM
None 1.60 GB 296.00 K 1.59 GB 0%/var/run
None 1.60 GB 0.00 B 1.60 GB 0%/var/lock
None 1.60 GB 0.00 B 1.60 GB 0%/lib/init/RW
/Dev/sda6 112.62 GB 86.67 GB 25.95 GB 77%/Media/work _
/Dev/sda9 25.38 GB 22.10 GB 1.99 Gbit/s 92%/home

----------------- Hua --- Li --- points --- cut --- line -----------------

When I reprinted it, I changed/N in the source code to \ n.

The source code of DF is in the coreutils project. Coreutils is the core tool of the GNU operating system and contains many basic commands, such as CP and RM. To learn more or download the latest source code, click here.

Related Article

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.