Skip to content

Instantly share code, notes, and snippets.

@clausecker
Created June 9, 2020 20:45
Show Gist options
  • Select an option

  • Save clausecker/54bf8533d23590cd995b7c4592f970d1 to your computer and use it in GitHub Desktop.

Select an option

Save clausecker/54bf8533d23590cd995b7c4592f970d1 to your computer and use it in GitHub Desktop.

Revisions

  1. clausecker created this gist Jun 9, 2020.
    30 changes: 30 additions & 0 deletions pipetest.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>

    extern int
    main(int argc, char *argv[])
    {
    ssize_t count;
    int flags;
    char buf;

    flags = fcntl(STDIN_FILENO, F_GETFL);
    if (flags == -1) {
    perror("fcntl(stdin, F_GETFL)");
    exit(EXIT_FAILURE);
    }

    flags = fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
    if (flags == -1) {
    perror("fcntl(stdin, F_SETFL)");
    exit(EXIT_FAILURE);
    }

    count = read(STDIN_FILENO, &buf, 1);
    if (count == -1)
    perror("read");
    else
    printf("read: %zd bytes\n", count);
    }