Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/vndcompress Simplify.



details:   https://anonhg.NetBSD.org/src/rev/92beee7918e2
branches:  trunk
changeset: 822470:92beee7918e2
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Tue Mar 21 13:56:38 2017 +0000

description:
Simplify.

diffstat:

 usr.bin/vndcompress/utils.c |  74 ++++++++++++++++++--------------------------
 1 files changed, 30 insertions(+), 44 deletions(-)

diffs (114 lines):

diff -r f0a954253c9e -r 92beee7918e2 usr.bin/vndcompress/utils.c
--- a/usr.bin/vndcompress/utils.c       Tue Mar 21 13:49:03 2017 +0000
+++ b/usr.bin/vndcompress/utils.c       Tue Mar 21 13:56:38 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: utils.c,v 1.5 2016/04/07 23:29:59 riastradh Exp $      */
+/*     $NetBSD: utils.c,v 1.6 2017/03/21 13:56:38 riastradh Exp $      */
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: utils.c,v 1.5 2016/04/07 23:29:59 riastradh Exp $");
+__RCSID("$NetBSD: utils.c,v 1.6 2017/03/21 13:56:38 riastradh Exp $");
 
 #include <sys/types.h>
 
@@ -59,31 +59,23 @@
  * Read, returning partial data only at end of file.
  */
 ssize_t
-read_block(int fd, void *buffer, size_t n)
+read_block(int fd, void *buf, size_t len)
 {
-       char *p = buffer, *const end __diagused = (p + n);
-       size_t total_read = 0;
+       char *p = buf;
+       size_t n = len;
+       const char *const end __diagused = p + n;
+       ssize_t nread = 0;
 
-       while (n > 0) {
-               const ssize_t n_read = read(fd, p, n);
-               if (n_read == -1)
+       while (0 < n && (nread = read(fd, p, n)) != 0) {
+               if (nread == -1)
                        return -1;
-               assert(n_read >= 0);
-               if (n_read == 0)
-                       break;
-
-               assert((size_t)n_read <= n);
-               n -= (size_t)n_read;
-
-               assert(p <= end);
-               assert(n_read <= (end - p));
-               p += (size_t)n_read;
-
-               assert((size_t)n_read <= (SIZE_MAX - total_read));
-               total_read += (size_t)n_read;
+               p += MIN(n, (size_t)nread);
+               n -= MIN(n, (size_t)nread);
+               assert(p + n == end);
        }
 
-       return total_read;
+       assert(n == 0 || nread == 0); /* complete read or EOF */
+       return len - n;
 }
 
 /*
@@ -91,35 +83,29 @@
  * of file.
  */
 ssize_t
-pread_block(int fd, void *buffer, size_t n, off_t fdpos)
+pread_block(int fd, void *buf, size_t len, off_t fdpos)
 {
-       char *p = buffer, *const end __diagused = (p + n);
-       size_t total_read = 0;
+       char *p = buf;
+       size_t n = len;
+       const char *const end __diagused = p + n;
+       ssize_t nread = 0;
 
        assert(0 <= fdpos);
-       assert(n <= (OFF_MAX - (uintmax_t)fdpos));
+       assert(n <= OFF_MAX - (uintmax_t)fdpos);
+       const off_t endpos __diagused = fdpos + n;
 
-       while (n > 0) {
-               assert(total_read <= n);
-               const ssize_t n_read = pread(fd, p, n, (fdpos + total_read));
-               if (n_read == -1)
+       while (0 < n && (nread = pread(fd, p, n, fdpos)) != 0) {
+               if (nread == -1)
                        return -1;
-               assert(n_read >= 0);
-               if (n_read == 0)
-                       break;
-
-               assert((size_t)n_read <= n);
-               n -= (size_t)n_read;
-
-               assert(p <= end);
-               assert(n_read <= (end - p));
-               p += (size_t)n_read;
-
-               assert((size_t)n_read <= (SIZE_MAX - total_read));
-               total_read += (size_t)n_read;
+               fdpos += MIN(n, (size_t)nread);
+               p += MIN(n, (size_t)nread);
+               n -= MIN(n, (size_t)nread);
+               assert(p + n == end);
+               assert(fdpos + (off_t)n == endpos);
        }
 
-       return total_read;
+       assert(n == 0 || nread == 0); /* complete read or EOF */
+       return len - n;
 }
 
 /*



Home | Main Index | Thread Index | Old Index