Robert Lowry avatar Robert Lowry committed e22221a

seperated inotify and kqueue code into different functions in cw.c

Comments (0)

Files changed (1)

 #include <sys/select.h>
 
 #ifdef __linux__
+#define HAVE_INOTIFY
 #include <sys/inotify.h>
 #endif
 
+#ifdef BSD
+#define HAVE_KQUEUE
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#endif
+
 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
 
 FILE *in, *out;
 	fflush(stdout);
 }
 
+#ifdef HAVE_INOTIFY
+void run_inotify(char *infile, char *outfile)
+{
+	int evq, in_wd;
+	fd_set rd;
+
+	if ((evq = inotify_init()) < 0)
+		err(1, "failed inotify_init()");
+	if ((in_wd = inotify_add_watch(evq, argv[1], IN_MODIFY)) < 0)
+		err(1, "failed inotify_add_watch()");
+
+	for(;;) {
+		FD_ZERO(&rd);
+		FD_SET(evq, &rd);
+		FD_SET(fileno(stdin), &rd);
+		if (select(evq + 1, &rd, 0, 0, NULL) < 0) {
+		    if (errno == EINTR)
+			continue;
+		    else
+			warn("error on select");
+		}
+		if (FD_ISSET(evq, &rd)) {
+			if (read(evq, buf, sizeof(buf)) < 0 && errno != EINTR)
+				err(1, "failed read() on evq");
+
+			while (fgets(buf, sizeof(buf), in))
+				fputs(buf, stdout);
+			fflush(stdout);
+		}
+		if (FD_ISSET(fileno(stdin), &rd)) {
+			if (fgets(buf, sizeof(buf), stdin))
+				fputs(buf, out);
+			fflush(out);
+		}
+	}
+}
+#endif
+
+#ifdef HAVE_KQUEUE
+void run_kqueue(char *infile, char *outfile) {
+}
+#endif
+
 int main(int argc, char **argv)
 {
-#ifdef __linux__
-	int evq, in_wd;
-#endif
-	fd_set rd;
-
 	if (argc != 3) {
 		printf("Usage: cw infile outfile\n");
 		printf("cw-"VERSION" © Evan Gates\n");
 	if ((in = fopen(argv[1], "r")) == NULL)
 		err(1, "failed to fopen %s", argv[1]);
 
-#ifdef __linux__
-	if ((evq = inotify_init()) < 0)
-		err(1, "failed inotify_init()");
-	if ((in_wd = inotify_add_watch(evq, argv[1], IN_MODIFY)) < 0)
-		err(1, "failed inotify_add_watch()");
-#endif
-
 	while (fgets(buf, sizeof(buf), in))
 		fputs(buf, stdout);
 	fflush(stdout);
 	if ((out = fopen(argv[2], "w")) == NULL)
 		err(1, "failed to fopen %s", argv[2]);
 
-	for (;;) {
-		FD_ZERO(&rd);
-		FD_SET(evq, &rd);
-		FD_SET(fileno(stdin), &rd);
+#ifdef HAVE_INOTIFY
+	run_inotify(argv[1],argv[2]);
+#endif
+#ifdef HAVE_KQUEUE
+	run_kqueue(argv[1],argv[2]);
+#endif
 
-#ifdef __linux__
-		if (select(evq + 1, &rd, 0, 0, NULL) < 0) {
-#else
-		if (select(fileno(stdin) + 1, &rd, 0, 0, NULL) < 0) {
-#endif
-		    if (errno == EINTR)
-			continue;
-		    else
-			warn("error on select");
-		}
-
-#ifdef __linux__
-		if (FD_ISSET(evq, &rd)) {
-			if (read(evq, buf, sizeof(buf)) < 0 && errno != EINTR)
-				err(1, "failed read() on evq");
-
-			while (fgets(buf, sizeof(buf), in))
-				fputs(buf, stdout);
-			fflush(stdout);
-		}
-#endif
-		if (FD_ISSET(fileno(stdin), &rd)) {
-			if (fgets(buf, sizeof(buf), stdin))
-				fputs(buf, out);
-			fflush(out);
-		}
-	}
 	return 0;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.