From b316a71a8280910ee996c73ce85e0859ff066943 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 29 May 2015 11:59:01 +0000 Subject: [PATCH] Make things a little less confusing by making fill_buffer self-contained and return both the buffer and amount filled rather than having it modify the input struct and return a value. --- usr.bin/file/file.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/usr.bin/file/file.c b/usr.bin/file/file.c index 7402fa00c04..406e64b74d9 100644 --- a/usr.bin/file/file.c +++ b/usr.bin/file/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.41 2015/05/29 11:03:37 nicm Exp $ */ +/* $OpenBSD: file.c,v 1.42 2015/05/29 11:59:01 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott @@ -371,7 +371,7 @@ child(int fd, pid_t parent, int argc, char **argv) } static void * -fill_buffer(struct input_file *inf) +fill_buffer(int fd, size_t size, size_t *used) { static void *buffer; ssize_t got; @@ -382,9 +382,9 @@ fill_buffer(struct input_file *inf) buffer = xmalloc(FILE_READ_SIZE); next = buffer; - left = inf->size; + left = size; while (left != 0) { - got = read(inf->fd, next, left); + got = read(fd, next, left); if (got == -1) { if (errno == EINTR) continue; @@ -395,13 +395,15 @@ fill_buffer(struct input_file *inf) next = (char *)next + got; left -= got; } - inf->size -= left; + *used = size - left; return buffer; } static int load_file(struct input_file *inf) { + size_t used; + inf->size = inf->msg->sb.st_size; if (inf->size > FILE_READ_SIZE) inf->size = FILE_READ_SIZE; @@ -414,12 +416,13 @@ load_file(struct input_file *inf) inf->base = mmap(NULL, inf->size, PROT_READ, MAP_PRIVATE, inf->fd, 0); if (inf->base == MAP_FAILED) { - inf->base = fill_buffer(inf); + inf->base = fill_buffer(inf->fd, inf->size, &used); if (inf->base == NULL) { xasprintf(&inf->result, "cannot read '%s' (%s)", inf->path, strerror(errno)); return (1); } + inf->size = used; } else inf->mapped = 1; return (0); -- 2.20.1