It's been some time since I wrote something in C. I am looking for some advices related to my code in terms of coding style, bad/wrong logic, optimization (performance) and good practices.
The code speaks for itself, but if there are any questions, I'll add some comments to make it clearer.
#include <stdio.h>
#include <string.h>
#include <windows.h>
#define START_COUNTING 0
#define NEW_FILENAME_MAXCHARS 25
#define CURRENT_INDEX_MAXCHAR 25
typedef unsigned int uint;
// Basic logger.
struct Logger
{
uint failed;
uint succeeded;
};
void print_warning(void);
// Add a token at the end of a file
char *add_token(char *path, char *token);
void rename_files(char *path);
// If the cwd is not the path where the files are located
// than change the cwd to that path.
void check_dir(const char *path);
int main(int argc, char *argv[])
{
print_warning();
if(argc != 2)
{
printf("Usage: <program> <path>\n");
exit(EXIT_FAILURE);
}
rename_files(argv[1]);
return 0;
}
void check_dir(const char *path)
{
char *buffer = _getcwd(NULL, 0);
if(buffer == NULL)
{
printf("FAILED TO GET THE CURRENT WORKING DIRECTORY\n");
exit(EXIT_FAILURE);
}
else
{
if(strcmp(buffer, path) != 0)
{
#ifdef DEBUG
printf("Changing path to: %s\n", path);
#endif
_chdir(path);
}
}
free(buffer);
}
char *add_token(char *path, char *token)
{
char *new_array = malloc(strlen(path) + strlen(token) + 1);
if(new_array == NULL)
{
printf("MALLOC FAILED");
exit(EXIT_FAILURE);
}
strcpy(new_array, path);
strcat(new_array, token);
return new_array;
}
void rename_files(char *path)
{
// INIT FUNCTIONS
check_dir(path);
path = add_token(path, "\\*");
// INIT DATA
WIN32_FIND_DATA find_files;
HANDLE handle_FindFiles = FindFirstFile(path, &find_files);
uint current_index = START_COUNTING;
// INIT LOGGER
struct Logger logger;
logger.failed = 0;
logger.succeeded = 0;
if(handle_FindFiles == INVALID_HANDLE_VALUE)
{
printf("FAIL TO FIND FIRST FILE IN DIRECTORY: %s\n", path);
exit(EXIT_FAILURE);
}
else
{
while(FindNextFile(handle_FindFiles, &find_files) != 0)
{
// TODO: Add support for renaming files in directories.
// Make sure we don't rename a directory.
if(!(find_files.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
current_index++;
// Get the extenstion of the file.
char *ext = strrchr(find_files.cFileName, '.') + 1;
#ifdef DEBUG
printf("%s => %u.%s\n", find_files.cFileName, current_index, ext);
#endif
// Conver the current index to a string
char current_index_str[CURRENT_INDEX_MAXCHAR];
// Store the file's new name here.
char new_filename[NEW_FILENAME_MAXCHARS];
// Conver the index
sprintf(current_index_str, "%d", current_index);
// Construct the new file name
strcpy(new_filename, current_index_str);
strcat(new_filename, ".");
strcat(new_filename, ext);
int result = rename(find_files.cFileName, new_filename);
if(!result)
{
#ifdef DEBUG
printf("FAILED TO RENAME FILE %s with new name %s\n", find_files.cFileName, new_filename);
#endif
logger.failed++;
}
else
{
logger.succeeded++;
}
}
}
}
// DEBUG INFO.
#ifdef DEBUG
printf("\n---------------------------\n");
printf("INFO\n");
printf("FAILED: %10u\n", logger.failed);
printf("SUCCEEDED: %10u\n", logger.succeeded);
#endif // DEBUG
printf("Done renaming files\n");
free(path);
}
void print_warning(void)
{
printf("USE THIS PROGRAM AT YOUR OWN RISK\n");
printf("IT MAY DAMAGE FILES AND/OR CAUSE DELETE OF DATA\n");
printf("THE AUTHOR OF THIS PROGRAM IS NOT RESPOSABLE OF ANY DAMAGE"
"DONE BY THE PROGRAM OR OF YOUR ACTIONS WITH IT\n");
}
Any constructive critique is welcomed.