Get downloads folder.
- Get downloads folder with XDG Base Director Spec. - Fallback to constant default specified.
This commit is contained in:
parent
d727b7f3fe
commit
f4a9705b93
32
src/main.c
32
src/main.c
|
@ -3,6 +3,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
|
|
||||||
|
@ -10,15 +12,41 @@
|
||||||
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))
|
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))
|
||||||
#define WATCHLIST_SIZE 4096
|
#define WATCHLIST_SIZE 4096
|
||||||
|
|
||||||
|
static const char downloads_dir_default[] = "Downloads";
|
||||||
|
static char downloads_dir[2048];
|
||||||
|
|
||||||
static int inotify_instance = -1;
|
static int inotify_instance = -1;
|
||||||
static char event_buffer[EVENT_BUF_LEN];
|
static char event_buffer[EVENT_BUF_LEN];
|
||||||
static int watch_list[WATCHLIST_SIZE];
|
static int watch_list[WATCHLIST_SIZE];
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
/* Get downloads directory. */
|
||||||
|
FILE *xdg_cmd = NULL;
|
||||||
|
xdg_cmd = popen("/bin/bash xdg-user-dir", "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 */
|
||||||
|
fgets(downloads_dir, 4096, xdg_cmd);
|
||||||
|
fclose(xdg_cmd);
|
||||||
|
}
|
||||||
|
|
||||||
/* Initialize inotify instance */
|
/* Initialize inotify instance */
|
||||||
inotify_instance = inotify_init();
|
inotify_instance = inotify_init();
|
||||||
if (inotify_instance < 0)
|
if (!inotify_instance)
|
||||||
perror("Failed initializing inotify library!");
|
perror("Failed initializing inotify library!");
|
||||||
|
|
||||||
/* Add wanted Downloads to watch lists (Recursive) */
|
/* Add wanted Downloads to watch lists (Recursive) */
|
||||||
|
@ -31,7 +59,7 @@ int main()
|
||||||
/* Get events*/
|
/* Get events*/
|
||||||
int length =
|
int length =
|
||||||
read(inotify_instance, event_buffer, EVENT_BUF_LEN);
|
read(inotify_instance, event_buffer, EVENT_BUF_LEN);
|
||||||
if (length < 0)
|
if (!length)
|
||||||
perror("Failed to read inotify buffer!");
|
perror("Failed to read inotify buffer!");
|
||||||
|
|
||||||
/* Process events */
|
/* Process events */
|
||||||
|
|
Loading…
Reference in New Issue