Commits

Robert Lowry committed cdab912

smoothed out inotify code. not intensively tested yet, but working.

Comments (0)

Files changed (1)

 #define HAVE_INOTIFY
 #include <sys/inotify.h>
 #include <sys/wait.h>
-#endif
-
-#ifdef BSD
+#else
 #define HAVE_KQUEUE
 #endif
 
-#ifndef HAVE_INOTIFY
-#ifndef HAVE_KQUEUE
-#error "Either inotify or kqueue is required for pcw to function"
-#endif
-#endif
-
 #include "config.h"
 
 // fairly arbitray values...feel free to change
 	exit(1);
 }
 
+watch_t *new_watch()
+{
+	watch_t *w = malloc(sizeof(watch_t));
+	memset(w,0x00,sizeof(watch_t));
+	return w;
+}
 
 void spawn_win(watch_t *w)
 {
 	char *cmd[] = CMD;
 	char *cmd_color[] = CMD_COLOR;
 
+	if(w->pid) return;
+
 	strcpy(path,w->path);
 	snprintf(channel,sizeof(channel),"%s> ",basename(path));
 	snprintf(out,sizeof(out),"%s/out",w->path);
 	snprintf(in,sizeof(in),"%s/in",w->path);
 
-	printf("spawning %s\n",w->path);
-
 	if ((w->pid = fork()) == 0) {
 		color ? execvp(cmd_color[0],cmd_color) : execvp(cmd[0],cmd);
 		err(1, "failed on execvp %s", color ? cmd_color[0] : cmd[0]);
 	watch_t *w;
 	int wd;
 
-	printf("adding %s\n",fpath);
-
 	if(!(typeflag & FTW_D))
 		return 0;
 
 	}
 
 	if (!watch) {
-		watch = malloc(sizeof(watch_t));
+		watch = new_watch();
 		watch->wd = wd;
 	}
 	for(w = watch; w; w = w->next) {
 			break;
 		}
 		if (w->next == NULL) {
-			w->next = malloc(sizeof(watch_t));
+			w->next = new_watch();
 			w->next->wd = wd;
 		}
 	}
 	pid_t pid;
 	watch_t *w;
 
-	while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
-		for (w = watch; w; w = watch->next) {
-			if (w->pid == pid) {
+	while ((pid = waitpid(-1, NULL, WNOHANG)) > 0)
+		for (w = watch; w; w = watch->next)
+			if (w->pid == pid)
 				w->pid = 0;
-			}
-		}
-	}
 }
 
 #ifdef HAVE_INOTIFY
 
 	for (i = 1; i < argc; i++) {
 		if (argv[i][0] != '-') {
-			if (!dir) {
+			if(!dir) {
 				dir = argv[i];
 				continue;
-			} else { usage(); }
+			} else usage();
 		}
 		if (argv[i][2] != '\0')
 			usage();