From: Martin Hundebøll Date: Mon, 25 Feb 2019 09:43:07 +0000 (+0100) Subject: gpiomon: add option to set line buffered output X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=63d5f02a5c74c70ef5131cb1c02722f09afd95cf;p=qemu-gpiodev%2Flibgpiod.git gpiomon: add option to set line buffered output Some applications call gpiomon in a sub process, in which case glibc defaults to block buffered output on stdout. This makes the output arrive to the calling process only when the (4kB) buffer is filled (or when gpiomon exists), making the information obsolete and pretty much useless. Support such scenarios by adding a switch to configure line buffered output on stdout. Similar switches are available in other applications (e.g. `rsync`s --output argument). Signed-off-by: Martin Hundebøll [Bartosz: - use setlinebuf() instead of setvbuf(), - tweak the help string, - move the setlinebuf() call directly into the optarg switch statement] Signed-off-by: Bartosz Golaszewski --- diff --git a/src/tools/gpiomon.c b/src/tools/gpiomon.c index 0d5fcbd..9a1843b 100644 --- a/src/tools/gpiomon.c +++ b/src/tools/gpiomon.c @@ -26,17 +26,18 @@ static const struct option longopts[] = { { "silent", no_argument, NULL, 's' }, { "rising-edge", no_argument, NULL, 'r' }, { "falling-edge", no_argument, NULL, 'f' }, + { "line-buffered", no_argument, NULL, 'b' }, { "format", required_argument, NULL, 'F' }, { GETOPT_NULL_LONGOPT }, }; -static const char *const shortopts = "+hvln:srfF:"; +static const char *const shortopts = "+hvln:srfbF:"; static void print_help(void) { printf("Usage: %s [OPTIONS] ...\n", get_progname()); - printf("Wait for events on GPIO lines\n"); + printf("Wait for events on GPIO lines and print them to standard output\n"); printf("\n"); printf("Options:\n"); printf(" -h, --help:\t\tdisplay this message and exit\n"); @@ -46,6 +47,7 @@ static void print_help(void) printf(" -s, --silent:\t\tdon't print event info\n"); printf(" -r, --rising-edge:\tonly process rising edge events\n"); printf(" -f, --falling-edge:\tonly process falling edge events\n"); + printf(" -b, --line-buffered:\tset standard output as line buffered\n"); printf(" -F, --format=FMT\tspecify custom output format\n"); printf("\n"); printf("Format specifiers:\n"); @@ -278,6 +280,9 @@ int main(int argc, char **argv) case 'f': watch_falling = true; break; + case 'b': + setlinebuf(stdout); + break; case 'F': ctx.fmt = optarg; break;