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); }