File: rand.c

package info (click to toggle)
nessus-libraries 2.2.8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,724 kB
  • ctags: 2,314
  • sloc: ansic: 21,397; sh: 10,536; makefile: 414; yacc: 234; lex: 203
file content (66 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* 
 * Copyright (C) 2002 Michel Arboi
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Random generator helper functions
 */ 

#define EXPORTING
#include <includes.h>

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


/*
 * If the libc does not provide [ls]rand48, we use [s]rand() instead.
 *
 * While rand() is weak in comparison of lrand48, this is not a big
 * issue, as we want moderately random values in our code (meaning
 * that we don't use any of these functions for crypto-related operations)
 *
 */

ExtFunc void
nessus_init_random()
{
  FILE	*fp;
  long	x = 0;


  if ((fp = fopen("/dev/urandom", "r")) != NULL)
    {
      (void) fread(&x, sizeof(x), 1, fp);
      fclose(fp);
    }
  x += time(NULL) + getpid() + getppid();
  srand48(x);
}

#ifndef HAVE_LRAND48

ExtFunc
long lrand48()
{
 return rand();
}

ExtFunc
void srand48(long seed)
{ 
 srand(seed);
}
#endif