commit 1cbd9511f8f86a3201f0ac2383a226415de05584 Author: niansa Date: Sun Oct 11 12:11:56 2020 +0200 Initial commit diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d6bef74 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libcatch"] + path = libcatch + url = https://gitlab.com/niansa/libcatch.git diff --git a/a.out b/a.out new file mode 100755 index 0000000..2af980b Binary files /dev/null and b/a.out differ diff --git a/inotifyrun.c b/inotifyrun.c new file mode 100644 index 0000000..9839439 --- /dev/null +++ b/inotifyrun.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "libcatch/libcatch.h" + +struct fcmd_pair { + const char *fname; + const char *command; +}; +struct fcmdwatcher_pair { + struct fcmd_pair *pair; + int watcher; +}; + + +int main(int argc, char **argv) { + int inotifier; + libcatch_init(); + //int *p = NULL; *p = 1; //DEBUG + //*p = 1; //DEBUG + // Check arguments + if (argc == 1 || (argc - 1) % 2 != 0) { + // No arguments passed + printf("Usage: %s { }\n", argv[0]); + return EXIT_FAILURE; + } + // Initialise inotify + CATCH( + inotifier = inotify_init() + ); + // Get array of files and commands + libcatch_retry = 0; + struct fcmd_pair *assocs = (struct fcmd_pair *)(argv + 1); + struct fcmdwatcher_pair *assocs_watches = malloc((sizeof(struct fcmdwatcher_pair) * ((argc - 1) / 2)) + 1); + struct fcmdwatcher_pair *watcherpair_ptr; + CATCH(assocs_watches = malloc((sizeof(struct fcmdwatcher_pair) * ((argc - 1) / 2)) + 1)); + // Initlialise inotify further while checking if all files are actual files + libcatch_retry = 2; + for (struct fcmd_pair *thispair = assocs; *(char**)thispair; thispair++) { + // Check if file is an actual file + struct stat fname_stat; + stat(thispair->fname, &fname_stat); + if (S_ISDIR(fname_stat.st_mode)) { + fprintf(stderr, "'%s' is a directory and will be ignored.\n", thispair->fname); + continue; + } + // Initialise inotify further + watcherpair_ptr = &assocs_watches[thispair - assocs]; + watcherpair_ptr->pair = thispair; + CATCH( + watcherpair_ptr->watcher = inotify_add_watch(inotifier, thispair->fname, IN_OPEN) + ); + } + // Wait for events to occur + libcatch_retry = 1; + struct inotify_event thisevent; + while (1) { + // Read new event + CATCH( + read(inotifier, &thisevent, sizeof(thisevent) + NAME_MAX + 1) + ); + // Find associated event pair + for ( + watcherpair_ptr = assocs_watches; + watcherpair_ptr->watcher != thisevent.wd; + watcherpair_ptr++ + ); + // Run assiciated command + system(watcherpair_ptr->pair->command); + } +} diff --git a/libcatch b/libcatch new file mode 160000 index 0000000..4a7f680 --- /dev/null +++ b/libcatch @@ -0,0 +1 @@ +Subproject commit 4a7f680d19963b35c0688aa3dcf2ec4f06940ff9 diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..4dc7564 --- /dev/null +++ b/test.sh @@ -0,0 +1 @@ +#! /bin/true