Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===-------------------------- random.cpp --------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "random" |
| 11 | #include "system_error" |
| 12 | |
| 13 | #include <fcntl.h> |
| 14 | #include <unistd.h> |
| 15 | #include <errno.h> |
| 16 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 17 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 18 | |
| 19 | random_device::random_device(const string& __token) |
| 20 | : __f_(open(__token.c_str(), O_RDONLY)) |
| 21 | { |
| 22 | if (__f_ <= 0) |
| 23 | __throw_system_error(errno, ("random_device failed to open " + __token).c_str()); |
| 24 | } |
| 25 | |
| 26 | random_device::~random_device() |
| 27 | { |
| 28 | close(__f_); |
| 29 | } |
| 30 | |
| 31 | unsigned |
| 32 | random_device::operator()() |
| 33 | { |
| 34 | unsigned r; |
| 35 | read(__f_, &r, sizeof(r)); |
| 36 | return r; |
| 37 | } |
| 38 | |
| 39 | double |
| 40 | random_device::entropy() const |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | _LIBCPP_END_NAMESPACE_STD |