Code:
#include <stdio.h>#include<mntent.h>#include<string.h>#include<sys/vfs.h>Static ConstUnsignedLong LongG =1024x768*1024x768*1024ull;Static ConstUnsignedLong LongM =1024x768*1024x768;Static ConstUnsignedLong LongK =1024x768;Static Charstr[ -];Char* Kscale (unsignedLongB, unsignedLongBS) {unsignedLong LongSize = b * (unsignedLong Long) BS; if(Size >G) {sprintf (str,"%0.2f GB", size/(g*1.0)); returnstr; } Else if(Size >M) {sprintf (str,"%0.2f MB", size/(1.0*M)); returnstr; } Else if(Size >K) {sprintf (str,"%0.2f K", size/(1.0*K)); returnstr; } Else{sprintf (str,"%0.2f B", size*1.0); returnstr; }}intMainintargcChar*argv[]) {FILE*mount_table; structMntent *Mount_entry; structStatfs S; unsignedLongblocks_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) > -) printf ("\n%-20s",""); Chars1[ -]; Chars2[ -]; Chars3[ -]; 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_p OINT); } } return 0;}
The results are as follows:
Compile: g++-g-wall Main.CPPyou can generate a a.out file, the following is run A.out and DF-output Comparison of H::/tmp/tmp$DF-h file system capacity already available for use%mount point/dev/sda79.4G6.5G2.5G the% /None1.6G 300K1.6G1% /Devnone1.6G 212K1.6G1%/dev/Shmnone1.6G 296K1.6G1%/var/Runnone1.6G0 1.6G0%/var/Locknone1.6G0 1.6G0%/lib/init/RW/dev/sda6 113G 87G 26G the%/media/Work_/DEV/SDA9 26G 23G2.0G the% /Home/tmp/tmp$./a.outfilesystem Size used Available use%mounted on/dev/sda79.39GB6.45GB2.46GB the% /None1.59GB300.00K1.59GB0% /Devnone1.60GB1020.00K1.59GB0%/dev/Shmnone1.60GB296.00K1.59GB0%/var/Runnone1.60GB0.00B1.60GB0%/var/Locknone1.60GB0.00B1.60GB0%/lib/init/RW/dev/sda6112.62GB86.67GB25.95GB the%/media/Work_/dev/sda925.38GB22.10GB1.99GB the%/Home
APUE Programming Reference DF Code write a simple DF command source code