random.cpp revision 227825
1227825Stheraven//===-------------------------- random.cpp --------------------------------===// 2227825Stheraven// 3227825Stheraven// The LLVM Compiler Infrastructure 4227825Stheraven// 5227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open 6227825Stheraven// Source Licenses. See LICENSE.TXT for details. 7227825Stheraven// 8227825Stheraven//===----------------------------------------------------------------------===// 9227825Stheraven 10227825Stheraven#include "random" 11227825Stheraven#include "system_error" 12227825Stheraven 13227825Stheraven#include <fcntl.h> 14227825Stheraven#include <unistd.h> 15227825Stheraven#include <errno.h> 16227825Stheraven 17227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD 18227825Stheraven 19227825Stheravenrandom_device::random_device(const string& __token) 20227825Stheraven : __f_(open(__token.c_str(), O_RDONLY)) 21227825Stheraven{ 22227825Stheraven if (__f_ <= 0) 23227825Stheraven __throw_system_error(errno, ("random_device failed to open " + __token).c_str()); 24227825Stheraven} 25227825Stheraven 26227825Stheravenrandom_device::~random_device() 27227825Stheraven{ 28227825Stheraven close(__f_); 29227825Stheraven} 30227825Stheraven 31227825Stheravenunsigned 32227825Stheravenrandom_device::operator()() 33227825Stheraven{ 34227825Stheraven unsigned r; 35227825Stheraven read(__f_, &r, sizeof(r)); 36227825Stheraven return r; 37227825Stheraven} 38227825Stheraven 39227825Stheravendouble 40227825Stheravenrandom_device::entropy() const 41227825Stheraven{ 42227825Stheraven return 0; 43227825Stheraven} 44227825Stheraven 45227825Stheraven_LIBCPP_END_NAMESPACE_STD 46