This repository has been archived on 2020-12-25. You can view files and clone it, but cannot push or open issues or pull requests.
ldl3/main.c

276 lines
8.0 KiB
C
Raw Normal View History

2020-04-26 21:55:11 +00:00
/*
limpador do luca0N! 3
Copyright (C) 2020 luca0N!
2020-04-26 21:55:11 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2020-04-26 21:55:11 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2020-04-26 21:55:11 +00:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-04-26 21:55:11 +00:00
2020-05-24 03:10:21 +00:00
Contact me by e-mail via <mailto:luca0n@protonmail.com>.
2020-04-26 21:55:11 +00:00
*/
#include "about.h"
#include "str.h"
#include "cfg.h"
2020-06-27 05:01:02 +00:00
// Because this program is currently targeted at Windows only, the use of ANSI colors are no longer used as of May 5, 2020.
2020-06-27 16:04:46 +00:00
#include "colors.h"
2020-04-26 21:55:11 +00:00
#include <windows.h>
2020-05-11 16:35:29 +00:00
#include <tchar.h>
2020-04-26 21:55:11 +00:00
#include <stdio.h>
2020-06-26 04:14:19 +00:00
#include <stdbool.h>
#include <string.h>
2020-06-27 06:08:22 +00:00
#include <math.h>
2020-04-26 21:55:11 +00:00
2020-06-27 16:04:46 +00:00
void setConsoleColor(int consoleColor){
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
WORD wColor;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(h, &info);
wColor = info.wAttributes;
SetConsoleTextAttribute(h, consoleColor);
}
2020-06-19 14:12:21 +00:00
// Prints the program license to the user.
2020-04-26 21:55:11 +00:00
int printLicense(int full){
if (full == 0) {
2020-05-11 16:35:29 +00:00
printf("%s\n", LICENSE);
return 0;
} else {
FILE* flic = fopen(PATH_LICENSE, "r");
if (flic == NULL){
printf("%s\n", STR_ERROR_LIC);
return -1;
}
char c;
while ((c = fgetc(flic)) != EOF) {
printf("%c", c);
}
}
2020-04-26 21:55:11 +00:00
}
2020-06-26 04:49:46 +00:00
// Cleans a directory.
2020-06-27 06:08:22 +00:00
void cleanDir(const char* path, unsigned* objectCount, unsigned long* totalDeletedBytes){
2020-06-26 04:49:46 +00:00
const char* validPath = path;
//char validPath[sizeof(path) + 1];
//strcpy(validPath, path);
// Check if the path ends with '\'
// Because the last character of a string is null, we should check the character before that one.
if (path[strlen(path) - 1] != '\\'){
2020-06-26 04:49:46 +00:00
strcat(validPath, "\\");
}
2020-06-26 04:49:46 +00:00
// Path with wildcard
char wildPath[strlen(validPath) + 1];
strcpy(wildPath, validPath);
strcat(wildPath, "*");
2020-06-27 16:12:52 +00:00
printf(STR_CLEANING_DIR, wildPath);
2020-06-26 04:49:46 +00:00
// Fetch files in dir
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile(wildPath, &data);
unsigned int iteration = 0;
if (hFind != INVALID_HANDLE_VALUE){
do{
// Ignore pseudo dirs (. and ..)
iteration++;
if (iteration <= 2)
continue;
2020-06-27 16:12:52 +00:00
printf(STR_FOUND_ITEM, data.cFileName);
2020-06-26 04:49:46 +00:00
// Attempt to delete item...
int pathSize = strlen(wildPath) + strlen(data.cFileName);
char fullPath[pathSize];
strcpy(fullPath, validPath);
strcat(fullPath, data.cFileName);
// If the item is a directory, call cleanDir on that directory.
2020-06-26 04:49:46 +00:00
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
2020-06-27 06:08:22 +00:00
cleanDir(fullPath, objectCount, totalDeletedBytes);
/*int shellReturnCode = shellRemoveDir(fullPath);
2020-06-26 04:49:46 +00:00
if (shellReturnCode != 0){
printf(" %s\n", STR_FAILED_DIR);
printf(" --> %s\n", fullPath);
printf(" --> %#010x\n", shellReturnCode);
}*/
2020-06-26 04:49:46 +00:00
continue;
}
2020-06-27 06:08:22 +00:00
// Get the length of the file before deleting it.
HANDLE fileHandle = CreateFile(fullPath, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
LARGE_INTEGER size;
unsigned long length = 0;
if (fileHandle != INVALID_HANDLE_VALUE){
if (GetFileSizeEx(fileHandle, &size)){
length = size.QuadPart;
}
CloseHandle(fileHandle);
}
2020-06-26 04:49:46 +00:00
if (DeleteFile(fullPath) == 0){
2020-06-27 16:04:46 +00:00
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_RED);
2020-06-26 04:49:46 +00:00
int lastError = GetLastError();
printf(" %s\n", STR_FAILED_FILE);
printf(" --> %s\n", fullPath);
printf(" --> %#010x\n", lastError);
2020-06-27 16:04:46 +00:00
setConsoleColor(RESET_CONSOLE_COLOR);
} else {
2020-06-27 16:04:46 +00:00
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_BLUE);
2020-06-27 16:12:52 +00:00
printf(STR_DELETED_FILE);
// Update the deleted object count if the process was successful.
(*objectCount)++;
2020-06-27 06:08:22 +00:00
(*totalDeletedBytes) += length;
2020-06-27 16:04:46 +00:00
setConsoleColor(RESET_CONSOLE_COLOR);
}
2020-06-26 04:49:46 +00:00
} while(FindNextFile(hFind, &data));
} else {
printf("%s\n", STR_FAILED_DIR_LIST);
}
}
2020-06-19 14:12:21 +00:00
// Gets the user defined directories
2020-06-26 04:28:46 +00:00
void getAdditionalDirs(bool safeMode){
unsigned* objectCount = 0;
2020-06-27 06:08:22 +00:00
unsigned long* totalDeletedBytes = 0;
printf("\n%s\n", STR_FETCHING_ADD_DIRS);
int dirs;
dirs = GetPrivateProfileInt("Pastas", "Pastas", 3, PATH_CFG);
2020-06-27 16:12:52 +00:00
printf(STR_DIR_COUNT, dirs);
fflush(stdout);
for(unsigned short x = 0; x < dirs; x++){
char dirKey[7] = "Pasta";
2020-06-26 04:49:46 +00:00
char* intPtr = (char*) (x + 1) + '0';
strcat(dirKey, (char*) &intPtr);
_TCHAR currentPath[128];
GetPrivateProfileString("Pastas", dirKey, "null", currentPath,
sizeof(currentPath) / sizeof(currentPath[0]), PATH_CFG);
2020-06-20 01:50:58 +00:00
// Check if the specified path is an environment variable.
// If it starts with %, then we should treat it as such.
2020-06-26 04:14:19 +00:00
bool containsVars = false;
for (unsigned c = 0; c < strlen(currentPath); c++)
if (currentPath[c] == '%'){
containsVars = true;
break;
}
if (containsVars){
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, &currentPath[c], 1);
else
strncat(buffer, &currentPath[c], 1);
}
2020-06-20 01:50:58 +00:00
2020-06-26 04:14:19 +00:00
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, buffer);
2020-06-26 04:28:46 +00:00
if (!safeMode)
2020-06-27 06:08:22 +00:00
cleanDir(buffer, &objectCount, &totalDeletedBytes);
2020-06-26 04:28:46 +00:00
free(buffer);
free(varBuffer);
2020-06-20 01:50:58 +00:00
continue;
}
2020-06-27 16:12:52 +00:00
printf(STR_FOUND_DIR, dirKey, currentPath);
2020-06-20 01:50:58 +00:00
2020-06-26 04:28:46 +00:00
if (!safeMode)
2020-06-27 06:08:22 +00:00
cleanDir(currentPath, &objectCount, &totalDeletedBytes);
}
2020-06-27 16:04:46 +00:00
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_GREEN);
2020-06-27 16:12:52 +00:00
printf(STR_OBJECT_COUNT, objectCount);
2020-06-27 06:09:45 +00:00
// Determine whether we should output the size as bytes, KiB, MiB or GiB.
2020-06-27 06:08:22 +00:00
if (totalDeletedBytes < 1024){
2020-06-27 16:12:52 +00:00
printf(STR_TOTAL_DELETED_BYTES, totalDeletedBytes);
2020-06-27 06:08:22 +00:00
} else if (totalDeletedBytes < (unsigned long) pow(1024, 2)){
float kib = (unsigned long) totalDeletedBytes / pow(1024, 1);
2020-06-27 16:12:52 +00:00
printf(STR_TOTAL_DELETED_KIBIBYTES, kib);
2020-06-27 06:08:22 +00:00
} else if (totalDeletedBytes < (unsigned long) pow(1024, 3)){
float mib = (unsigned long) totalDeletedBytes / pow(1024, 2);// / (float) 1024 / (float) 1024;
2020-06-27 16:12:52 +00:00
printf(STR_TOTAL_DELETED_MEBIBYTES, mib);
2020-06-27 06:08:22 +00:00
} else {
float gib = (unsigned long) totalDeletedBytes / pow(1024, 3);
2020-06-27 16:12:52 +00:00
printf(STR_TOTAL_DELETED_GIBIBYTES, gib);
2020-06-27 06:08:22 +00:00
}
2020-06-27 16:04:46 +00:00
setConsoleColor(RESET_CONSOLE_COLOR);
2020-04-26 21:55:11 +00:00
}
int main(void){
SetConsoleOutputCP(CP_UTF8);
2020-06-27 16:04:46 +00:00
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_YELLOW);
printLicense(0);
2020-06-27 16:04:46 +00:00
setConsoleColor(RESET_CONSOLE_COLOR);
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_BLUE);
2020-05-23 05:58:36 +00:00
printf("%s\n%s %d, %s\n", STR_LN, PROGRAM_NAME, PROGRAM_SEASON, PROGRAM_VERSION);
2020-05-24 01:17:51 +00:00
printf("%s\n%s\n\n", PROGRAM_COPYRIGHT, STR_LN);
2020-06-27 16:04:46 +00:00
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_YELLOW);
2020-06-27 16:12:52 +00:00
printf(STR_TAMPER_WARNING);
unsigned int ignoreWarning = GetPrivateProfileInt("Geral", "IgnorarAlerta", 0, PATH_CFG);
unsigned int licenseAccepted = GetPrivateProfileInt("Geral", "AceitarLicença", 0, PATH_CFG);
if (!ignoreWarning || !licenseAccepted){
printf("%s ", STR_USE_WARNING, ignoreWarning, licenseAccepted);
char in;
fflush(stdout);
2020-06-27 16:04:46 +00:00
setConsoleColor(RESET_CONSOLE_COLOR);
scanf("%c", &in);
2020-04-26 21:55:11 +00:00
if (in == 'w' || in == 'c')
if (printLicense(1) != 0)
return -2;
if (in != 's' && in != 'S' && in != 'y' && in != 'Y')
return 0;
}
2020-06-27 16:04:46 +00:00
setConsoleColor(RESET_CONSOLE_COLOR);
2020-06-26 04:28:46 +00:00
unsigned int safeMode = GetPrivateProfileInt("Geral", "ModoSeguro", 1, PATH_CFG);
getAdditionalDirs(safeMode);
return 0;
2020-04-26 21:55:11 +00:00
}