Function:
Statfs
Function Description:
Query Information related to the file system. Usage:
# Include <sys/VFS. h>/* or <sys/statfs. h> */
Int statfs (const char * path, struct statfs * BUF );
Int fstatfs (int fd, struct statfs * BUF );
Parameters:
Path: the file path name of the file system to query information.
FD: The file description word of the file system to query information.
Buf: pointer variable of the following struct used to store information related to the file system
Struct statfs {
Long f_type;/* file system type */
Long f_bsize;/* optimized transfer block size */
Long f_blocks;/* Total Number of data blocks in the file system */
Long f_bfree;/* Number of available blocks */
Long f_bavail;/* number of blocks that non-superusers can obtain */
Long f_files;/* Total number of file nodes */
Long f_ffree;/* Number of available File nodes */
Fsid_t f_fsid;/* File System ID */
Long f_namelen;/* Maximum File Name Length */
};
Return description:
0 is returned when execution is successful. -1 is returned for failure, and errno is set to one of the following values
Eacces: the directory in the (statfs () file or path name is not accessible.
The description of the (fstatfs () file is invalid.
Efault: the memory address is invalid.
Eintr: The operation is interrupted by a signal.
EIO: read/write error
Eloop: (statfs () explains that too many symbolic connections exist in the path name process.
Enametoolong :( statfs () path name is too long
Enoent :( statfs () file does not exist
Enomem: insufficient core memory
Enosys: The file system does not support calling.
The component used as a directory in the path name of enotdir :( statfs () is not a directory.
Eoverflow: Information Overflow
Example:
1 // Description: pdisk path name, for example, "/home" 2 3 int dh_getdiskfreespacepercent (char * pdisk) {4 long freespace = 0; 5 struct statfs disk_statfs; 6 long totalspace = 0; 7 float freespacepercent = 0; 8 9 If (statfs (pdisk, & disk_statfs)> = 0) {10 freespace = (long) disk_statfs.f_bsize * (long) disk_statfs.f_bfree)/(long) 1024); 11 totalspace = (long) disk_statfs.f_bsize * (long) disk _ Statfs. f_blocks)/(long) 1024); 12} 13 14 freespacepercent = (float) freespace/(float) totalspace) * 100; 15 return freespacepercent; 16} 17 18 Linux DF command implementation: 19 20 # include <stdio. h> 21 # include <stdlib. h> 22 # include <string. h> 23 # include <errno. h> 24 # include <sys/statfs. h> 25 26 static int OK = exit_success; 27 28 static void printsize (long n) 29 {30 char unit = 'K'; 31 N/= 1024; 3 2 If (n> 1024) {33 N/= 1024; 34 unit = 'M'; 35} 36 IF (n> 1024) {37 N/= 1024; 38 unit = 'G'; 39} 40 printf ("% 4lld % C", N, Unit); 41} 42 43 static void DF (char * s, int always) {44 struct statfs st; 45 46 If (statfs (S, & St) <0) {47 fprintf (stderr, "% s: % s \ n", S, strerror (errno); 48 OK = exit_failure; 49} else {50 if (St. f_blocks = 0 &&! Always) 51 return; 52 printf ("%-20 s", S); 53 printf ("%-20 s", S); 54 printsize (long) ST. f_blocks * (long) ST. f_bsize); 55 printf (""); 56 printsize (long) (St. f_blocks-(long) ST. f_bfree) * St. f_bsize); 57 printf (""); 58 printsize (long) ST. f_bfree * (long) ST. f_bsize); 59 printf ("% d \ n", (INT) ST. f_bsize); 60} 61} 62 63 int main (INT argc, char * argv []) {64 printf ("filesystem size used free blksize \ n "); 65 if (argc = 1) {66 char s [2000]; 67 file * f = fopen ("/proc/mounts", "R "); 68 69 while (fgets (S, 2000, f) {70 char * C, * E = s; 71 72 for (C = s; * C; C ++) {73 If (* c = '') {74 E = C + 1; 75 break; 76} 77} 78 79 for (C = E; * C; c ++) {80 If (* c = '') {81 * c = '\ 0'; 82 break; 83} 84} 85 86 DF (E, 0); 87} 88 89 fclose (f); 90} else {91 printf ("No argv \ n"); 92 int I; 93 94 for (I = 1; I <argc; I ++) {95 DF (argv [I], 1); 96} 97} 98 99 exit (OK); 100}