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

172 lines
4.6 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"
// Because this program is currently target at Windows only, the use of ANSI colors are no longer used as of May 5, 2020.
// #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>
#include <string.h>
2020-04-26 21:55:11 +00:00
2020-06-19 15:12:45 +00:00
// Removes a directory using the Windows shell
int shellRemoveDir(LPCTSTR dirPath){
// Remove the directory
//printf("!! ATTEMPT TO REMOVE (%d) %s\\...\n", len, shortPath);
2020-06-19 15:12:45 +00:00
// Append null to the end of the path
// Double null terminated path string
char dntps[strlen(dirPath) + 2];
strcpy(dntps, dirPath);
strcat(dntps, "\\\0");
SHFILEOPSTRUCT dir = {0};
dir.wFunc = FO_DELETE;
dir.pFrom = dntps;
dir.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI;
dir.fAnyOperationsAborted = 0;
/*dir = {
2020-06-19 15:12:45 +00:00
NULL,
FO_DELETE,
dntps,
NULL,
FOF_NOCONFIRMATION | FOF_NOERRORUI,
0,
0,
""
};*/
2020-06-19 15:12:45 +00:00
return SHFileOperation(&dir);
}
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-19 14:12:21 +00:00
// Gets the user defined directories
2020-04-26 21:55:11 +00:00
void getAdditionalDirs(){
printf("\n%s\n", STR_FETCHING_ADD_DIRS);
int dirs;
dirs = GetPrivateProfileInt("Pastas", "Pastas", 3, PATH_CFG);
printf("%s %d\n", STR_DIR_COUNT, dirs);
fflush(stdout);
for(unsigned short x = 0; x < dirs; x++){
char dirKey[7] = "Pasta";
char* intPtr = (x + 1) + '0';
strcat(dirKey, &intPtr);
_TCHAR currentPath[128];
GetPrivateProfileString("Pastas", dirKey, "null", currentPath,
sizeof(currentPath) / sizeof(currentPath[0]), PATH_CFG);
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, currentPath);
cleanDir(currentPath);
}
2020-04-26 21:55:11 +00:00
}
2020-06-19 14:12:21 +00:00
// Cleans a directory.
void cleanDir(const char* path){
2020-06-10 15:53:08 +00:00
// Path with wildcard
char wildPath[strlen(path) + 1];
strcpy(wildPath, path);
strcat(wildPath, "*");
printf("%s %s\n", STR_CLEANING_DIR, wildPath);
// Fetch files in dir
WIN32_FIND_DATA data;
2020-06-10 15:53:08 +00:00
HANDLE hFind = FindFirstFile(wildPath, &data);
unsigned int iteration = 0;
if (hFind != INVALID_HANDLE_VALUE){
do{
// Ignore pseudo dirs (. and ..)
iteration++;
if (iteration <= 2)
continue;
printf("%s %s\n", STR_FOUND_ITEM, data.cFileName);
2020-06-19 15:12:45 +00:00
// Attempt to delete item...
2020-06-10 15:53:08 +00:00
int pathSize = strlen(wildPath) + strlen(data.cFileName);
char fullPath[pathSize];
strcpy(fullPath, path);
strcat(fullPath, data.cFileName);
2020-06-19 15:12:45 +00:00
// If the item is a directory, then delete it through the shell
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
int shellReturnCode = shellRemoveDir(fullPath);
if (shellReturnCode != 0){
printf(" %s\n", STR_FAILED_DIR);
printf(" --> %s\n", fullPath);
printf(" --> %#010x\n", shellReturnCode);
}
continue;
}
2020-06-10 15:53:08 +00:00
if (DeleteFile(fullPath) == 0){
2020-06-19 15:12:45 +00:00
int lastError = GetLastError();
2020-06-10 15:53:08 +00:00
printf(" %s\n", STR_FAILED_FILE);
printf(" --> %s\n", fullPath);
2020-06-19 15:12:45 +00:00
printf(" --> %#010x\n", lastError);
2020-06-10 15:53:08 +00:00
} else
printf(" %s\n", STR_DELETED_FILE);
} while(FindNextFile(hFind, &data));
} else {
printf("%s\n", STR_FAILED_DIR_LIST);
}
2020-04-26 21:55:11 +00:00
}
int main(void){
SetConsoleOutputCP(CP_UTF8);
2020-05-23 05:58:36 +00:00
printLicense(0);
2020-04-26 21:55:11 +00:00
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);
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-04-26 21:55:11 +00:00
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;
}
getAdditionalDirs();
return 0;
2020-04-26 21:55:11 +00:00
}