From f8eb0b87d30545f76ab8f0aa7348863cf7dee3b5 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 16 Aug 2020 11:47:16 +0200 Subject: [PATCH] Ensure input regions are newline-separated fscanf accepts any whitespace character when \n is in the format string. References: https://github.com/emersion/slurp/issues/58#issuecomment-668484614 --- main.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 1becf9a..d95bcf7 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 2 +#define _POSIX_C_SOURCE 200809L #include #include @@ -168,7 +168,7 @@ static void handle_selection_start(struct slurp_seat *seat, struct slurp_selection *current_selection) { struct slurp_state *state = seat->state; current_selection->has_selection = true; - + if (state->single_point) { state->result.x = current_selection->x; state->result.y = current_selection->y; @@ -749,11 +749,18 @@ int main(int argc, char *argv[]) { wl_list_init(&state.boxes); if (!isatty(STDIN_FILENO) && !state.single_point) { - struct slurp_box in_box = {0}; - while (fscanf(stdin, "%d,%d %dx%d\n", &in_box.x, &in_box.y, - &in_box.width, &in_box.height) == 4) { + char *line = NULL; + size_t line_size = 0; + while (getline(&line, &line_size, stdin) >= 0) { + struct slurp_box in_box = {0}; + if (sscanf(line, "%d,%d %dx%d", &in_box.x, &in_box.y, + &in_box.width, &in_box.height) != 4) { + fprintf(stderr, "invalid box format: %s\n", line); + return EXIT_FAILURE; + } add_choice_box(&state, &in_box); } + free(line); } wl_list_init(&state.outputs); wl_list_init(&state.seats);