Over a million developers have joined DZone.

C Functions For Getting Disk Space Information In Linux OS

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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;
}


The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:
linux

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

{{ parent.tldr }}

{{ parent.urlSource.name }}