Get free disk space


	int64_t free_disk_space;
	get_int64_value_from_ascii_string(do_console_command_get_result((char*)"df -k /tmp | tail -1 | awk '{print $4}'"), 0, &free_disk_space);		//Get free space in kB

	std::cout << "Free disk space: " << free_disk_space << "kB" << std::endl;

 


int get_int64_value_from_ascii_string (string source_string, int char_index, int64_t *result)
{
	int32_t value = -1;
	int32_t value_last = 0;
	char *p_source_string;
	
	
	p_source_string = (char*)source_string.c_str();
	p_source_string += char_index;

	//Ignore any leading spaces
	while (*p_source_string == ' ')
		p_source_string++;

	while ((*p_source_string >= '0') && (*p_source_string <= '9'))
	{
		if (value < 0)
			value = 0;
		value_last = value;

		value *= 10;
		value += (*p_source_string++ - 0x30);

		if (value_last > value)
		{
			value = -1;				//Value is > max possible
			break;
		}
	}
	*result = value;
	return((int)(p_source_string - (char*)source_string.c_str()));
}

string do_console_command_get_result (char* command)
{
	FILE* pipe = popen(command, "r");
	if (!pipe)
		return "ERROR";
	
	char buffer[128];
	string result = "";
	while(!feof(pipe))
	{
		if(fgets(buffer, 128, pipe) != NULL)
			result += buffer;
	}
	pclose(pipe);
	return(result);
}

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. Manuel

    3 years ago

    Is it to view the left space on SD card of raspberry?

Leave a Reply to Manuel Cancel reply

Your email address will not be published. Required fields are marked *