"Fossies" - the Fresh Open Source Software Archive

Member "mlmmj-1.3.0/contrib/receivestrip/mlmmj_src/random-int.c" (13 Mar 2012, 1933 Bytes) of package /linux/privat/mlmmj-1.3.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk>
    2  *
    3  * $Id$
    4  *
    5  * Permission is hereby granted, free of charge, to any person obtaining a copy
    6  * of this software and associated documentation files (the "Software"), to
    7  * deal in the Software without restriction, including without limitation the
    8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    9  * sell copies of the Software, and to permit persons to whom the Software is
   10  * furnished to do so, subject to the following conditions:
   11  *
   12  * The above copyright notice and this permission notice shall be included in
   13  * all copies or substantial portions of the Software.
   14  *
   15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
   21  * IN THE SOFTWARE.
   22  */
   23 
   24 #include <fcntl.h>
   25 #include <stdlib.h>
   26 #include <unistd.h>
   27 #include <time.h>
   28 
   29 #include "wrappers.h"
   30 
   31 int random_int()
   32 {
   33     static int init = 0;
   34     unsigned int seed;
   35     int devrandom;
   36     unsigned char ch;
   37 
   38     if (init) return rand();
   39 
   40     seed = (unsigned int)time(NULL);
   41 
   42     devrandom = open("/dev/urandom", O_RDONLY);
   43     if(devrandom < 0)
   44         devrandom = open("/dev/random", O_RDONLY);
   45 
   46     if (devrandom >= 0) {
   47         readn(devrandom, &ch, 1);
   48         seed ^= ch;
   49         readn(devrandom, &ch, 1);
   50         seed ^= ch << 8;
   51         readn(devrandom, &ch, 1);
   52         seed ^= ch << 16;
   53         readn(devrandom, &ch, 1);
   54         seed ^= ch << 24;
   55         close(devrandom);
   56     }
   57 
   58     srand(seed);
   59     init = 1;
   60 
   61     return rand();
   62 }
   63 #if 0
   64 int main(int argc, char **argv)
   65 {
   66     int i;
   67     
   68     for(i = 0; i < 25; i++)
   69         printf("%i\n", random_int());
   70     
   71     return 0;
   72 }
   73 #endif