tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / libc++ / dist / libcxx / src / random.cpp
blobbd24f2e50c1ab923f2b8a06bb065b833554ab68e
1 //===-------------------------- random.cpp --------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #if defined(_WIN32)
11 // Must be defined before including stdlib.h to enable rand_s().
12 #define _CRT_RAND_S
13 #include <stdio.h>
14 #endif
16 #include "random"
17 #include "system_error"
19 #ifdef __sun__
20 #define rename solaris_headers_are_broken
21 #endif
22 #if !defined(_WIN32)
23 #include <fcntl.h>
24 #include <unistd.h>
25 #endif // defined(_WIN32)
26 #include <errno.h>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 #if defined(_WIN32)
31 random_device::random_device(const string&)
35 random_device::~random_device()
39 unsigned
40 random_device::operator()()
42 unsigned r;
43 errno_t err = rand_s(&r);
44 if (err)
45 __throw_system_error(err, "random_device rand_s failed.");
46 return r;
48 #else
49 random_device::random_device(const string& __token)
50 : __f_(open(__token.c_str(), O_RDONLY))
52 if (__f_ <= 0)
53 __throw_system_error(errno, ("random_device failed to open " + __token).c_str());
56 random_device::~random_device()
58 close(__f_);
61 unsigned
62 random_device::operator()()
64 unsigned r;
65 read(__f_, &r, sizeof(r));
66 return r;
68 #endif // defined(_WIN32)
70 double
71 random_device::entropy() const _NOEXCEPT
73 return 0;
76 _LIBCPP_END_NAMESPACE_STD