Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Created October 10, 2022 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dulimarta/5917733d6d253461a14d315ed93f10b5 to your computer and use it in GitHub Desktop.
Save dulimarta/5917733d6d253461a14d315ed93f10b5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>
int number = 0;
bool running = true;
void do_nothing(int s) {
if (number < 0) {
running = false;
}
printf ("Inside signal handler %d %d\n", running, number);
}
void skip_input() {
printf ("Skipping invalid input...\n");
int c;
do {
c = getchar();
} while (c != EOF && c != '\n');
// c is EOF or newline
}
int main() {
struct sigaction z;
sigemptyset(&z.sa_mask);
z.sa_handler = do_nothing;
sigaction(SIGINT, &z, NULL);
number = 47;
do {
printf ("Enter a number: ");
errno = 0;
int how_many_items = scanf("%d", &number);
if (errno == EINTR) {
perror("What?");
printf ("I did not get your input. Last value was %d\n", number);
} else {
if (how_many_items == 0) {
skip_input();
}
printf ("You entered %d\n", number);
}
} while (number != 0 && running);
printf ("\nEnd of program\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment