4.ilyaz.18_test.decoder 4.ilyas.18_test.decoder

--- 4.ilyaz.18_test.decoder.fmt.c	2020-06-08 11:39:11.746115796 +0300
+++ 4.ilyas.18_test.decoder.fmt.c	2020-06-08 11:39:11.738115710 +0300
@@ -1,32 +1,36 @@
 #1 "<stdin>"
-#include <stdio.h>
-#include <sys/types.h>
+#include <assert.h>
 #include <signal.h>
-#include <unistd.h>
+#include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-int flag_end = 0;
-char cur_char = 0;
-int length = 0;
-void handler(int signal) {
-    cur_char = cur_char << 1;
-    if (signal == SIGUSR2)
-	cur_char += 1;
-    length += 1;
-    if (length == 8) {
-	if (cur_char == '\0')
-	    printf("\n");
-	length = 0;
-	printf("%c", cur_char);
-	fflush(stdout);
+#include <sys/wait.h>
+#include <unistd.h>
+int done = 1;
+char current = 0;
+int len = 0;
+void signal_handler(int sig) {
+    current = current << 1;
+    if (sig == SIGUSR2)
+	current += 1;
+    len += 1;
+    if (len == 8) {
+	if (current == 0) {
+	    done = 0;
+	    return;
+	}
+	len = 0;
+	printf("%c", current);
     }
 }
 int main(int argc, char *argv[]) {
-    int pid = getpid();
-    printf("My PID: %d\n", pid);
-    signal(SIGUSR1, handler);
-    signal(SIGUSR2, handler);
-    while (1)
+    int pid;
+    pid = getpid();
+    printf("PID: %d\n", pid);
+    signal(SIGUSR1, signal_handler);
+    signal(SIGUSR2, signal_handler);
+    while (done)
 	sleep(1);
     return 0;
 }