summaryrefslogtreecommitdiffstats
path: root/src/io.c
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-02-15 13:08:54 +0200
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-02-15 13:08:54 +0200
commit9569e629c8e73dcdc3f93b5fc6156db827d6b7b1 (patch)
tree43453709b52f3a1d3f74059c22d85a4d89a54aca /src/io.c
parent3f0c21826b4a4e59c937dc8435edb5be7100b078 (diff)
downloadalgos-ld1-9569e629c8e73dcdc3f93b5fc6156db827d6b7b1.tar.gz
algos-ld1-9569e629c8e73dcdc3f93b5fc6156db827d6b7b1.tar.bz2
algos-ld1-9569e629c8e73dcdc3f93b5fc6156db827d6b7b1.zip
cease inefficient /dev/urandom fiddling.
Instead of reading /dev/urandom like a file (i.e. overhead / extra syscalls, etc.), just use getrandom(2) directly, lol. Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c22
1 files changed, 4 insertions, 18 deletions
diff --git a/src/io.c b/src/io.c
index 9dae2e1..682eea2 100644
--- a/src/io.c
+++ b/src/io.c
@@ -20,7 +20,6 @@
static int stream_open_out(struct stream * const in);
static int stream_open_out_lite(struct stream * const in);
static int stream_open_in(struct stream * const in);
-static int stream_open_chardev(struct stream * const in);
static int stream_flush(struct stream * const in);
static int stream_flush_array(struct stream * const in);
static int stream_flush_list(struct stream * const in);
@@ -29,7 +28,7 @@ int stream_open(struct stream * const in)
{
int ret = 0;
- try(!in || in->fd > 0 || !in->name, err, EINVAL, "invalid argunent");
+ try(!in || in->fd > 0 || (!in->name && in->type != stream_randread), err, EINVAL, "invalid argunent");
switch (in->type) {
case (stream_outlite):
@@ -43,8 +42,8 @@ int stream_open(struct stream * const in)
try(!in->name, err, EINVAL, "no filename given");
ret = stream_open_in(in);
break;
- case (stream_chardev):
- ret = stream_open_chardev(in);
+ case (stream_randread):
+ ; /* NOOP */
break;
default:
try(0, err, EINVAL, "cannot open stream: stream is invalid");
@@ -58,7 +57,7 @@ int stream_close(struct stream * const in)
{
int ret = 0;
- try(!in || in->fd < 0, early_err, EINVAL, "invalid argunent");
+ try(!in || (in->fd < 0 && in->type != stream_randread), early_err, EINVAL, "invalid argunent");
if (in->type != stream_out) {
goto out;
@@ -166,19 +165,6 @@ err:
return ret;
}
-static int stream_open_chardev(struct stream * const in)
-{
- struct stat st;
- int ret = 0;
-
- try(stat(in->name, &st), err, errno, "stat failed: %s", strerror(ret));
- in->fd = open(in->name, O_RDONLY);
- try(in->fd < 0, err, errno, "failed opening input file: %s", strerror(ret));
-
-err:
- return ret;
-}
-
int stream_flush(struct stream * const in)
{
int ret = 0;