diff --git a/Makefile b/Makefile index 67f8995..a4f98d2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CXX=gcc -CXXFLAGS=-g -Wall -Wextra -Werror -fstack-protector-all -pedantic -Wno-unused +CXXFLAGS=-Wall -Wextra -Werror -fstack-protector-all -pedantic -Wno-unused SANITY_FLAGS=-Wfloat-equal -Wshadow -Wpointer-arith -Wstrict-overflow=5 -Wformat=2 SRC_DIR=src/ @@ -9,10 +9,19 @@ HEADERS=$(SRC_DIR)standart.h $(SRC_DIR)extended.h $(SRC_DIR)cpuid.h $(SRC_DIR)pr OUTPUT=cpufetch +all: $(OUTPUT) + +debug: CXXFLAGS += -g -O0 +debug: $(OUTPUT) + +release: CXXFLAGS += -static -O3 +release: $(OUTPUT) + $(OUTPUT): Makefile $(SOURCE) $(HEADERS) $(CXX) $(CXXFLAGS) $(SANITY_FLAGS) $(SOURCE) -o $(OUTPUT) run: ./$(OUTPUT) + clean: @rm $(OUTPUT) diff --git a/src/global.c b/src/global.c index 2529d61..b76ee6f 100644 --- a/src/global.c +++ b/src/global.c @@ -2,10 +2,20 @@ #include #include "global.h" +#ifdef _WIN32 + +#define RED "" +#define BOLD "" +#define RESET "" + +#else + #define RED "\x1b[31;1m" #define BOLD "\x1b[;1m" #define RESET "\x1b[0m" +#endif + #define LOG_LEVEL_NORMAL 0 #define LOG_LEVEL_VERBOSE 1 diff --git a/src/standart.c b/src/standart.c index e3a9dc5..f89a94a 100644 --- a/src/standart.c +++ b/src/standart.c @@ -19,6 +19,7 @@ #define STRING_MEGAHERZ "MHz" #define STRING_GIGAHERZ "GHz" #define STRING_KILOBYTES "KB" +#define STRING_MEGABYTES "MB" #define UNKNOWN -1 @@ -313,6 +314,25 @@ struct cache* get_cache_info(struct cpuInfo* cpu) { } } + // Sanity checks. If we read values greater than this, they can't be valid ones + // The values were chosen by me + if(cach->L1i > 64 * 1024) { + printBug("Invalid L1i size: %dKB\n", cach->L1i/1024); + return NULL; + } + if(cach->L1d > 64 * 1024) { + printBug("Invalid L1d size: %dKB\n", cach->L1d/1024); + return NULL; + } + if(cach->L2 > 2 * 1048576) { + printBug("Invalid L2 size: %dMB\n", cach->L2/(1048576)); + return NULL; + } + if(cach->L3 > 100 * 1048576) { + printBug("Invalid L3 size: %dMB\n", cach->L3/(1048576)); + return NULL; + } + return cach; } @@ -534,39 +554,63 @@ char* get_str_sha(struct cpuInfo* cpu) { // String functions char* get_str_l1(struct cache* cach) { - //Max 2 digits,2 for 'KB',3 for '(D)' and '(I)' - int size = (2*(2+2)+6+1); + // 2*2 for digits, 4 for two 'KB' and 6 for '(D)' and '(I)' + int size = (2*2+4+6+1); + int sanity_ret; char* string = malloc(sizeof(char)*size); - snprintf(string,size,"%d"STRING_KILOBYTES"(D)%d"STRING_KILOBYTES"(I)",cach->L1d/1024,cach->L1i/1024); + sanity_ret = snprintf(string,size,"%d"STRING_KILOBYTES"(D)%d"STRING_KILOBYTES"(I)",cach->L1d/1024,cach->L1i/1024); + assert(sanity_ret > 0); return string; } char* get_str_l2(struct cache* cach) { if(cach->L2 == UNKNOWN) { - char* string = malloc(sizeof(char)*5); - snprintf(string,5,STRING_NONE); + char* string = malloc(sizeof(char) * 5); + snprintf(string, 5, STRING_NONE); return string; } else { - //Max 4 digits and 2 for 'KB' - int size = (4+2+1); - char* string = malloc(sizeof(char)*size); - snprintf(string,size,"%d"STRING_KILOBYTES,cach->L2/1024); + int sanity_ret; + char* string; + if(cach->L2/1024 > 1024) { + //1 for digit, 2 for 'MB' + int size = (1+2+1); + string = malloc(sizeof(char)*size); + sanity_ret = snprintf(string,size,"%d"STRING_MEGABYTES,cach->L2/(1048576)); + } + else { + //4 for digits, 2 for 'KB' + int size = (4+2+1); + string = malloc(sizeof(char)*size); + sanity_ret = snprintf(string,size,"%d"STRING_KILOBYTES,cach->L2/1024); + } + assert(sanity_ret > 0); return string; } } char* get_str_l3(struct cache* cach) { if(cach->L3 == UNKNOWN) { - char* string = malloc(sizeof(char)*5); - snprintf(string,5,STRING_NONE); + char* string = malloc(sizeof(char) * 5); + snprintf(string, 5, STRING_NONE); return string; } else { - //Max 4 digits and 2 for 'KB' - int size = (4+2+1); - char* string = malloc(sizeof(char)*size); - snprintf(string,size,"%d"STRING_KILOBYTES,cach->L3/1024); + int sanity_ret; + char* string; + if(cach->L3/1024 > 1024) { + //1 for digit, 2 for 'MB' + int size = (1+2+1); + string = malloc(sizeof(char)*size); + sanity_ret = snprintf(string,size,"%d"STRING_MEGABYTES,cach->L3/(1048576)); + } + else { + //4 for digits, 2 for 'KB' + int size = (4+2+1); + string = malloc(sizeof(char)*size); + sanity_ret = snprintf(string,size,"%d"STRING_KILOBYTES,cach->L3/1024); + } + assert(sanity_ret > 0); return string; } }