fix segfault when config.emk does not exist

This commit is contained in:
Liam Waldron 2023-05-04 13:13:06 -04:00
parent 2a579f070e
commit 8dcce562cd

View File

@ -26,6 +26,12 @@
#include "parse.h"
/* Variable indicating whether config.emk exists
* 1 means config.emk exists
* 0 means config.emk does not exist
*/
int emkConfigExists;
struct emkfileData {
char cc;
char prefix;
@ -60,6 +66,7 @@ char checkForFiles() {
printf(COL_YELLOW "WRN" COL_RESET " | use a config.emk file, rather\n");
printf(COL_YELLOW "WRN" COL_RESET " | than embedding settings directly\n");
printf(COL_YELLOW "WRN" COL_RESET " | within the emkfile.\n");
emkConfigExists = 1;
} else if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf(COL_BLUE "INF" COL_RESET " | using config.emk at %s/config.emk\n", cwd);
}
@ -85,8 +92,10 @@ char includeConfig() {
FILE *emkconfig, *emkfile, *emkfile_new;
char filename[100], contents;
if (emkConfigExists == 1) {
printf(COL_BLUE "INF" COL_RESET " | parsing config.emk\n");
emkconfig = fopen(EMKCONFIG, "r");
}
printf(COL_BLUE "INF" COL_RESET " | parsing emkfile\n");
emkfile = fopen(EMKFILE, "r");
@ -95,11 +104,13 @@ char includeConfig() {
printf(COL_BLUE "INF" COL_RESET " | running preprocessing tasks...\n");
/* write emkconfig to new emkfile */
if (emkConfigExists == 1) {
contents = fgetc(emkconfig);
while (contents != EOF) {
fputc(contents, emkfile_new);
contents = fgetc(emkconfig);
}
}
/* write emkfile to new emkfile */
contents = fgetc(emkfile);
@ -109,7 +120,9 @@ char includeConfig() {
}
printf(COL_BLUE "INF" COL_RESET " | closing files\n");
if (emkConfigExists == 1) {
fclose(emkconfig);
}
fclose(emkfile);
fclose(emkfile_new);
}