comparison watcher.c @ 166:66fef65c34e7

watcher: exit if errors occur.
author John "Elwin" Edwards
date Wed, 07 Jan 2015 13:22:05 -0500
parents 245a2959f504
children
comparison
equal deleted inserted replaced
165:59e62710cbb5 166:66fef65c34e7
41 fflush(stdout); 41 fflush(stdout);
42 return 0; 42 return 0;
43 } 43 }
44 44
45 int main(int argc, char *argv[]) { 45 int main(int argc, char *argv[]) {
46 int ifd, rsize, off, done, nwatchers, i; 46 int ifd, rsize, off, done, nwatchers, i, result;
47 char typecode; 47 char typecode;
48 struct inotify_event *iev; 48 struct inotify_event *iev;
49 fd_set rfds; 49 fd_set rfds;
50 struct watchdir *watchers; 50 struct watchdir *watchers;
51 51
67 67
68 while (!done) { 68 while (!done) {
69 FD_ZERO(&rfds); 69 FD_ZERO(&rfds);
70 FD_SET(0, &rfds); 70 FD_SET(0, &rfds);
71 FD_SET(ifd, &rfds); 71 FD_SET(ifd, &rfds);
72 select(ifd + 1, &rfds, NULL, NULL, NULL); 72 result = select(ifd + 1, &rfds, NULL, NULL, NULL);
73 if (result < 0) {
74 /* Something went wrong. */
75 break;
76 }
73 if (FD_ISSET(ifd, &rfds)) { 77 if (FD_ISSET(ifd, &rfds)) {
74 off = 0; 78 off = 0;
75 rsize = read(ifd, ibuf, sizeof(struct inotify_event) + NAME_MAX + 1); 79 rsize = read(ifd, ibuf, sizeof(struct inotify_event) + NAME_MAX + 1);
76 while (off < rsize) { 80 while (off < rsize) {
77 iev = (struct inotify_event *) (ibuf + off); 81 iev = (struct inotify_event *) (ibuf + off);
88 off += sizeof(struct inotify_event) + iev->len; 92 off += sizeof(struct inotify_event) + iev->len;
89 } 93 }
90 fflush(stdout); 94 fflush(stdout);
91 } 95 }
92 if (FD_ISSET(0, &rfds)) { 96 if (FD_ISSET(0, &rfds)) {
93 read(0, &typecode, 1); 97 result = read(0, &typecode, 1);
98 if (result <= 0) {
99 /* EOF on stdin: controlling process probably died. */
100 done = 1;
101 }
94 if (typecode == '\n') 102 if (typecode == '\n')
95 done = 1; 103 done = 1;
96 } 104 }
97 105
98 } 106 }