1
0
forked from aniani/vim

patch 9.1.0518: initialize the random buffer can be improved

Problem:  initialize the random buffer can be improved
Solution: refactor init_srand() function, move machine-specific parts to
          os_mswin and os_unix, implement a fallback for Windows 10 and
          later (LemonBoy)

closes: #15125

Signed-off-by: LemonBoy <thatlemon@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
LemonBoy
2024-07-04 13:20:49 +02:00
committed by Christian Brabandt
parent 898b3740c7
commit 9987fe8ca0
6 changed files with 91 additions and 47 deletions

View File

@@ -7722,6 +7722,34 @@ sig_sysmouse SIGDEFARG(sigarg)
}
#endif // FEAT_SYSMOUSE
/*
* Fill the buffer 'buf' with 'len' random bytes.
* Returns FAIL if the OS PRNG is not available or something went wrong.
*/
int
mch_get_random(char_u *buf, int len)
{
static int dev_urandom_state = NOTDONE;
if (dev_urandom_state == FAIL)
return FAIL;
int fd = open("/dev/urandom", O_RDONLY);
// Attempt reading /dev/urandom.
if (fd == -1)
dev_urandom_state = FAIL;
else if (read(fd, buf, len) == len)
dev_urandom_state = OK;
else
{
dev_urandom_state = FAIL;
close(fd);
}
return dev_urandom_state;
}
#if defined(FEAT_LIBCALL) || defined(PROTO)
typedef char_u * (*STRPROCSTR)(char_u *);
typedef char_u * (*INTPROCSTR)(int);