]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
Bye bye FILE*
authormmj <none@none>
Thu, 3 Jun 2004 16:57:14 +0000 (02:57 +1000)
committermmj <none@none>
Thu, 3 Jun 2004 16:57:14 +0000 (02:57 +1000)
src/random-int.c

index 1fe6873fd7cf9d618d362640b4106187737127bd..b5405d8826b2d4856e540d69a1db956e196e9513 100644 (file)
@@ -6,30 +6,47 @@
  * Public License as described at http://www.gnu.org/licenses/gpl.txt
  */
 
-#include <stdio.h>
+#include <fcntl.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <time.h>
 
 int random_int()
 {
        unsigned int seed;
-       FILE *devrandom;
+       int devrandom;
+       unsigned char ch;
 
        seed = (unsigned int)time(NULL);
 
-       devrandom = fopen("/dev/urandom", "r");
-       if(!devrandom)
-               devrandom = fopen("/dev/random", "r");
+       devrandom = open("/dev/urandom", O_RDONLY);
+       if(devrandom < 0)
+               devrandom = open("/dev/random", O_RDONLY);
 
-       if (devrandom) {
-               seed ^= ((unsigned char)fgetc(devrandom));
-               seed ^= ((unsigned char)fgetc(devrandom)) << 8;
-               seed ^= ((unsigned char)fgetc(devrandom)) << 16;
-               seed ^= ((unsigned char)fgetc(devrandom)) << 24;
-               fclose(devrandom);
+       if (devrandom >= 0) {
+               read(devrandom, &ch, 1);
+               seed ^= ch;
+               read(devrandom, &ch, 1);
+               seed ^= ch << 8;
+               read(devrandom, &ch, 1);
+               seed ^= ch << 16;
+               read(devrandom, &ch, 1);
+               seed ^= ch << 24;
+               close(devrandom);
        }
 
        srand(seed);
 
        return rand();
 }
+#if 0
+int main(int argc, char **argv)
+{
+       int i;
+       
+       for(i = 0; i < 25; i++)
+               printf("%i\n", random_int());
+       
+       return 0;
+}
+#endif