Functionalize functionalities
This commit is contained in:
parent
9a885a549d
commit
f6593511e2
|
@ -6,10 +6,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
|||
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake" ${CMAKE_MODULE_PATH})
|
||||
|
||||
# Set C compiler options
|
||||
if (NOT CODE_COVERAGE)
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3")
|
||||
endif(NOT CODE_COVERAGE)
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3")
|
||||
|
||||
add_executable(fixmydl src/main.c)
|
||||
target_compile_options(fixmydl PUBLIC -Wall -Werror -Wno-unused-function -pedantic)
|
||||
|
|
155
src/main.c
155
src/main.c
|
@ -12,44 +12,26 @@
|
|||
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))
|
||||
|
||||
static const char downloads_dir_default[] = "Downloads";
|
||||
static char downloads_dir[4096];
|
||||
static char *downloads_dir;
|
||||
|
||||
static const char documents_dir_default[] = "Documents";
|
||||
static char *documents_dir;
|
||||
|
||||
static int inotify_instance = -1;
|
||||
static char event_buffer[EVENT_BUF_LEN];
|
||||
static int watch_list;
|
||||
|
||||
int main()
|
||||
{
|
||||
/* Get downloads directory. */
|
||||
FILE *xdg_cmd = NULL;
|
||||
xdg_cmd = popen("xdg-user-dir DOWNLOAD", "r");
|
||||
if (!xdg_cmd) {
|
||||
/* Default Download folder */
|
||||
char *home_dir = getenv("HOME");
|
||||
if (!home_dir) {
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (!pw)
|
||||
perror("Failed to get home directory!");
|
||||
home_dir = pw->pw_dir;
|
||||
if (!home_dir)
|
||||
perror("Failed miserably to get downloads directory!");
|
||||
}
|
||||
strcat(downloads_dir, home_dir);
|
||||
strcat(downloads_dir, "/");
|
||||
strcat(downloads_dir, downloads_dir_default);
|
||||
} else {
|
||||
/* Read path from output of xdg */
|
||||
int c, n = 0;
|
||||
while ((c = fgetc(xdg_cmd)) != EOF) {
|
||||
if (c == '\n')
|
||||
downloads_dir[n++] = '\0';
|
||||
else
|
||||
downloads_dir[n++] = (char)c;
|
||||
}
|
||||
downloads_dir[n] = '\0';
|
||||
char *get_directory(const char *param);
|
||||
char *get_extension(const char *name);
|
||||
void move(const char *name);
|
||||
|
||||
pclose(xdg_cmd);
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* Get Downloads directory Location */
|
||||
downloads_dir = get_directory("DOWNLOAD");
|
||||
|
||||
/* Get Documents directory Location */
|
||||
documents_dir = get_directory("DOCUMENTS");
|
||||
|
||||
/* Initialize inotify instance */
|
||||
inotify_instance = inotify_init();
|
||||
|
@ -57,43 +39,94 @@ int main()
|
|||
perror("Failed initializing inotify library!");
|
||||
|
||||
/* Add wanted Downloads to watchlist */
|
||||
watch_list = inotify_add_watch(inotify_instance, downloads_dir,
|
||||
IN_CREATE | IN_DELETE);
|
||||
watch_list =
|
||||
inotify_add_watch(inotify_instance, downloads_dir, IN_CREATE);
|
||||
if (!watch_list)
|
||||
perror("Failed to add downloads folder to watchlist!");
|
||||
|
||||
/* Get events*/
|
||||
int length = read(inotify_instance, event_buffer, EVENT_BUF_LEN);
|
||||
if (!length)
|
||||
perror("Failed to read inotify buffer!");
|
||||
while (1) {
|
||||
/* Get events*/
|
||||
int length =
|
||||
read(inotify_instance, event_buffer, EVENT_BUF_LEN);
|
||||
if (!length)
|
||||
perror("Failed to read inotify buffer!");
|
||||
|
||||
/* Process events */
|
||||
int i = 0;
|
||||
while (i < length) {
|
||||
struct inotify_event *event =
|
||||
(struct inotify_event *)&event_buffer[i];
|
||||
if (event->len) {
|
||||
if (event->mask & IN_CREATE) {
|
||||
if (event->mask & IN_ISDIR) {
|
||||
printf("New directory %s created.\n",
|
||||
event->name);
|
||||
} else {
|
||||
printf("New file %s created.\n",
|
||||
event->name);
|
||||
}
|
||||
} else if (event->mask & IN_DELETE) {
|
||||
if (event->mask & IN_ISDIR) {
|
||||
printf("Directory %s deleted.\n",
|
||||
event->name);
|
||||
} else {
|
||||
printf("File %s deleted.\n",
|
||||
event->name);
|
||||
}
|
||||
/* Process events */
|
||||
int i = 0;
|
||||
while (i < length) {
|
||||
struct inotify_event *event =
|
||||
(struct inotify_event *)&event_buffer[i];
|
||||
if (event->len) {
|
||||
if (event->mask & IN_CREATE)
|
||||
move(event->name);
|
||||
}
|
||||
i += EVENT_SIZE + event->len;
|
||||
}
|
||||
i += EVENT_SIZE + event->len;
|
||||
}
|
||||
inotify_rm_watch(inotify_instance, watch_list);
|
||||
|
||||
close(inotify_instance);
|
||||
}
|
||||
|
||||
char *get_extension(const char *name)
|
||||
{
|
||||
char *ext = (char *)malloc(sizeof(char));
|
||||
int ext_len = 0;
|
||||
for (int i = 0; i < strlen(name); i++) {
|
||||
if (name[i] == '.') {
|
||||
for (int j = i + 1; j < strlen(name); j++) {
|
||||
ext = (char *)realloc(ext, ++ext_len);
|
||||
ext[ext_len - 1] = name[j];
|
||||
}
|
||||
ext = (char *)realloc(ext, ++ext_len);
|
||||
ext[ext_len - 1] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ext;
|
||||
}
|
||||
|
||||
void move(const char *name)
|
||||
{
|
||||
char *ext = get_extension(name);
|
||||
printf("Moving: %s Extension: %s\n", name, ext);
|
||||
}
|
||||
|
||||
char *get_directory(const char *name)
|
||||
{
|
||||
char *result = (char *)malloc(4096 * sizeof(char));
|
||||
FILE *xdg_cmd = NULL;
|
||||
char *cmd = (char *)malloc(20 * sizeof(char));
|
||||
sprintf(cmd, "xdg-user-dir %s", name);
|
||||
xdg_cmd = popen(cmd, "r");
|
||||
if (!xdg_cmd) {
|
||||
char *home_dir = getenv("HOME");
|
||||
if (!home_dir) {
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (!pw)
|
||||
perror("Failed to get home directory!");
|
||||
home_dir = pw->pw_dir;
|
||||
if (!home_dir)
|
||||
perror("Failed miserably to get a directory path!");
|
||||
}
|
||||
strcat(result, home_dir);
|
||||
strcat(result, "/");
|
||||
if (strcmp(name, "DOWNLOAD"))
|
||||
strcat(result, downloads_dir_default);
|
||||
else if (strcmp(name, "DOCUMENTS"))
|
||||
strcat(result, documents_dir_default);
|
||||
} else {
|
||||
/* Read path from output of xdg */
|
||||
int c, n = 0;
|
||||
while ((c = fgetc(xdg_cmd)) != EOF) {
|
||||
if (c == '\n')
|
||||
result[n++] = '\0';
|
||||
else
|
||||
result[n++] = (char)c;
|
||||
}
|
||||
result[n] = '\0';
|
||||
|
||||
pclose(xdg_cmd);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue