From 36673ef3601fe8afd9ba93810fab77250fd6c97e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20Bartoszkiewicz?= Date: Thu, 12 Feb 2015 18:38:34 +0100 Subject: [PATCH] Czytamy z /dev/urandom przez read, nie fread --- choinka.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) 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); -- 2.45.2