diff --git a/VirtualFileSystem/Makefile b/VirtualFileSystem/Makefile index 28994083b2..d6114a4ba2 100644 --- a/VirtualFileSystem/Makefile +++ b/VirtualFileSystem/Makefile @@ -22,6 +22,7 @@ VFS_OBJS = \ ZeroDevice.o \ NullDevice.o \ FullDevice.o \ + RandomDevice.o \ test.o OBJS = $(AK_OBJS) $(VFS_OBJS) diff --git a/VirtualFileSystem/RandomDevice.cpp b/VirtualFileSystem/RandomDevice.cpp new file mode 100644 index 0000000000..a47dc246dc --- /dev/null +++ b/VirtualFileSystem/RandomDevice.cpp @@ -0,0 +1,47 @@ +#include "RandomDevice.h" +#include "Limits.h" +#include +#include +#include + +RandomDevice::RandomDevice() +{ +} + +RandomDevice::~RandomDevice() +{ +} + +// Simple rand() and srand() borrowed from the POSIX standard: + +static unsigned long next = 1; + +#define MY_RAND_MAX 32767 +static int myrand() +{ + next = next * 1103515245 + 12345; + return((unsigned)(next/((MY_RAND_MAX + 1) * 2)) % (MY_RAND_MAX + 1)); +} + +static void mysrand(unsigned seed) +{ + next = seed; +} + +Unix::ssize_t RandomDevice::read(byte* buffer, Unix::size_t bufferSize) +{ + const int range = 'z' - 'a'; + Unix::ssize_t nread = min(bufferSize, GoodBufferSize); + for (Unix::ssize_t i = 0; i < nread; ++i) { + double r = ((double)myrand() / (double)MY_RAND_MAX) * (double)range; + buffer[i] = 'a' + r; + } + return nread; +} + +Unix::ssize_t RandomDevice::write(const byte*, Unix::size_t bufferSize) +{ + // FIXME: Use input for entropy? I guess that could be a neat feature? + return min(GoodBufferSize, bufferSize); +} + diff --git a/VirtualFileSystem/RandomDevice.h b/VirtualFileSystem/RandomDevice.h new file mode 100644 index 0000000000..4a30c259fc --- /dev/null +++ b/VirtualFileSystem/RandomDevice.h @@ -0,0 +1,13 @@ +#pragma once + +#include "CharacterDevice.h" + +class RandomDevice final : public CharacterDevice { +public: + RandomDevice(); + virtual ~RandomDevice(); + + Unix::ssize_t read(byte* buffer, Unix::size_t bufferSize) override; + Unix::ssize_t write(const byte* buffer, Unix::size_t bufferSize) override; +}; + diff --git a/VirtualFileSystem/small.fs b/VirtualFileSystem/small.fs index 0eb8186ea1..f336606ddb 100644 Binary files a/VirtualFileSystem/small.fs and b/VirtualFileSystem/small.fs differ diff --git a/VirtualFileSystem/test.cpp b/VirtualFileSystem/test.cpp index 3977541f5d..7b07a6fc39 100644 --- a/VirtualFileSystem/test.cpp +++ b/VirtualFileSystem/test.cpp @@ -6,6 +6,7 @@ #include "ZeroDevice.h" #include "NullDevice.h" #include "FullDevice.h" +#include "RandomDevice.h" #include #include #include @@ -29,6 +30,9 @@ int main(int c, char** v) auto full = make(); vfs.registerCharacterDevice(1, 7, *full); + auto random = make(); + vfs.registerCharacterDevice(1, 8, *random); + if (!vfs.mountRoot(makeFileSystem(filename))) { printf("Failed to mount root :(\n"); return 1; @@ -187,7 +191,6 @@ int main(int c, char** v) byte buffer[512]; for (;;) { nread = handle->read(buffer, sizeof(buffer)); - printf("read() returned %d\n", nread); if (nread <= 0) break; fwrite(buffer, 1, nread, stdout);