blob: 97a40c509dc9a6395e31038773dd137698ba8e03 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===-------------------------- random.cpp --------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "random"
11#include "system_error"
12
David Chisnall997e4542012-02-29 13:05:08 +000013#ifdef __sun__
14#define rename solaris_headers_are_broken
15#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000016#include <fcntl.h>
17#include <unistd.h>
18#include <errno.h>
19
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000020_LIBCPP_BEGIN_NAMESPACE_STD
21
22random_device::random_device(const string& __token)
23 : __f_(open(__token.c_str(), O_RDONLY))
24{
25 if (__f_ <= 0)
26 __throw_system_error(errno, ("random_device failed to open " + __token).c_str());
27}
28
29random_device::~random_device()
30{
31 close(__f_);
32}
33
34unsigned
35random_device::operator()()
36{
37 unsigned r;
38 read(__f_, &r, sizeof(r));
39 return r;
40}
41
42double
Howard Hinnantc83960a2012-07-20 21:44:27 +000043random_device::entropy() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044{
45 return 0;
46}
47
48_LIBCPP_END_NAMESPACE_STD