Tag: is the tle skin null har free typedef nbsp THUNDER
There is nothing to say, in fact, is to obtain the hard disk STATFS information structure
The code is as follows:
#include <stdio.h> #include <stdlib.h> #include <sys/statfs.h> #include <sys/vfs.h> #include <string.h> #include <errno.h> #define DEFAULT_DISK_PATH "/home" typedef struct STATFS disk,*pdisk;// Gets the structure//parameter two: the location of the disk space information to get the disk information//return value: Successfully returned 1, failed to return 0int Getdiskinfo (pdisk diskinfo,const char *path) {char dpath[100]= default_disk_path;//Set the default location int flag=0; if (Null!=path) {strcpy (Dpath,path); if ( -1== (Flag=statfs (dpath,diskinfo)))//Gets the structure containing the disk space information {perror ("Getdiskinfo statfs fail"); return 0; } return 1;} Calculates the total disk space, non-superuser free space, all remaining space on the disk, and the computed result is stored as a string in three strings, in Mbint caldiskinfo (char *disktotal,char *diskavail,char * Diskfree,pdisk diskinfo) {unsigned long long total=0,avail=0,free=0,blocksize=0; int flag=0; if (!disktotal&&diskavail&&diskfree&&diskinfo) {printf ("\ncaldiskinfo param null!\n"); return 0; } blocksize=diskinfo->f_bsize;//each block contains the byte size Total=diskinfo->f_blocks*blocksize;//Total disk space avail=diskinfo->f_bavail*blocksize;//non-super user free space free=diskinfo->f_bfree*blocksize;// Disk all remaining space//String conversion flag=sprintf (Disktotal, "%llu", total>>20); flag=sprintf (Diskavail, "%llu", avail>>20); flag=sprintf (Diskfree, "%llu", free>>20); if ( -1==flag) {return 0; } return 1;} int main () {DISK diskinfo; Char str1[30],str2[30],str3[30]; memset (&diskinfo,0,sizeof (DISK)); Getdiskinfo (&diskinfo,default_disk_path);//Get disk information structure Body caldiskinfo (str1,str2,str3,&diskinfo);//Calculate disk information structure printf ("\ntotal:%s avail:%s free%s\n", STR1,STR2,STR3); printf ("Hello world!\n"); return 0;}
The results are as follows and the result is in MB
Disk information obtained using the DF command
Visible, the result is similar.
Transferred from: https://www.cnblogs.com/thegodofthunder/p/7234803.html
Linux C Gets the hard drive usage of any path