aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/doc/builtin.txt7
-rw-r--r--src/nvim/eval.c5
-rw-r--r--src/nvim/testdir/test_blob.vim5
3 files changed, 14 insertions, 3 deletions
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index 113703680f..eab415f495 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -6164,7 +6164,12 @@ readblob({fname} [, {offset} [, {size}]]) *readblob()*
readblob('file.bin', 0, 100)
< If {size} is -1 or omitted, the whole data starting from
{offset} will be read.
- When the file can't be opened an error message is given and
+ This can be also used to read the data from a character device
+ on Unix when {size} is explicitly set. Only if the device
+ supports seeking {offset} can be used. Otherwise it should be
+ zero. E.g. to read 10 bytes from a serial console: >
+ readblob('/dev/ttyS0', 0, 10)
+< When the file can't be opened an error message is given and
the result is an empty |Blob|.
When trying to read bytes beyond the end of the file the
result is an empty blob.
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index c2f84735b2..92fd8a8de3 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -5893,10 +5893,11 @@ int read_blob(FILE *const fd, typval_T *rettv, off_T offset, off_T size_arg)
}
// Trying to read bytes that aren't there results in an empty blob, not an
// error.
- if (size < 0 || size > (off_T)os_fileinfo_size(&file_info)) {
+ if (size < 0 || (!S_ISCHR(file_info.stat.st_mode)
+ && size > (off_T)os_fileinfo_size(&file_info))) {
return OK;
}
- if (vim_fseek(fd, offset, whence) != 0) {
+ if (offset != 0 && vim_fseek(fd, offset, whence) != 0) {
return OK;
}
diff --git a/src/nvim/testdir/test_blob.vim b/src/nvim/testdir/test_blob.vim
index dd37fb2f11..5fc232119c 100644
--- a/src/nvim/testdir/test_blob.vim
+++ b/src/nvim/testdir/test_blob.vim
@@ -459,6 +459,11 @@ func Test_blob_read_write()
END
call CheckLegacyAndVim9Success(lines)
+ if filereadable('/dev/random')
+ let b = readblob('/dev/random', 0, 10)
+ call assert_equal(10, len(b))
+ endif
+
call assert_fails("call readblob('notexist')", 'E484:')
" TODO: How do we test for the E485 error?