diff options
Diffstat (limited to 'src/inotifywatch.c')
-rw-r--r-- | src/inotifywatch.c | 77 |
1 files changed, 43 insertions, 34 deletions
diff --git a/src/inotifywatch.c b/src/inotifywatch.c index fe0d738..85f9c51 100644 --- a/src/inotifywatch.c +++ b/src/inotifywatch.c @@ -1,39 +1,32 @@ -// kate: replace-tabs off; space-indent off; - -#include "../config.h" -#include "common.h" - // FIXME this is cheating! Make this use only the public API. #include "../libinotifytools/src/inotifytools_p.h" +#include "../config.h" +#include "common.h" -#include <unistd.h> -#include <signal.h> -#include <stdbool.h> -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <sys/types.h> #include <sys/select.h> #include <sys/stat.h> #include <sys/time.h> -#include <fcntl.h> -#include <errno.h> +#include <sys/types.h> + #include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> #include <regex.h> +#include <signal.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> #include <inotifytools/inotifytools.h> #include <inotifytools/inotify.h> -#define _GNU_SOURCE -#include <getopt.h> extern char *optarg; extern int optind, opterr, optopt; -#define EXIT_OK 0 -#define EXIT_ERROR 1 -#define EXIT_TIMEOUT 2 - #define EXCLUDE_CHUNK 1024 #define nasprintf(...) niceassert( -1 != asprintf(__VA_ARGS__), "out of memory") @@ -75,15 +68,26 @@ void handle_signal( int signal __attribute__((unused)) ) { done = true; } +int print_info(); + +void print_info_now( int signal __attribute__((unused)) ) { + print_info(); + printf("\n"); +} + +int events; +int sort; +int zero; + int main(int argc, char ** argv) { - int events = 0; + events = 0; int timeout = 0; int verbose = 0; - int zero = 0; + zero = 0; int recursive = 0; char * fromfile = 0; - int sort = -1; + sort = -1; done = false; char * regex = NULL; char * iregex = NULL; @@ -93,7 +97,7 @@ int main(int argc, char ** argv) // Parse commandline options, aborting if something goes wrong if ( !parse_opts( &argc, &argv, &events, &timeout, &verbose, &zero, &sort, &recursive, &fromfile, ®ex, &iregex ) ) { - return EXIT_ERROR; + return EXIT_FAILURE; } if ( @@ -102,7 +106,7 @@ int main(int argc, char ** argv) REG_ICASE)) ) { fprintf(stderr, "Error in `exclude' regular expression.\n"); - return EXIT_ERROR; + return EXIT_FAILURE; } if ( !inotifytools_initialize() ) { @@ -113,7 +117,7 @@ int main(int argc, char ** argv) "something mysterious has gone wrong. Please e-mail " PACKAGE_BUGREPORT "\n" " and mention that you saw this message.\n"); - return EXIT_ERROR; + return EXIT_FAILURE; } // Attempt to watch file @@ -125,7 +129,7 @@ int main(int argc, char ** argv) if (0 == list.watch_files[0]) { fprintf(stderr, "No files specified to watch!\n"); - return EXIT_ERROR; + return EXIT_FAILURE; } unsigned int num_watches = 0; @@ -158,7 +162,7 @@ int main(int argc, char ** argv) fprintf(stderr, "Failed to watch %s: %s\n", this_file, strerror( inotifytools_error() ) ); } - return EXIT_ERROR; + return EXIT_FAILURE; } if ( recursive && verbose ) { fprintf( stderr, "OK, %s is now being watched.\n", this_file ); @@ -183,6 +187,7 @@ int main(int argc, char ** argv) signal( SIGALRM, handle_signal ); alarm( timeout ); } + signal( SIGUSR1, print_info_now ); inotifytools_initialize_stats(); // Now wait till we get event @@ -197,7 +202,7 @@ int main(int argc, char ** argv) } else if ( inotifytools_error() != EINTR ) { fprintf(stderr, "%s\n", strerror( inotifytools_error() ) ); - return EXIT_ERROR; + return EXIT_FAILURE; } else { continue; @@ -256,12 +261,16 @@ int main(int argc, char ** argv) } } while ( !done ); + return print_info(); +} - num_watches = inotifytools_get_num_watches(); +int print_info() { + unsigned int num_watches = 0; + num_watches = inotifytools_get_num_watches(); if ( !inotifytools_get_stat_total( 0 ) ) { fprintf( stderr, "No events occurred.\n" ); - return EXIT_OK; + return EXIT_SUCCESS; } // OK, go through the watches and print stats. @@ -364,7 +373,7 @@ int main(int argc, char ** argv) rbcloselist(rblist); rbdestroy(tree); - return EXIT_OK; + return EXIT_SUCCESS; } @@ -655,8 +664,8 @@ void print_help() printf("\t-d|--descending <event>\n" "\t\tSort descending by a particular event, or `total'.\n\n"); printf("Exit status:\n"); - printf("\t%d - Exited normally.\n", EXIT_OK); - printf("\t%d - Some error occurred.\n\n", EXIT_ERROR); + printf("\t%d - Exited normally.\n", EXIT_SUCCESS); + printf("\t%d - Some error occurred.\n\n", EXIT_FAILURE); printf("Events:\n"); print_event_descriptions(); } |