diff --git a/main.c b/main.c index 43b4a68..7b2ba70 100644 --- a/main.c +++ b/main.c @@ -26,6 +26,7 @@ #include #include #include +#include #include // Removes a directory using the Windows shell @@ -92,8 +93,17 @@ void getAdditionalDirs(){ // Check if the specified path is an environment variable. // If it starts with %, then we should treat it as such. - if (currentPath[0] == '%'){ - HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); + + bool containsVars = false; + + for (unsigned c = 0; c < strlen(currentPath); c++) + if (currentPath[c] == '%'){ + containsVars = true; + break; + } + + if (containsVars){ + /*HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); WORD wColor; CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(h, &info); @@ -101,25 +111,57 @@ void getAdditionalDirs(){ SetConsoleTextAttribute(h, FOREGROUND_RED); printf("%s\n\n", STR_ERROR_VAR_CURRENTLY_NOT_SUPPORTED); - SetConsoleTextAttribute(h, wColor); + SetConsoleTextAttribute(h, wColor);*/ + /*char varBuffer[sizeof(currentPath)]; memcpy(varBuffer, ¤tPath[1], strlen(currentPath)-3); - varBuffer[sizeof(varBuffer)] = '\0'; + varBuffer[sizeof(varBuffer)] = '\0';*/ - char varResBuffer[32767]; - GetEnvironmentVariable( - varBuffer, - &varResBuffer, - sizeof(varResBuffer) - ); - printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, varResBuffer); - cleanDir(varResBuffer);*/ + char* buffer; + char* varBuffer; + + if ((buffer = malloc(512)) == NULL || + (varBuffer = malloc(64)) == NULL){ + printf("%s\n", STR_MEM_ERR); + } + + buffer[0] = '\0'; + varBuffer[0] = '\0'; + bool fetchingVar = false; + + for (unsigned c = 0; c < strlen(currentPath); c++){ + if (currentPath[c] == '%') + if (!fetchingVar) + fetchingVar = true; + else { + // Get the environment variable + + char varValBuffer[256]; + GetEnvironmentVariable( + varBuffer, + &varValBuffer, + sizeof(varValBuffer) + ); + + strncat(buffer, &varValBuffer, strlen(varValBuffer)); + + fetchingVar = false; + } + else if (fetchingVar) + strncat(varBuffer, ¤tPath[c], 1); + else + strncat(buffer, ¤tPath[c], 1); + } + + + printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, buffer); + //cleanDir(varResBuffer); continue; } printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, currentPath); - cleanDir(currentPath); + //cleanDir(currentPath); } } diff --git a/str.h b/str.h index 50c2ee9..05f7a23 100644 --- a/str.h +++ b/str.h @@ -37,6 +37,8 @@ #define STR_FAILED_FILE "[Arquivo mantido] Não foi possível remover este arquivo pelo seguinte erro:" #define STR_FAILED_DIR "[Pasta mantida] Não foi possível remover esta pasta pelo seguinte erro:" +#define STR_MEM_ERR "[Erro fatal] Não foi possível alocar memória. Tente fechar alguns programas abertos e tente novamente." + #define STR_ERROR_LIC "Falha ao ler arquivo de licença. Visite para mais informações." #define STR_ERROR_VAR_CURRENTLY_NOT_SUPPORTED "[Aviso] O suporte para variáveis de sistema não existe nesta versão."