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 //===----------------------------------------------------------------------===// 9 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 15 16 #include "random" 17 #include "system_error" 18 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> 27 28 _LIBCPP_BEGIN_NAMESPACE_STD 29 30 #if defined(_WIN32) 31 random_device::random_device(const string&) 32 { 33 } 34 35 random_device::~random_device() 36 { 37 } 38 39 unsigned 40 random_device::operator()() 41 { 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; 47 } 48 #else 49 random_device::random_device(const string& __token) 50 : __f_(open(__token.c_str(), O_RDONLY)) 51 { 52 if (__f_ <= 0) 53 __throw_system_error(errno, ("random_device failed to open " + __token).c_str()); 54 } 55 56 random_device::~random_device() 57 { 58 close(__f_); 59 } 60 61 unsigned 62 random_device::operator()() 63 { 64 unsigned r; 65 read(__f_, &r, sizeof(r)); 66 return r; 67 } 68 #endif // defined(_WIN32) 69 70 double 71 random_device::entropy() const _NOEXCEPT 72 { 73 return 0; 74 } 75 76 _LIBCPP_END_NAMESPACE_STD 77