From a01cea572daf71b08624824941b561723967bf80 Mon Sep 17 00:00:00 2001 From: DTB Date: Fri, 19 Jul 2024 18:26:59 -0600 Subject: [PATCH] dj(1): replace do/while on hard seeking --- src/dj.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/dj.c b/src/dj.c index 22772cb..4af59ff 100644 --- a/src/dj.c +++ b/src/dj.c @@ -259,28 +259,27 @@ int main(int argc, char *argv[]) { } } - /* hard seeking */ - if (io[1].seek > 0) { - size_t t; + assert(io[1].bufuse == 0); /* requirement for hard seeking */ - do { - memset( - io[1].buf, '\0', - (t = io[1].bufuse = MIN(io[1].bs, io[1].seek)) - ); + /* hard seeking; t is io[1].bufuse, before Io_write subtracts from it */ + for(size_t t; io[1].seek > 0; io[1].seek -= (t - io[1].bufuse)) { + memset( + io[1].buf, '\0', /* set buf to all nulls */ + (t = io[1].bufuse = MIN(io[1].bs, io[1].seek)) /* saturate block */ + ); - if (Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) { - Io_write(&io[1]); /* second chance */ - } - if (io[1].error != 0) { - return oserr(io[1].fn, io[1].error); - } - } while ((io[1].seek -= (t - io[1].bufuse)) > 0 && io[1].bufuse != t); + if (Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) { + Io_write(&io[1]); /* second chance */ + } - io[1].bufuse = 0; + if (io[1].error != 0) { return oserr(io[1].fn, io[1].error); } + + if (io[1].bufuse == t) { break; } /* all writes failed! */ } - if (io[1].seek > 0) { + io[1].bufuse = 0; + + if (io[1].seek > 0) { /* hard seeking failed */ fprintio(stderr, fmt, io); return oserr(io[1].fn, errno); }