Minor cleanup

This commit is contained in:
Jōshin 2019-01-02 16:22:19 -08:00
parent 1f0e431259
commit 7506fb3157
2 changed files with 16 additions and 18 deletions

View File

@ -1,10 +1,9 @@
project('libent', 'c', version: '0.1.2', license: 'MIT') project('libent', 'c', version: '0.1.3', license: 'MIT')
inc = include_directories('include') inc = include_directories('include')
subdir('include') subdir('include')
subdir('src') subdir('src')
ent_sample = executable('sample', ent_sample = executable('sample', ['sample.c'],
['sample.c'],
dependencies: [libent_dep]) dependencies: [libent_dep])

View File

@ -19,19 +19,8 @@
# define _GNU_SOURCE # define _GNU_SOURCE
# include <unistd.h> # include <unistd.h>
# include <sys/syscall.h> # include <sys/syscall.h>
# define ENT_DEFS (void)0
# define ENT_INIT() (void)0
# define ENT_READ(B, L) syscall(SYS_getrandom, B, L, 0)
# define ENT_FINI() (void)0
# elif defined(ENT_URANDOM) # elif defined(ENT_URANDOM)
# include <stdio.h> # include <stdio.h>
# define ENT_DEFS FILE *f
# define ENT_INIT() do { \
if (!(f = fopen("/dev/urandom", "re"))) \
return -1; \
} while (0)
# define ENT_READ(B, L) fread(B, 1, L, f)
# define ENT_FINI() (void) fclose(f)
# endif # endif
static int static int
@ -45,13 +34,23 @@
ent_getentropy(void* buf, size_t len) ent_getentropy(void* buf, size_t len)
{ {
int r; int r;
ENT_DEFS;
if (len > 256) if (len > 256)
return _ent_fail(); return _ent_fail();
ENT_INIT(); # if defined(ENT_GETRANDOM)
r = ENT_READ(buf, len); r = syscall(SYS_getrandom, buf, len, 0);
ENT_FINI(); if (r < 0)
return r;
# elif defined(ENT_URANDOM)
{
FILE *f;
if (!(f = fopen("/dev/urandom", "re")))
return -1;
r = fread(buf, 1, len, f);
(void) fclose(f);
}
# endif
if (r != len) if (r != len)
return _ent_fail(); return _ent_fail();
return 0; return 0;