urandom.c
changeset 0 af501b0c1716
child 8 96d16dfe787a
new file mode 100644
--- /dev/null
+++ b/urandom.c
@@ -0,0 +1,41 @@
+/**
+ *     $Id: urandom.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
+ * $Source: /development/c/demos/urandom.c,v $
+ *
+ */
+
+#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;
+
+    qsort (numbers, elements, sizeof (unsigned char), (void *)&compare);
+
+    for (i = 0; i < elements; i++) {
+        printf ("Number: %d\n", numbers[i]);
+    }
+
+    close (fd);
+    return EXIT_SUCCESS;
+}