summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-09-25 15:28:20 +0200
committerCarlos Maiolino <[email protected]>2025-09-25 15:28:20 +0200
commit7abf21eb77067191ededd96e0b0a3ddb31317eeb (patch)
tree35bc9a2733e35eb59526bfa8b81fba36ae6030cd
parent29e2e93f3f32efa47e95f95260431167e228fe7a (diff)
sigme: playground for signals
Program to play with signal manipulation Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--C/HF/sigme.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/C/HF/sigme.c b/C/HF/sigme.c
new file mode 100644
index 0000000..b1d5fb6
--- /dev/null
+++ b/C/HF/sigme.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+
+typedef void (*sig_handler) (int);
+void
+kill_me(int s) {
+ puts("\n\nGoodbye cruel world...\n");
+ exit(99);
+}
+
+int
+change_signal(int sig, sig_handler handler) {
+ struct sigaction action = {
+ .sa_handler = handler,
+ .sa_flags = 0,
+ };
+
+ sigemptyset(&action.sa_mask);
+ return sigaction(sig, &action, NULL);
+}
+
+int
+main(void) {
+ char name[30];
+
+ if (change_signal(SIGINT, kill_me) < 0) {
+ fprintf(stderr, "Couldn't change SIGINT action\n");
+ exit(1);
+ }
+
+ printf("Enter your name: ");
+ fgets(name, 30, stdin);
+ printf("Hello %s\n", name);
+ return 0;
+}