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" #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 { struct emkfileData {
char cc; char cc;
char prefix; 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 " | use a config.emk file, rather\n");
printf(COL_YELLOW "WRN" COL_RESET " | than embedding settings directly\n"); printf(COL_YELLOW "WRN" COL_RESET " | than embedding settings directly\n");
printf(COL_YELLOW "WRN" COL_RESET " | within the emkfile.\n"); printf(COL_YELLOW "WRN" COL_RESET " | within the emkfile.\n");
emkConfigExists = 1;
} else if (getcwd(cwd, sizeof(cwd)) != NULL) { } else if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf(COL_BLUE "INF" COL_RESET " | using config.emk at %s/config.emk\n", cwd); 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; FILE *emkconfig, *emkfile, *emkfile_new;
char filename[100], contents; char filename[100], contents;
if (emkConfigExists == 1) {
printf(COL_BLUE "INF" COL_RESET " | parsing config.emk\n"); printf(COL_BLUE "INF" COL_RESET " | parsing config.emk\n");
emkconfig = fopen(EMKCONFIG, "r"); emkconfig = fopen(EMKCONFIG, "r");
}
printf(COL_BLUE "INF" COL_RESET " | parsing emkfile\n"); printf(COL_BLUE "INF" COL_RESET " | parsing emkfile\n");
emkfile = fopen(EMKFILE, "r"); emkfile = fopen(EMKFILE, "r");
@ -95,11 +104,13 @@ char includeConfig() {
printf(COL_BLUE "INF" COL_RESET " | running preprocessing tasks...\n"); printf(COL_BLUE "INF" COL_RESET " | running preprocessing tasks...\n");
/* write emkconfig to new emkfile */ /* write emkconfig to new emkfile */
if (emkConfigExists == 1) {
contents = fgetc(emkconfig); contents = fgetc(emkconfig);
while (contents != EOF) { while (contents != EOF) {
fputc(contents, emkfile_new); fputc(contents, emkfile_new);
contents = fgetc(emkconfig); contents = fgetc(emkconfig);
} }
}
/* write emkfile to new emkfile */ /* write emkfile to new emkfile */
contents = fgetc(emkfile); contents = fgetc(emkfile);
@ -109,7 +120,9 @@ char includeConfig() {
} }
printf(COL_BLUE "INF" COL_RESET " | closing files\n"); printf(COL_BLUE "INF" COL_RESET " | closing files\n");
if (emkConfigExists == 1) {
fclose(emkconfig); fclose(emkconfig);
}
fclose(emkfile); fclose(emkfile);
fclose(emkfile_new); fclose(emkfile_new);
} }