mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 07:50:40 +01:00
[v0.98] Use malloc/calloc wrapper that exits when alloc fails, as suggested by #90
This commit is contained in:
@@ -71,7 +71,7 @@ bool show_debug() {
|
||||
}
|
||||
|
||||
bool show_raw() {
|
||||
return args.raw_flag;
|
||||
return args.raw_flag;
|
||||
}
|
||||
|
||||
bool verbose_enabled() {
|
||||
@@ -83,20 +83,20 @@ int max_arg_str_length() {
|
||||
int len = sizeof(args_str) / sizeof(args_str[0]);
|
||||
for(int i=0; i < len; i++) {
|
||||
max_len = max(max_len, (int) strlen(args_str[i]));
|
||||
}
|
||||
}
|
||||
return max_len;
|
||||
}
|
||||
|
||||
STYLE parse_style(char* style) {
|
||||
uint8_t i = 0;
|
||||
uint8_t styles_count = sizeof(SYTLES_STR_LIST) / sizeof(SYTLES_STR_LIST[0]);
|
||||
|
||||
|
||||
while(i != styles_count && (SYTLES_STR_LIST[i] == NULL || strcmp(SYTLES_STR_LIST[i], style) != 0))
|
||||
i++;
|
||||
|
||||
if(i == styles_count)
|
||||
return STYLE_INVALID;
|
||||
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -109,11 +109,11 @@ void free_colors_struct(struct colors* cs) {
|
||||
}
|
||||
|
||||
bool parse_color(char* optarg_str, struct colors** cs) {
|
||||
*cs = malloc(sizeof(struct colors));
|
||||
(*cs)->c1 = malloc(sizeof(struct color));
|
||||
(*cs)->c2 = malloc(sizeof(struct color));
|
||||
(*cs)->c3 = malloc(sizeof(struct color));
|
||||
(*cs)->c4 = malloc(sizeof(struct color));
|
||||
*cs = emalloc(sizeof(struct colors));
|
||||
(*cs)->c1 = emalloc(sizeof(struct color));
|
||||
(*cs)->c2 = emalloc(sizeof(struct color));
|
||||
(*cs)->c3 = emalloc(sizeof(struct color));
|
||||
(*cs)->c4 = emalloc(sizeof(struct color));
|
||||
struct color** c1 = &((*cs)->c1);
|
||||
struct color** c2 = &((*cs)->c2);
|
||||
struct color** c3 = &((*cs)->c3);
|
||||
@@ -121,43 +121,43 @@ bool parse_color(char* optarg_str, struct colors** cs) {
|
||||
int32_t ret;
|
||||
char* str_to_parse = NULL;
|
||||
bool free_ptr;
|
||||
|
||||
|
||||
if(strcmp(optarg_str, COLOR_STR_INTEL) == 0) {
|
||||
str_to_parse = malloc(sizeof(char) * 46);
|
||||
str_to_parse = emalloc(sizeof(char) * 46);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_INTEL);
|
||||
free_ptr = true;
|
||||
}
|
||||
else if(strcmp(optarg_str, COLOR_STR_AMD) == 0) {
|
||||
str_to_parse = malloc(sizeof(char) * 44);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_AMD);
|
||||
str_to_parse = emalloc(sizeof(char) * 44);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_AMD);
|
||||
free_ptr = true;
|
||||
}
|
||||
else if(strcmp(optarg_str, COLOR_STR_IBM) == 0) {
|
||||
str_to_parse = malloc(sizeof(char) * 45);
|
||||
str_to_parse = emalloc(sizeof(char) * 45);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_IBM);
|
||||
free_ptr = true;
|
||||
}
|
||||
else if(strcmp(optarg_str, COLOR_STR_ARM) == 0) {
|
||||
str_to_parse = malloc(sizeof(char) * 46);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_ARM);
|
||||
str_to_parse = emalloc(sizeof(char) * 46);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_ARM);
|
||||
free_ptr = true;
|
||||
}
|
||||
else {
|
||||
else {
|
||||
str_to_parse = optarg_str;
|
||||
free_ptr = false;
|
||||
}
|
||||
|
||||
ret = sscanf(str_to_parse, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d",
|
||||
|
||||
ret = sscanf(str_to_parse, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d",
|
||||
&(*c1)->R, &(*c1)->G, &(*c1)->B,
|
||||
&(*c2)->R, &(*c2)->G, &(*c2)->B,
|
||||
&(*c3)->R, &(*c3)->G, &(*c3)->B,
|
||||
&(*c4)->R, &(*c4)->G, &(*c4)->B);
|
||||
|
||||
|
||||
if(ret != 12) {
|
||||
printErr("Expected to read 12 values for color but read %d", ret);
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//TODO: Refactor c1->R c2->R ... to c[i]->R
|
||||
if((*c1)->R < 0 || (*c1)->R > 255) {
|
||||
printErr("Red in color 1 is invalid. Must be in range (0, 255)");
|
||||
@@ -182,17 +182,17 @@ bool parse_color(char* optarg_str, struct colors** cs) {
|
||||
if((*c2)->B < 0 || (*c2)->B > 255) {
|
||||
printErr("Blue in color 2 is invalid. Must be in range (0, 255)");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(free_ptr) free (str_to_parse);
|
||||
|
||||
return true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char* build_short_options() {
|
||||
const char *c = args_chr;
|
||||
int len = sizeof(args_chr) / sizeof(args_chr[0]);
|
||||
char* str = (char *) malloc(sizeof(char) * (len*2 + 1));
|
||||
char* str = (char *) emalloc(sizeof(char) * (len*2 + 1));
|
||||
memset(str, 0, sizeof(char) * (len*2 + 1));
|
||||
|
||||
#ifdef ARCH_X86
|
||||
@@ -210,7 +210,7 @@ char* build_short_options() {
|
||||
|
||||
bool parse_args(int argc, char* argv[]) {
|
||||
int opt;
|
||||
int option_index = 0;
|
||||
int option_index = 0;
|
||||
opterr = 0;
|
||||
|
||||
bool color_flag = false;
|
||||
@@ -243,7 +243,7 @@ bool parse_args(int argc, char* argv[]) {
|
||||
printErr("Color option specified more than once");
|
||||
return false;
|
||||
}
|
||||
color_flag = true;
|
||||
color_flag = true;
|
||||
if(!parse_color(optarg, &args.colors)) {
|
||||
printErr("Color parsing failed");
|
||||
return false;
|
||||
@@ -276,7 +276,7 @@ bool parse_args(int argc, char* argv[]) {
|
||||
else if(opt == args_chr[ARG_VERSION]) {
|
||||
args.version_flag = true;
|
||||
}
|
||||
else {
|
||||
else {
|
||||
printWarn("Invalid options");
|
||||
args.help_flag = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user