Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

C Functions For Getting Disk Space Information In Linux OS

DZone's Guide to

C Functions For Getting Disk Space Information In Linux OS

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

These are 2 C functions for getting disk capacity and disk free space in megabytes into char array


#include 
#include 

gchar *
g_get_capacity ( gchar * dev_path)
{
unsigned long long result = 0;
int n;
gchar s_cap[50];
gchar * ss_cap = "N/A";
struct statvfs sfs;
if ( statvfs ( dev_path, &sfs) != -1 )
{
result = (unsigned long long)sfs.f_bsize * sfs.f_blocks;
}
if (result > 0)
{
double f_cap = (double)result/(1024*1024);
n = sprintf(s_cap, "%.2f Mb", f_cap);
ss_cap = g_strdup(s_cap);
}
return ss_cap;
}

gchar * 
g_get_free_space ( gchar * dev_path)
{
unsigned long long result = 0;
int n;
gchar s_cap[50];
gchar * ss_cap = "N/A";
struct statvfs sfs;
if ( statvfs ( dev_path, &sfs) != -1 )
{
result = (unsigned long long)sfs.f_bsize * sfs.f_bfree;
}
if (result > 0)
{
double f_cap = (double)result/(1024*1024);
n = sprintf(s_cap, "%.2f Mb", f_cap);
ss_cap = g_strdup(s_cap);
}
return ss_cap;
}


MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.

Topics:
linux

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}