Linux下df命令简单实现

过程总结

  1. 在项目文件下查找关于Android设备上显示内存使用情况的源代码(比如:~\src\com\android\settings

  2. 将源代码导入Source Insight中进行查看,找到内存显示的代码片段分析如何进行内存容量的计算

  3. 将其Android代码翻译为C代码即可

相关代码

命令实现代码

// showstorage.c
#include <stdio.h>
#include <stdlib.h>
#include <mntent.h>
#include <string.h>
#include <sys/vfs.h>
#include <math.h>

extern char *formatStorageSizeLong(unsigned long long size);

unsigned long long getRomTotalSize();
unsigned  long long getRomAvailSize();
//unsigned long long getRomUsedSize();
unsigned  long long getSystemSize();
char *calculateStorage(unsigned long long size);

static char sizeToString[20] = {0};
unsigned long long T = 1024 * 1024 * 1024 * 1024ULL;
unsigned long long G = 1024 * 1024 * 1024ULL;
unsigned long long M = 1024 * 1024ULL;
unsigned long long K = 1024ULL; 

int main(void)
{
	//printf("%s start\n", __func__);
	char totalSpaceString[5] = {0};
	strcpy(totalSpaceString, formatStorageSizeLong(getRomTotalSize() + getSystemSize()));
	//printf("totalSpaceString:%s\n", totalSpaceString);
	unsigned long long totalStorage = 0;
	if(strchr(totalSpaceString, 'T')){
		char totalSpaceValue[10] = {0};
		char *indexGigabyte = strchr(totalSpaceString, 'T');
		int pos = indexGigabyte - totalSpaceString;
		strncpy(totalSpaceValue, totalSpaceString, pos - 1);
		totalStorage = (unsigned long long)(atof(totalSpaceValue) * 1024 * 1024 * 1024 * 1024);
		//printf("totalSize = %lld\n", totalStorage);
	}else if(strchr(totalSpaceString, 'G')){
		char totalSpaceValue[10] = {0};
		char *indexGigabyte = strchr(totalSpaceString, 'G');
		int pos = indexGigabyte - totalSpaceString;
		strncpy(totalSpaceValue, totalSpaceString, pos - 1);
		totalStorage = (unsigned long long)(atof(totalSpaceValue) * 1024 * 1024 * 1024);
		//printf("totalSize = %lld\n", totalStorage);
	}else if(strchr(totalSpaceString, 'M')){
		char totalSpaceValue[10] = {0};
		char *indexMegabyte = strchr(totalSpaceString, 'M');
		int pos = indexMegabyte - totalSpaceString;
		strncpy(totalSpaceValue, totalSpaceString, pos - 1);
		totalStorage = (long)(atof(totalSpaceValue) * 1024 * 1024);
	}
	
	char totalSize[20] = {0};
	char userSize[20] = {0};
	char systemSize[20] = {0};
	char availSize[20] = {0};
	//char usedSize[10] = {0};
	strcpy(totalSize, calculateStorage(totalStorage));
	//printf("totalSize = %s\n", totalSize);
	//strcpy(usedSize, calculateStorage(getRomUsedSize()));
	strcpy(userSize, calculateStorage(getRomTotalSize()));
	//printf("userSize = %s\n", userSize);
	strcpy(systemSize, calculateStorage(totalStorage - getRomTotalSize()));
	//printf("systemSize = %s\n", systemSize);
	strcpy(availSize, calculateStorage(getRomAvailSize()));
	//printf("availSize = %s\n", availSize);
	//printf("totalStorage = %lld\n", totalStorage);
	double usedPercent = ceil((double)((totalStorage - getRomAvailSize()) * 100 / totalStorage));
	printf("Total     User     System    Avail     Use%%\n");
	printf("%s     %s    %s     %s     %3.0f%%\n", totalSize, userSize, 
	       systemSize, availSize, usedPercent);
	/*printf("Total		Used		Avail		Use%%\n");
	printf("%s		%s		%s		%3.0f%%\n", totalSize, usedSize, availSize, usedPercent);*/
	//printf("%s end\n", __func__);
	return 0;
}

unsigned long long getRomTotalSize()
{
	//printf("%s start\n", __func__);
	struct statfs dataPartition;
	memset(&dataPartition, 0, sizeof(dataPartition));
	unsigned long long totalSize = 0;
	
	statfs("/data", &dataPartition);
	totalSize = dataPartition.f_blocks * dataPartition.f_bsize;
	//printf("getRomTotalSize: totalSize %lld\n", totalSize);
	//printf("%s end\n", __func__);
	return totalSize;
}

unsigned long long getRomAvailSize()
{
	//printf("%s start\n", __func__);
	struct statfs dataPartition;
	memset(&dataPartition, 0, sizeof(dataPartition));
	unsigned long long availSize = 0;
	
	statfs("/data", &dataPartition);
	availSize = dataPartition.f_bavail * dataPartition.f_bsize;
	//printf("%s end\n", __func__);
	return availSize;
}

#if 0
unsigned long long getRomUsedSize()
{
	printf("%s start\n", __func__);
	struct statfs dataPartition;
	memset(&dataPartition, 0, sizeof(dataPartition));
	unsigned long long usedSize = 0;
	
	statfs("/home", &dataPartition);
	usedSize = (dataPartition.f_blocks - dataPartition.f_bfree) * dataPartition.f_bsize;
	
	printf("%s end\n", __func__);
	return usedSize;
}
#endif

unsigned long long getSystemSize()
{
	//printf("%s start\n", __func__);
	struct statfs systemPartition;
	memset(&systemPartition, 0, sizeof(systemPartition));
	unsigned long long systemSize = 0;
	
	statfs("/system", &systemPartition);
	systemSize = systemPartition.f_blocks * systemPartition.f_bsize;
	//printf("%s end\n", __func__);
	return systemSize;
}

char *calculateStorage(unsigned long long size)
{
	//printf("%s start\n", __func__);
	if(size > T){
		sprintf(sizeToString, "%.2lfT", (double)size / T);
	}else if(size > G){
		sprintf(sizeToString, "%.2lfG", (double)size / G);
	}else if(size > M){
		sprintf(sizeToString, "%.2lfM", (double)size / M);
	}else if(size > K){
		sprintf(sizeToString, "%.2lfK", (double)size / K);
	}else{
		sprintf(sizeToString, "%.2lfB", (double)size);
	}
	//printf("%s end\n", __func__);
	return sizeToString;
}

工具函数代码

// utils.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

static char s[20] = {0};

char *formateDouble(double result, unsigned int n);

char *formatStorageSizeLong(unsigned long long size)
{
	//printf("%s start\n", __func__);
	//printf("size = %llu\n", size);
	double result = size;
	char suffix[] = "B";
	
	if(result > 900){
		strcpy(suffix,"K");
		result /= 1024;
	}
	if(result > 900){
		strcpy(suffix, "M");
		result /= 1024;
	}
	if(result > 512){
		strcpy(suffix, "G");
		result /= 1024;
	}
	if(result > 900){
		strcpy(suffix, "T");
		result /= 1024;
	}
	if(result > 900){
		strcpy(suffix, "P");
		result /= 1024;
	}
	
	char *value = NULL;
	if(result < 1){
		value = formateDouble(result, 2);
	}else if(result < 10){
		value = formateDouble(result, 1);
	}else if(result < 100){
		value = formateDouble(result, 0);
	}else{
		value = formateDouble(result, 0);
	}
	
	double convertValue = ceil(atof(value));
	if(convertValue <= 0.5){
		convertValue = 512;
	}else if(convertValue <= 1.0){
		convertValue = 1.0;
	}else if(1.0 < convertValue &&convertValue <= 2.0){
		convertValue = 2.0;
	}else if(2.0 < convertValue && convertValue <= 4.0){
		convertValue = 4.0;
	}else if(4.0 < convertValue && convertValue <= 8.0){
		convertValue = 8.0;
	}else if(8.0 < convertValue && convertValue <= 16.0){
		convertValue = 16.0;
	}else if(16.0 < convertValue && convertValue <= 32.0){
		convertValue = 32.0;
	}
	char buf[5];
	sprintf(buf, "%.1f", convertValue);
	//printf("buf = %s\n", buf);
	//printf("%s end\n", __func__);
	return strcat(buf, suffix);
}

char *formateDouble(double result, unsigned int n)
{
	//printf("%s start\n", __func__);
	//printf("formatDouble result = %f\n", result);
	char resultByte[20] = {0};
	sprintf(resultByte, "%f", result);
	char *index = strchr(resultByte, '.');
	int pos = 0;
	if(index == NULL)
		pos = -1;
	else
		pos = index - resultByte;
	//printf("formatDouble pos = %d\n", pos);
	if(pos < 0){
		strcpy(s, resultByte);
		if(n > 0){
			char subString[5]= {0};
			strncpy(subString, ".000", n+1);
			strcat(resultByte, subString);
			strcpy(s, resultByte);
		}
	}else if(pos > 0){
		int number = 1;
		unsigned int i;
		for(i = 0; i < n; i++){
			number *= 10;
		}
		char numberResultByte[20] = {0};
		sprintf(numberResultByte, "%f", (double)number*atof(resultByte));
		strcpy(s, numberResultByte);
		strncpy(s, s, pos + n);
		if(n > 0){
			//printf("number = %d\n", number);
			sprintf(s, "%f", (double)atof(s) / number);
		}
		if(strchr(s, '.')){
			char *token = strtok(s, ".");
			if(token){
				token = strtok(NULL, ".");
				if(strlen(token) < n){
					strcat(s, "0");
				}
			}
		}
	}
	//printf("s = %s\n", s);
	//printf("%s end\n", __func__);
	return s;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值