#include "rename.h"
#include "config.h"
-glob_t get_files(const char* dir_path)
+glob_t _get_files(const char* dir_path)
{
glob_t files;
char* pattern = malloc(sizeof(char) * strlen(dir_path) + 3);
return files;
}
-bool is_dir(const char* path)
+bool _is_dir(const char* path)
{
struct stat statbuf;
return isdir;
}
-void rename_file(const char* input, const char* output)
+void _rename_file(const char* input, const char* output)
{
if (strcmp(input, output) == 0)
return;
rename(input, output);
}
-void rename_files(const char* dir_path, rename_ptr renamefunc)
+int _rename_files(const char* dir_path, rename_ptr renamefunc)
{
- glob_t files = get_files(dir_path);
+ glob_t files = _get_files(dir_path);
for (size_t i = 0; i < files.gl_pathc; i++) {
char* output = strdup(files.gl_pathv[i]);
output = renamefunc(&output);
- rename_file(files.gl_pathv[i], output);
+ _rename_file(files.gl_pathv[i], output);
- if (is_dir(output)) {
+ if (_is_dir(output)) {
output = escape(&output);
- rename_files(output, renamefunc);
+ _rename_files(output, renamefunc);
}
free(output);
}
globfree(&files);
+ return EXIT_SUCCESS;
+}
+
+bool _validate_number_of_args(int argc)
+{
+ if (argc > 1 && argc < 4)
+ return true;
+
+ printf("ERROR: %s <dir> [<command>]\n", PROJECT_NAME);
+ return false;
+}
+
+bool _validate_command(const char* cmd)
+{
+ if (strcmp(cmd, "lower") == 0)
+ return true;
+
+ if (strcmp(cmd, "point") == 0)
+ return true;
+
+ if (strcmp(cmd, "revert") == 0)
+ return true;
+
+ printf("ERROR: Unknown command\n");
+ return false;
}
int main(int argc, char* argv[])
{
printf("Version %s\n", VERSION);
- if (argc < 2) {
- printf("ERROR: %s <dir>\n", argv[0]);
+
+ if (!_validate_number_of_args(argc))
return EXIT_FAILURE;
- }
const char* dir = argv[1];
- if (argc == 2) {
- rename_files(dir, rename_string);
- return EXIT_SUCCESS;
- }
+ if (argc == 2)
+ return _rename_files(dir, rename_string);
const char* cmd = argv[2];
- if (strcmp(cmd, "lower") == 0) {
- rename_files(dir, rename_lower);
- return EXIT_SUCCESS;
- }
+ if (!_validate_command(cmd))
+ return EXIT_FAILURE;
- if (strcmp(cmd, "point") == 0) {
- rename_files(dir, rename_point);
- return EXIT_SUCCESS;
- }
+ if (strcmp(cmd, "lower") == 0)
+ return _rename_files(dir, rename_lower);
- if (strcmp(cmd, "revert") == 0) {
- rename_files(dir, rename_revert);
- return EXIT_SUCCESS;
- }
+ if (strcmp(cmd, "point") == 0)
+ return _rename_files(dir, rename_point);
- return EXIT_SUCCESS;
+ if (strcmp(cmd, "revert") == 0)
+ return _rename_files(dir, rename_revert);
}
\ No newline at end of file