Skip to content

Instantly share code, notes, and snippets.

@clausecker
Created June 9, 2020 20:45
Show Gist options
  • Save clausecker/54bf8533d23590cd995b7c4592f970d1 to your computer and use it in GitHub Desktop.
Save clausecker/54bf8533d23590cd995b7c4592f970d1 to your computer and use it in GitHub Desktop.
#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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment