urandom.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:47:18 +0200
changeset 27 81a574d60c15
parent 20 5fec678f931b
child 29 7abf6146898e
permissions -rw-r--r--
typo in min2time format string committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/urandom.c
 * Copyright (C) 2008 Markus Broeker
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int compare (unsigned char *a, unsigned char *b)
{
    if (*a > *b)
        return 1;
    return -1;
}

int main (int argc, char **argv)
{
    int i;

    int elements;

    int fd;
    unsigned char numbers[256] = { 0 };

    fd = open ("/dev/urandom", O_RDONLY);
    if (fd == -1)
        return EXIT_FAILURE;

    elements = read (fd, numbers, 255);
    numbers[elements] = 0;
    close (fd);

    qsort (numbers, elements, sizeof (unsigned char), (void *)&compare);

    for (i = 0; i < elements; i++) {
        printf ("Number: %d\n", numbers[i]);
    }

    return EXIT_SUCCESS;
}