]> git.mdlowis.com Git - projs/tide.git/commitdiff
reworked gapbuf_save
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 15 Oct 2019 20:00:29 +0000 (16:00 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 15 Oct 2019 20:00:29 +0000 (16:00 -0400)
src/lib/gapbuf.c
src/lib/job.c

index 03c93afb9141003bf75ffd13ba5e14fd56a23891..869e69281af8cc0679698fb8e0dae5433fe67382 100644 (file)
@@ -88,20 +88,28 @@ void gapbuf_init(GapBuf* buf)
     buf->gapend    = buf->bufend;
 }
 
+static long writefd(int fd, char* data, long towrite)
+{
+    long nwrite = 0;;
+    while (towrite && ((nwrite = write(fd, data, towrite)) > 0))
+    {
+        data += nwrite;
+        towrite -= nwrite;
+    }
+    return nwrite;
+}
+
 long gapbuf_save(GapBuf* buf, char* path)
 {
-    char* wptr;
-    long fd, nwrite = 0, towrite = 0;
+    long fd;
+    long nwrite;
     if (path && (fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0)
     {
-        /* write the chunk before the gap */
-        wptr = buf->bufstart, towrite = (buf->gapstart - buf->bufstart);
-        while (towrite && ((nwrite = write(fd, wptr, towrite)) > 0))
-            wptr += nwrite, towrite -= nwrite;
-        /* write the chunk after the gap */
-        wptr = buf->gapend, towrite = (buf->bufend - buf->gapend);
-        while (towrite && ((nwrite = write(fd, wptr, towrite)) > 0))
-            wptr += nwrite, towrite -= nwrite;
+        long nwrite = writefd(fd, buf->bufstart, (buf->gapstart - buf->bufstart));
+        if (nwrite >= 0)
+        {
+            nwrite = writefd(fd, buf->gapend, (buf->bufend - buf->gapend));
+        }
         close(fd);
     }
     else
index 17dfbad81ff29a6c5759829b3d3f9cca1ecc403a..127b7badf63e8d25e5e0ff4217f4f0d38dae94a0 100644 (file)
@@ -159,8 +159,15 @@ bool job_poll(int ms)
     }
     /* Poll until a job is ready, call the functions based on events */
     long ret = poll(JobFds, njobs, ms);
+    if (ret < 0)
+    {
+        perror("poll() failed: ");
+        exit(1);
+    }
     for (int i = 0; i < njobs; i++)
+    {
         job_process(JobFds[i].fd, JobFds[i].revents);
+    }
     /* reap zombie processes */
     for (int status; waitpid(-1, &status, WNOHANG) > 0;);
     return (ret > 0);