aboutsummaryrefslogtreecommitdiff
path: root/toys/pending
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2020-03-11 10:07:27 -0700
committerRob Landley <rob@landley.net>2020-03-11 20:24:04 -0500
commit8a68f6dd4021986e10760a3a36d453809ac4be36 (patch)
tree7bfef02c278db5ecc465945f08f23d32a43cb61a /toys/pending
parente400e605471a5bc6571623b4446c41bfc9eb4942 (diff)
downloadtoybox-8a68f6dd4021986e10760a3a36d453809ac4be36.tar.gz
vi: don't keep fd open unnecessarily.
As soon as mmap() is done, we can close the fd. xmmap() also will exit rather than return failure so we can remove that check, and fdlength() will fall back to lseek() so there's no need to have the fallback in vi itself. Spotted because the `TT.fd = 0` in linelist_unload() seemed suspicious; -1 would have been more natural.
Diffstat (limited to 'toys/pending')
-rw-r--r--toys/pending/vi.c22
1 files changed, 8 insertions, 14 deletions
diff --git a/toys/pending/vi.c b/toys/pending/vi.c
index b5f34e21..62cbbab0 100644
--- a/toys/pending/vi.c
+++ b/toys/pending/vi.c
@@ -41,7 +41,7 @@ GLOBALS(
char* data;
} yank;
- int modified, fd;
+ int modified;
size_t filesize;
// mem_block contains RO data that is either original file as mmap
// or heap allocated inserted data
@@ -509,8 +509,7 @@ static void linelist_unload()
{
llist_traverse((void *)TT.slices, llist_free_double);
llist_traverse((void *)TT.text, block_list_free);
- if (TT.fd) xclose(TT.fd);
- TT.slices = 0, TT.text = 0, TT.fd = 0;
+ TT.slices = 0, TT.text = 0;
}
static int linelist_load(char *filename)
@@ -518,20 +517,15 @@ static int linelist_load(char *filename)
if (!filename) filename = (char*)*toys.optargs;
if (filename) {
- int fd;
- size_t len, size;
+ int fd = open(filename, O_RDONLY);
+ size_t size;
char *data;
- if ( (fd = open(filename, O_RDONLY)) <0) return 0;
- size = fdlength(fd);
- if (!(len = lseek(fd, 0, SEEK_END))) len = size;
- lseek(fd, 0, SEEK_SET);
-
- data = xmmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
- if (data == MAP_FAILED) return 0;
- insert_str(data, 0, size, len, MMAP);
+ if (fd == -1) return 0;
+ data = xmmap(0, size = fdlength(fd), PROT_READ, MAP_SHARED, fd, 0);
+ xclose(fd);
+ insert_str(data, 0, size, size, MMAP);
TT.filesize = text_filesize();
- TT.fd = fd;
}
return 1;