From: MichaƂ Bartoszkiewicz Date: Thu, 12 Feb 2015 17:38:34 +0000 (+0100) Subject: Czytamy z /dev/urandom przez read, nie fread X-Git-Url: https://git.bzium.org/embe/choinka.git/commitdiff_plain/36673ef3601fe8afd9ba93810fab77250fd6c97e?hp=ce2e37059c21803606557e44fe872174fed3772c Czytamy z /dev/urandom przez read, nie fread --- diff --git a/choinka.c b/choinka.c index b117972..7f64945 100644 --- a/choinka.c +++ b/choinka.c @@ -1,7 +1,10 @@ +#include +#include #include #include #include #include +#include #define A_POGRUBIONY (1 << 0) @@ -38,16 +41,16 @@ static atrybut const gwiazdka = {A_POGRUBIONY, KOLOR_BRAZOWY}; 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);