urandom.c
changeset 0 af501b0c1716
child 8 96d16dfe787a
equal deleted inserted replaced
-1:000000000000 0:af501b0c1716
       
     1 /**
       
     2  *     $Id: urandom.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
       
     3  * $Source: /development/c/demos/urandom.c,v $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <stdlib.h>
       
     9 #include <fcntl.h>
       
    10 #include <unistd.h>
       
    11 
       
    12 int compare (unsigned char *a, unsigned char *b)
       
    13 {
       
    14     if (*a > *b)
       
    15         return 1;
       
    16     return -1;
       
    17 }
       
    18 
       
    19 int main (int argc, char **argv)
       
    20 {
       
    21     int i;
       
    22     int elements;
       
    23     int fd;
       
    24     unsigned char numbers[256] = { 0 };
       
    25 
       
    26     fd = open ("/dev/urandom", O_RDONLY);
       
    27     if (fd == -1)
       
    28         return EXIT_FAILURE;
       
    29 
       
    30     elements = read (fd, numbers, 255);
       
    31     numbers[elements] = 0;
       
    32 
       
    33     qsort (numbers, elements, sizeof (unsigned char), (void *)&compare);
       
    34 
       
    35     for (i = 0; i < elements; i++) {
       
    36         printf ("Number: %d\n", numbers[i]);
       
    37     }
       
    38 
       
    39     close (fd);
       
    40     return EXIT_SUCCESS;
       
    41 }