Skip to content

Commit

Permalink
config. getenv variables
Browse files Browse the repository at this point in the history
Signed-off-by: Viktar Lukashonak <[email protected]>
  • Loading branch information
LukashonakV committed May 21, 2024
1 parent a2f314d commit d2ab056
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,41 @@ bool load_config(char configPath[PATH_MAX], struct config_params *p, bool colors
}

} else { // opening specified file
// expand env variables in configPath
char *cfgPathOrig = (char*)malloc(PATH_MAX*sizeof(char));
strcpy(cfgPathOrig, configPath);
configPath[0] = '\0';

const char *delimiter = "/";
char *pch_1 = cfgPathOrig;
char *pch_2 = strpbrk(pch_1 + 1, delimiter); // search for the first delimiter
char *env = NULL;
int lag = 0; // manages the lag where to look $ after strpbrk

while (pch_2 != NULL) {
char *dest = (char*)malloc((pch_2 - pch_1 - 1)*sizeof(char));
strncpy(dest, pch_1, (pch_2 - pch_1));

if (*(dest + lag) == '$') {
env = getenv(dest + lag + 1);

if (env != NULL) {
if (lag > 0 && env != delimiter)
strcat(configPath, delimiter);
strcat(configPath, env);
} else
strcat(configPath, dest);
} else
strcat(configPath, dest);

free(dest);
lag = 1;
pch_1 = pch_2;
pch_2 = strpbrk(pch_1 + 1, delimiter);
}

strcat(configPath, pch_1);
free(cfgPathOrig);

fp = fopen(configPath, "rb");
if (fp) {
Expand Down

0 comments on commit d2ab056

Please sign in to comment.