开发者

How can I get exact total space of a drive using C language program running on Linux?

开发者 https://www.devze.com 2022-12-08 05:44 出处:网络
How can I get exact total space of a drive using C language program running on Linux? I dont want to use开发者_JS百科 shell script. Any suggestions?statfs/statfs64

How can I get exact total space of a drive using C language program running on Linux? I dont want to use开发者_JS百科 shell script. Any suggestions?


statfs/statfs64

#include <sys/vfs.h>    /* or <sys/statfs.h> */
int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);

From the man page:

   The  function  statfs() returns information about a mounted file system.
   path is the pathname of any file within the mounted file system.
   buf is a pointer to a statfs structure defined approximately as follows:

       struct statfs {
          long    f_type;     /* type of file system (see below) */
          long    f_bsize;    /* optimal transfer block size */
          long    f_blocks;   /* total data blocks in file system */
          long    f_bfree;    /* free blocks in fs */
          long    f_bavail;   /* free blocks avail to non-superuser */
          long    f_files;    /* total file nodes in file system */
          long    f_ffree;    /* free file nodes in fs */
          fsid_t  f_fsid;     /* file system id */
          long    f_namelen;  /* maximum length of filenames */
       };

You can use it like this:

struct statfs buf;
statfs("/", &buf);
0

精彩评论

暂无评论...
验证码 换一张
取 消