From 8a68f6dd4021986e10760a3a36d453809ac4be36 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 11 Mar 2020 10:07:27 -0700 Subject: 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. --- toys/pending/vi.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'toys/pending/vi.c') 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; -- cgit v1.2.3