]> git.bzium.org - embe/choinka.git/blobdiff - choinka.c
meson.build: instalujemy program
[embe/choinka.git] / choinka.c
index c02dc72d5069fd15b88f3c13820c736796ea0c08..4865cff3f5468322c92f4f1eb9e9e71b2cdca553 100644 (file)
--- a/choinka.c
+++ b/choinka.c
@@ -1,10 +1,14 @@
+#include <fcntl.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <term.h>
 #include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <term.h>
 #include <time.h>
+#include <unistd.h>
 
 #define A_POGRUBIONY (1 << 0)
 
 
 #define A_POGRUBIONY (1 << 0)
 
+#define KOLOR_BRAK -1
 #define KOLOR_CZARNY 0
 #define KOLOR_CZERWONY 1
 #define KOLOR_ZIELONY 2
 #define KOLOR_CZARNY 0
 #define KOLOR_CZERWONY 1
 #define KOLOR_ZIELONY 2
@@ -29,6 +33,7 @@ static atrybut const bombki[] = {
 };
 static int const ile_b = sizeof bombki / sizeof *bombki;
 
 };
 static int const ile_b = sizeof bombki / sizeof *bombki;
 
+static atrybut const spacja = {0, KOLOR_BRAK};
 static atrybut const choinka = {0, KOLOR_ZIELONY};
 static atrybut const podstawa = {0, KOLOR_BRAZOWY};
 static atrybut const podpis = {A_POGRUBIONY, KOLOR_CZERWONY};
 static atrybut const choinka = {0, KOLOR_ZIELONY};
 static atrybut const podstawa = {0, KOLOR_BRAZOWY};
 static atrybut const podpis = {A_POGRUBIONY, KOLOR_CZERWONY};
@@ -36,16 +41,16 @@ static atrybut const gwiazdka = {A_POGRUBIONY, KOLOR_BRAZOWY};
 
 static void inicjuj_rng()
 {
 
 static void inicjuj_rng()
 {
-  unsigned ziarno = 0;
-  FILE* f = fopen("/dev/urandom", "rb");
-  if (f != NULL) {
-    unsigned nowe_ziarno;
-    if (fread(&nowe_ziarno, sizeof nowe_ziarno, 1, f) == 1) {
-      ziarno = nowe_ziarno;
+  bool ok = false;
+  unsigned ziarno;
+  int f = open("/dev/urandom", O_RDONLY);
+  if (f != -1) {
+    if (read(f, &ziarno, sizeof ziarno) == sizeof ziarno) {
+      ok = true;
     }
     }
-    fclose(f);
+    close(f);
   }
   }
-  if (ziarno == 0) {
+  if (!ok) {
     ziarno = time(NULL);
   }
   srand(ziarno);
     ziarno = time(NULL);
   }
   srand(ziarno);
@@ -71,11 +76,14 @@ static void ustaw(atrybut const* attr)
   if ((attr->attr & A_POGRUBIONY) != 0) {
     putp(enter_bold_mode);
   }
   if ((attr->attr & A_POGRUBIONY) != 0) {
     putp(enter_bold_mode);
   }
-  putp(tparm(set_a_foreground, attr->kolor));
+  if (attr->kolor != KOLOR_BRAK) {
+    putp(tparm(set_a_foreground, attr->kolor));
+  }
 }
 
 int main()
 {
 }
 
 int main()
 {
+  setvbuf(stdout, NULL, _IOFBF, BUFSIZ);
   inicjuj_rng();
   setupterm(NULL, 1, NULL);
   atrybut const* kolor = &choinka;
   inicjuj_rng();
   setupterm(NULL, 1, NULL);
   atrybut const* kolor = &choinka;
@@ -86,6 +94,7 @@ int main()
     if (c >= 'a' && c <= 'z') akt = &podpis;
     if (c == '.') akt = &bombki[rand() % ile_b];
     if (c == '*') akt = &gwiazdka;
     if (c >= 'a' && c <= 'z') akt = &podpis;
     if (c == '.') akt = &bombki[rand() % ile_b];
     if (c == '*') akt = &gwiazdka;
+    if (c == ' ' || c == '\n') akt = &spacja;
     if (c == ']') kolor = &choinka;
     ustaw(akt);
     putchar(c);
     if (c == ']') kolor = &choinka;
     ustaw(akt);
     putchar(c);