/* * Read lines from a file stream (normally STDIN) * Returns the number of regular files to be watched or -1 if max_files is * exceeded */ int process_input(FILE *file, WatchFile *files[], int max_files) { char buf[PATH_MAX]; char *p, *path; int n_files = 0; struct stat sb; int i, matches; while (fgets(buf, sizeof(buf), file) != NULL) { buf[PATH_MAX-1] = '\0'; if ((p = strchr(buf, '\n')) != NULL) *p = '\0'; if (buf[0] == '\0') continue; if (xstat(buf, &sb) == -1) { xwarnx("unable to stat '%s'", buf); continue; } if (S_ISREG(sb.st_mode) != 0) { files[n_files] = malloc(sizeof(WatchFile)); strlcpy(files[n_files]->fn, buf, MEMBER_SIZE(WatchFile, fn)); files[n_files]->is_dir = 0; files[n_files]->file_count = 0; files[n_files]->mode = sb.st_mode; n_files++; } /* also watch the directory if it's not already in the list */ if (dirwatch_opt == 1) { if (S_ISDIR(sb.st_mode) != 0) path = &buf[0]; else if ((path = dirname(buf)) == 0) err(1, "dirname '%s' failed", buf); for (matches=0, i=0; i<n_files; i++) if (strcmp(files[i]->fn, path) == 0) matches++; if (matches == 0) { files[n_files] = malloc(sizeof(WatchFile)); strlcpy(files[n_files]->fn, path, MEMBER_SIZE(WatchFile, fn)); files[n_files]->is_dir = 1; files[n_files]->file_count = xlist_dir(path); files[n_files]->mode = sb.st_mode; n_files++; } } if (n_files+1 > max_files) return -1; } return n_files; }
/* * Determine if the user is specifying FIFO mode by supplying a pathname * prefixed with '+' and set the global mode flag accordingly */ int set_fifo(char *argv[]) { if (argv[0][0] == (int)'+') { strlcpy(fifo.fn, argv[0]+1, MEMBER_SIZE(WatchFile, fn)); if (xmkfifo(fifo.fn, S_IRUSR| S_IWUSR) == -1) err(1, "mkfifo '%s' failed", fifo.fn); if ((fifo.fd = xopen(fifo.fn, O_WRONLY, 0)) == -1) err(1, "open fifo '%s' failed", fifo.fn); return 1; } memset(&fifo, 0, sizeof(fifo)); return 0; }