-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
937 lines (626 loc) · 23.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/*
MIT License
Copyright (c) 2017 Dennis Addo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
*@file main.c
* A simple file watcher for the linux system.
* @author Dennis Addo
* @date 22/07/2017
**/
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
#include <fnmatch.h>
#include <libgen.h>
#include "array.h"
/* User defined color for output formatting*/
enum ascolors{
RED, GREEN, YELLOW,
BLUE, MAGNENTAP, CYAN,
LCYAN, DARKGREAY, LGRAY,
LBLUE, RESET, NO_COLOR
};
static const char const *colors[] = {"\x1b[31m", "\x1b[32m", "\x1b[33m",
"\x1b[34m", "\x1b[35m", "\x1b[95m",
"\x1b[96m", "\x1b[90m", "\x1b[100m",
"\x1b[94m", "\x1b[0m" , ""
};
typedef struct acolor{
enum ascolors colr; // color
enum ascolors end; //color terminator
}acolor;
static int do_files(const char *path, sds_array *str);
static void do_dir(const char *path, sds_array *fp);
static void events_handler(int fd, int **wd, sds_array *argv);
static void args_parser(int *argc, char **argv, char *pth[]);
static int add_to_watchlist(int fd, int **wdd, size_t len, sds_array *ffname);
static int get_lfiles(char *sp[], int l, sds_array *f);
static int fexisit(const char *pathname);
static void do_delete_s(const int tp, char *pathname,sds_array *sda, size_t idx,int **wd);
static void do_delete(const int tp, char *pathname,sds_array *sda, int **wd);
static void do_formatted_output(const int type, const char *pathname, const char *msg,const acolor color);
static void do_move_from(const int typ, const char *pathname,sds_array *sda, int **wd);
static void do_move_to(const char *pathname);
static void get_time(char *buf, size_t s);
static void do_resize(int **wd,size_t indx, size_t size);
static void do_usage(void);
static void flush(void);
/**********User defined constants here*/
static int rflag = 0;
static int gflag = 0;
static const char *sflag = "?";
const char const *msg_open = "--> was open!";
const char const *msg_acess = "--> was accessed!";
const char const *msg_close = "--> was closed!";
const char const *msg_created = "--> was created!";
const char const *msg_delete = "--> was deleted!";
const char const *msg_modify = "--> The content was modified";
/**
*@brief Processing the passed commandline arguments
*
*/
void args_parser(int *argc, char **argv, char *lpaths[]) {
int opt;
int indx = 0;
opterr = 0;
if (*argc < 2) {
do_usage();
flush();
exit(EXIT_FAILURE);
}
while ((opt = getopt(*argc, argv, "hrp:s:")) != -1) {
switch (opt) {
case 'h':
do_usage();
break;
case 'r':
rflag = 1;
break;
case 'p':
lpaths[indx++] = optarg;
break;
case 's':
sflag = optarg;
break;
case '?':
if (optopt == 'p') {
fprintf(stderr, "Option -%c requirs argument.\n", optopt);
} else if (optopt == 's') {
fprintf(stderr, "Option -%c requirs argument.\n", optopt);
} else if (isprint(optopt)) {
fprintf(stderr, "Unkown option `-%c `.\n", optopt);
} else {
fprintf(stderr, "Uknown option character `\\x%x.`\n", optopt);
}
flush();
exit(EXIT_FAILURE);
default:
fprintf(stderr, "%s\n", strerror(errno));
flush();
exit(EXIT_FAILURE);
}
}
if (optind < *argc) {
fprintf(stderr, "%s\n", "Non-Option argument");
do_usage();
flush();
exit(EXIT_FAILURE);
}
*argc = indx;
lpaths[indx] = NULL;
}
void events_handler(int fd, int **wd,sds_array *argv) {
char buf[4096]
__attribute__ ((aligned (__alignof__(struct inotify_event))));
const struct inotify_event *event; //readonly inotify event
ssize_t len;
char *ptr;
for (;;) {
/* Read some events.
* I will use ioctl() for the buffer size later. is more robust
* than the work around with the gcc attribute aligned 4096 bytes
*/
#if 0
if(ioctl(fd,FIONREAD,&buflen) == -1){
fprintf(stderr, "ioctl: %s\n", strerror(errno));
freeall(argv,argv->size);
exit(EXIT_FAILURE);
}
char buf[buflen];
if (buflen <= 0)
break;
#endif
len = read(fd, buf, sizeof buf);
if (len == -1 && errno != EAGAIN) {
fprintf(stderr, "read: %s\n", strerror(errno));
freeall(argv, argv->size);
(void) close(fd);
free(wd);
flush();
exit(EXIT_FAILURE);
}
if (len <= 0)
break;
/* Loop over all events in the buffer */
for (ptr = buf; ptr < buf + len;
ptr += sizeof(struct inotify_event) + event->len) {
event = (const struct inotify_event *) ptr;
const char *pth = "?";
const char *name = "";
const char *slash = "";
size_t i,indx;
for (i = 0; i < argv->size ; ++i) {
if ((*wd)[i] == event->wd) {
pth = argv->mem[i];
break;
}
}
indx = i;
if (event->len) {
name = event->name;
size_t l = strlen(pth);
slash = (pth[l - 1] == '/' ? "" : "/");
i = l + strlen(name) + strlen(slash) + 1;
} else {
i = strlen(pth) + 1;
}
if( i < 1)
continue;
char pthname[i];
int type = (event->mask & IN_ISDIR) ? 1: 0;
snprintf(pthname, i, "%s%s%s", pth, slash, name);
/*Additional functions to process this information will be updated soon.
*
*/
acolor color;
switch( event->mask & (IN_ALL_EVENTS | IN_UNMOUNT | IN_Q_OVERFLOW | IN_IGNORED) ){
case IN_OPEN :
color.colr = GREEN;
color.end = RESET;
do_formatted_output(type, pthname,msg_open,color);
break;
case IN_ACCESS: case IN_ATTRIB:
color.colr = DARKGREAY;
color.end = RESET;
do_formatted_output(type,pthname,msg_acess,color);
break;
case IN_CLOSE_WRITE: case IN_CLOSE_NOWRITE:
color.colr = NO_COLOR;
color.end = RESET;
do_formatted_output(type,pthname,msg_close,color);
break;
case IN_CREATE:
color.colr = GREEN;
color.end = RESET;
do_formatted_output(type,pthname,msg_created,color);
break;
case IN_DELETE:
do_delete(type,pthname,argv,wd);
break;
case IN_DELETE_SELF:
do_delete_s(type,pthname,argv,indx,wd);
break;
case IN_MODIFY:
color.colr = CYAN;
color.end = RESET;
do_formatted_output(type,pthname,msg_modify,color);
break;
case IN_MOVED_FROM:
case IN_MOVE_SELF:
do_move_from(type,pthname, argv,wd);
break;
case IN_MOVED_TO:
do_move_to(pthname);
break;
default:
break;
}
}
}
}
/**\brief
* formatting the output from the inotify events
* to a speficied file or to STDOUT
*
* Since plain text files doesn't support ascii color
* output coloring is remove here.
*
*/
inline void do_formatted_output(const int t, const char *pathname, const char *msg,const acolor color){
char tbuf[30];
get_time(tbuf,30);
const char *type = (t == 1) ? "[Directory]": "[File]";
FILE *fp = NULL;
if(sflag[0] != '?' && (fp = fopen(sflag,"a")) == NULL){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
if(color.colr == NO_COLOR){
fprintf((fp == NULL? stdout: fp),"%s %s %s %s\n",tbuf,type,pathname,msg);
}else {
fprintf((fp == NULL? stdout: fp),"%s %s %s %s %s %s\n",tbuf,type,
fp == NULL ? colors[color.colr] :"",pathname,msg,fp == NULL ? colors[color.end]:"");
}
if(fp != NULL){
if(fclose(fp) == EOF){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
}
}
inline void do_move_from(const int t, const char *pathname, sds_array *sda,int **wd){
if(pathname == NULL){
return;
}
const char *mg = "moved_from: ";
const char *type = (t == 1) ? "[Directory]": "[File]";
char tbuf[30];
get_time(tbuf,30);
size_t gin;
int sear = search_indx(&gin,sda,pathname);
if(sear == 0 && fexisit(pathname) == -1){
(*wd)[gin] = -1;
do_resize(wd,gin,sda->size);
delete_str(sda,gin);
}
FILE *fp = NULL;
if(sflag[0] != '?' && (fp = fopen(sflag,"a")) == NULL){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
fprintf((fp == NULL? stdout: fp),"%s %s %s %s%s%s ->",tbuf,type,mg,
fp == NULL ? colors[LGRAY]:"",pathname,fp == NULL ? colors[RESET]:"");
if(fp != NULL){
if(fclose(fp) == EOF){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
}
}
inline void do_move_to(const char *pathname){
FILE *fp = NULL;
if(sflag[0] != '?' && (fp = fopen(sflag,"a")) == NULL){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
if(pathname == NULL || strlen(pathname) < 1 ){
fprintf((fp == NULL? stdout: fp),"\n");
} else{
fprintf((fp == NULL? stdout: fp)," %s \n",pathname);
}
if(fp != NULL){
if(fclose(fp) == EOF){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
}
}
/**\brief
*
* simple current time formatter
* */
inline void get_time(char *tbuf, size_t s){
struct tm ftm;
time_t time1;
time1 = time(NULL);
localtime_r(&time1,&ftm);
strftime(tbuf,s,"%F %T",&ftm);
}
/**\brief
*
* handles the formatting output for delete and delete_self
*
* */
void do_delete_s(const int tp, char *pathname,sds_array *sda, size_t idx, int **wd) {
const char *msg = "--> was deleted!";
const char *type = (tp == 1) ? "[Directory]": "[File]";
int l = fexisit(pathname);
char tbuf[30];
get_time(tbuf,30);
FILE *fp = NULL;
if(sflag[0] != '?' && (fp = fopen(sflag,"a") ) == NULL){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
if( l == 0){
fprintf(stdout,"\n");
} else if (l == -1 && sda->mem[idx] != NULL){
if (strcmp(sda->mem[idx],pathname) == 0) { // is it an object we are monitoring
(*wd)[idx] = -1;
do_resize(wd,idx,sda->size);
delete_str(sda,idx);
fprintf((fp == NULL? stdout: fp),"%s %s %s %s %s %s\n",tbuf ,type,
fp == NULL ? colors[RED]:"",pathname,msg,fp == NULL ? colors[RESET]:" ");
gflag =1;
}else{
fprintf((fp == NULL? stdout: fp),"%s %s %s %s %s %s\n",tbuf ,type,
fp == NULL ? colors[RED]: "",pathname,msg,fp == NULL ? colors[RESET]: "");
}
}
if(fp != NULL){
if(fclose(fp) == EOF){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
}
}
void do_delete(const int tp, char *pathname,sds_array *sda, int **wd){
if(gflag == 1)
return;
size_t gin;
int sear = search_indx(&gin,sda,pathname);
int status = fexisit(pathname);
const char *type = (tp == 1) ? "[Directory]": "[File]";
char tbuf[30];
get_time(tbuf,30);
if(status == 0){
return;
}
FILE *fp = NULL;
if(sflag[0] != '?' && (fp = fopen(sflag,"a")) == NULL){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
if (status == -1) {
if (sear != -1) {
(*wd)[gin] = -1;
do_resize(wd, gin, sda->size);
delete_str(sda, gin);
fprintf((fp == NULL? stdout: fp), "%s %s %s %s %s %s\n", tbuf, type,
fp == NULL ? colors[RED] :"", pathname, msg_delete,fp == NULL ?colors[RESET]:"");
} else {
fprintf((fp == NULL? stdout: fp), "%s %s %s %s %s %s\n", tbuf, type,
fp == NULL ? colors[RED] :"", pathname, msg_delete,fp == NULL? colors[RESET]:"");
}
}
if(fp != NULL){
if(fclose(fp) == EOF){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
}
}
int do_files(const char *path, sds_array *fnames) {
if (path == NULL) {
fprintf(stderr, "%s\n", "Path can't be NULL, call help Usage");
return -1;
}
static int iflag = 0;
#if 0
fprintf(stdout, "addingg:---> %s\n", newpath);
#endif
add_str(fnames, path);
struct stat fatr;
int ret = lstat(path, &fatr);
if (ret == -1) {
fprintf(stderr, "lstat: %s %s\n", strerror(errno), path);
freeall(fnames, fnames->size);
flush();
exit(EXIT_FAILURE);
}
if (S_ISDIR(fatr.st_mode) && rflag == 1) {
do_dir(path, fnames);
} else if (S_ISDIR(fatr.st_mode) && iflag == 0) {
iflag = 1;
do_dir(path, fnames);
}
return 0;
}
void do_dir(const char *path, sds_array *fps) {
struct dirent *drent;
DIR *dir = opendir(path);
if (dir == NULL) {
fprintf(stderr, "opendir: %s\n", strerror(errno));
return;
}
while ((drent = readdir(dir)) != NULL) {
if (strcmp(drent->d_name, ".") == 0 || strcmp(drent->d_name, "..") == 0) {
continue;
}
const char *slash = "";
size_t len = strlen(path);
if (path[len - 1] != '/') slash = "/";
len += strlen(drent->d_name) + 2;
char p[len];
snprintf(p, len, "%s%s%s", path, slash, drent->d_name);
do_files(p, fps);
}
(void) closedir(dir);
}
int add_to_watchlist(int fdd, int **wdd, size_t len, sds_array *ffname) {
size_t i;
int status = 0;
for (i = 0; ffname->mem[i] != NULL && i < len; i++) {
(*wdd)[i] = inotify_add_watch(fdd, ffname->mem[i], IN_ALL_EVENTS | IN_DONT_FOLLOW);
if((*wdd)[i] == -1 && errno == EACCES){
fprintf(stderr, "Cannot watch '%s': %s \n", ffname->mem[i], strerror(errno));
continue;
}
if ((*wdd)[i] == -1 && errno != EACCES) {
fprintf(stderr, "Cannot watch '%s': %s \n", ffname->mem[i], strerror(errno));
status = -1;
break;
}
// printf("Watchinf index[%d] --> %s\n",(*wdd)[i],ffname->mem[i]);
}
// printf("\n");
return status;
}
int get_lfiles(char *sp[], int l, sds_array *fn) {
for (int i = 0; sp[i] != NULL && i < l; ++i) {
if (do_files(sp[i], fn) == -1) { //get all the files and dir and store them in fnames
return -1;
}
}
return 0;
}
inline void do_resize(int **wd,size_t indx, size_t size){
if(indx >= size)
return;
for (size_t i = indx; i < size ; ++i) {
(*wd)[i] = i != size -1 ? (*wd)[i+1]:0;
}
*wd = realloc(*wd,(size -1) * sizeof(int));
}
/**\brief check if a file a exisit or not
* This is needed to determine or confirm
* some unexpected actions from vim modify
* that triggers IN_DELETE_self
* @retval 0 on success and -1 on failure with errno
*
* */
inline int fexisit(const char *pathname) {
FILE *r;
r = fopen(pathname,"r");
if (r == NULL && errno != EINVAL) {
return -1;
}
if(r == NULL && errno != 0){
fprintf(stderr,"fexisit: %s\n",strerror(errno));
exit(1);
}
if(fclose(r) == EOF){
fprintf(stderr,"fopen:%s \n",strerror(errno));
exit(1);
}
return 0;
}
void flush(void) {
if (fflush(stdout) == EOF) {
fprintf(stderr, "fflush: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
inline void do_usage(void) {
fprintf(stdout, "%s\n%s\n%s\n%s\n%s\n%s\n",
"Usage: sfwatch <options> [Path to dir/file]",
"Options: (You can use any of the following options)",
" : -h Shows alll necessary information for sfwatch",
" : -p path to the file/directory to monitor",
" : -r recursive monitor for directory/subdirectories [no file]",
" : -s output log to a file"
);
}
int main(int argc, char *argv[]) {
char buf;
int fd, poll_num;
nfds_t nfds;
struct pollfd fds[2];
char *lpath[argc];
memset(lpath, 0, argc);
int plen = argc;
args_parser(&plen, argv, lpath);
/* Create the file descriptor */
fd = inotify_init1(IN_NONBLOCK);
if (fd == -1) {
fprintf(stderr, "inotify_init1: %s\n", strerror(errno));
flush();
return EXIT_FAILURE;
}
/* Allocate memory for watch descriptors files/dir full paths */
sds_array fnames = init_sds_array(6);
if (get_lfiles(lpath, plen, &fnames) == -1) {//get all the files and dir and store them in fnames
freeall(&fnames, fnames.size);
(void) close(fd);
fprintf(stderr, "%s\n%s\n","Sorry Listening for events stopped.!!",
"Cleaning up resources now!!!");
flush();
return EXIT_FAILURE;
}
//size_t dlen = get_size(); //number of files and directories
fprintf(stdout, "%sCurrently monitoring %zu objects%s\n",colors[YELLOW], fnames.size,colors[RESET]);
int *wd = malloc(fnames.size * sizeof(int));
if(wd == NULL){
fprintf(stderr,"malloc: %s\n",strerror(errno));
freeall(&fnames,fnames.size);
(void)close(fd);
flush();
return EXIT_FAILURE;
}
if (add_to_watchlist(fd, &wd, fnames.size, &fnames) == -1) {
freeall(&fnames, fnames.size);
(void) close(fd);
fprintf(stdout, "%s\n%s\n","Listening for events stopped.",
"Cleaning up resources now!!");
free(wd);
flush();
return EXIT_FAILURE;
}
fprintf(stdout, "%sRedirrecting all log to: %s %s\n",colors[YELLOW],(sflag[0] != '?' ? sflag:"STDOUT"),colors[RESET]);
fprintf(stdout, "%sPress ENTER key to terminate.%s\n",colors[RED],colors[RESET]);
/* Prepare for polling 2 file discriptors for now*/
nfds = 2;
/* Console/terminal input */
fds[0].fd = STDIN_FILENO;
fds[0].events = POLLIN;
/* Inotify fd input */
fds[1].fd = fd;
fds[1].events = POLLIN;
/* Wait for events --> fd and terminal input */
fprintf(stdout, "%s\n","Listening for events.");
while (1) {
poll_num = poll(fds, nfds, -1);
if (poll_num == -1) {
if (errno == EINTR)
continue;
fprintf(stderr, "poll: %s\n", strerror(errno));
freeall(&fnames, fnames.size);
(void) close(fd);
free(wd);
flush();
exit(EXIT_FAILURE);
}
if (poll_num > 0) {
if (fds[0].revents & POLLIN) {
/* Console input is available. Empty stdin until newline and quit */
ssize_t sbf;
while ((sbf = read(STDIN_FILENO, &buf, 1)) > 0 && buf != '\n')
continue;
if (sbf == -1) {
fprintf(stderr, "read: %s\n", strerror(errno));
freeall(&fnames, fnames.size);
(void) close(fd);
free(wd);
flush();
exit(EXIT_FAILURE);
}
break;
}
if (fds[1].revents & POLLIN) {
events_handler(fd, &wd, &fnames);
}
}
}
fprintf(stdout, "%s\n%s\n","Listening for events stopped.",
"Cleaning up resources now!!");
freeall(&fnames, fnames.size);
(void) close(fd);
free(wd);
flush();
exit(EXIT_SUCCESS);
}