Added environment variables support

This commit is contained in:
luca0N! 2020-06-26 01:14:19 -03:00
parent 4c77ea405e
commit bee75691d5
No known key found for this signature in database
GPG Key ID: 9F1B99B522287CAB
2 changed files with 57 additions and 13 deletions

68
main.c
View File

@ -26,6 +26,7 @@
#include <windows.h> #include <windows.h>
#include <tchar.h> #include <tchar.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <string.h> #include <string.h>
// Removes a directory using the Windows shell // Removes a directory using the Windows shell
@ -92,8 +93,17 @@ void getAdditionalDirs(){
// Check if the specified path is an environment variable. // Check if the specified path is an environment variable.
// If it starts with %, then we should treat it as such. // 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; WORD wColor;
CONSOLE_SCREEN_BUFFER_INFO info; CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(h, &info); GetConsoleScreenBufferInfo(h, &info);
@ -101,25 +111,57 @@ void getAdditionalDirs(){
SetConsoleTextAttribute(h, FOREGROUND_RED); SetConsoleTextAttribute(h, FOREGROUND_RED);
printf("%s\n\n", STR_ERROR_VAR_CURRENTLY_NOT_SUPPORTED); printf("%s\n\n", STR_ERROR_VAR_CURRENTLY_NOT_SUPPORTED);
SetConsoleTextAttribute(h, wColor); SetConsoleTextAttribute(h, wColor);*/
/*char varBuffer[sizeof(currentPath)]; /*char varBuffer[sizeof(currentPath)];
memcpy(varBuffer, &currentPath[1], strlen(currentPath)-3); memcpy(varBuffer, &currentPath[1], strlen(currentPath)-3);
varBuffer[sizeof(varBuffer)] = '\0'; varBuffer[sizeof(varBuffer)] = '\0';*/
char varResBuffer[32767]; char* buffer;
GetEnvironmentVariable( char* varBuffer;
varBuffer,
&varResBuffer, if ((buffer = malloc(512)) == NULL ||
sizeof(varResBuffer) (varBuffer = malloc(64)) == NULL){
); printf("%s\n", STR_MEM_ERR);
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, varResBuffer); }
cleanDir(varResBuffer);*/
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, &currentPath[c], 1);
else
strncat(buffer, &currentPath[c], 1);
}
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, buffer);
//cleanDir(varResBuffer);
continue; continue;
} }
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, currentPath); printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, currentPath);
cleanDir(currentPath); //cleanDir(currentPath);
} }
} }

2
str.h
View File

@ -37,6 +37,8 @@
#define STR_FAILED_FILE "[Arquivo mantido] Não foi possível remover este arquivo pelo seguinte erro:" #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_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 <https://www.gnu.org/licenses/> para mais informações." #define STR_ERROR_LIC "Falha ao ler arquivo de licença. Visite <https://www.gnu.org/licenses/> 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." #define STR_ERROR_VAR_CURRENTLY_NOT_SUPPORTED "[Aviso] O suporte para variáveis de sistema não existe nesta versão."