about summary refs log tree commit diff homepage
path: root/merge.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--merge.c42
1 files changed, 16 insertions, 26 deletions
diff --git a/merge.c b/merge.c
index 7075226..aec875a 100644
--- a/merge.c
+++ b/merge.c
@@ -19,22 +19,12 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <sysexits.h>
-#include <unistd.h>
 
 #include "torus.h"
 
-static ssize_t writeAll(int fd, const char *buf, size_t len) {
-    ssize_t writeLen;
-    while (0 < (writeLen = write(fd, buf, len))) {
-        buf += writeLen;
-        len -= writeLen;
-    }
-    return writeLen;
-}
-
 static void drawTile(int offsetY, const struct Tile *tile) {
-    for (int y = 0; y < CELL_ROWS; ++y) {
-        for (int x = 0; x < CELL_COLS; ++x) {
+    for (uint8_t y = 0; y < CELL_ROWS; ++y) {
+        for (uint8_t x = 0; x < CELL_COLS; ++x) {
             uint8_t color = tile->colors[y][x];
             char cell = tile->cells[y][x];
 
@@ -48,14 +38,14 @@ static void drawTile(int offsetY, const struct Tile *tile) {
 int main(int argc, char *argv[]) {
     if (argc != 4) return EX_USAGE;
 
-    int fileA = open(argv[1], O_RDONLY);
-    if (fileA < 0) err(EX_IOERR, "%s", argv[1]);
+    FILE *fileA = fopen(argv[1], "r");
+    if (!fileA) err(EX_NOINPUT, "%s", argv[1]);
 
-    int fileB = open(argv[2], O_RDONLY);
-    if (fileB < 0) err(EX_IOERR, "%s", argv[2]);
+    FILE *fileB = fopen(argv[2], "r");
+    if (!fileB) err(EX_NOINPUT, "%s", argv[2]);
 
-    int fileC = open(argv[3], O_WRONLY | O_CREAT, 0644);
-    if (fileC < 0) err(EX_IOERR, "%s", argv[3]);
+    FILE *fileC = fopen(argv[3], "w");
+    if (!fileC) err(EX_CANTCREAT, "%s", argv[3]);
 
     initscr();
     cbreak();
@@ -78,14 +68,14 @@ int main(int argc, char *argv[]) {
 
     struct Tile tileA, tileB;
     for (;;) {
-        ssize_t lenA = read(fileA, &tileA, sizeof(tileA));
-        if (lenA < 0) err(EX_IOERR, "%s", argv[1]);
+        size_t countA = fread(&tileA, sizeof(tileA), 1, fileA);
+        if (ferror(fileA)) err(EX_IOERR, "%s", argv[1]);
 
-        ssize_t lenB = read(fileB, &tileB, sizeof(tileB));
-        if (lenB < 0) err(EX_IOERR, "%s", argv[2]);
+        size_t countB = fread(&tileB, sizeof(tileB), 1, fileB);
+        if (ferror(fileB)) err(EX_IOERR, "%s", argv[2]);
 
-        if (!lenA && !lenB) break;
-        if (!lenA || !lenB) errx(EX_IOERR, "different size inputs");
+        if (!countA && !countB) break;
+        if (!countA || !countB) errx(EX_DATAERR, "different size inputs");
 
         const struct Tile *tileC = (tileA.accessTime > tileB.accessTime)
             ? &tileA
@@ -102,8 +92,8 @@ int main(int argc, char *argv[]) {
             tileC = (c == 'a') ? &tileA : &tileB;
         }
 
-        ssize_t lenC = writeAll(fileC, (char *)tileC, sizeof(*tileC));
-        if (lenC < 0) err(EX_IOERR, "%s", argv[3]);
+        fwrite(tileC, sizeof(*tileC), 1, fileC);
+        if (ferror(fileC)) err(EX_IOERR, "%s", argv[3]);
     }
 
     endwin();
8a39e1cec59144babfcc0466c69557e5030b&follow=1'>Add merge.c to READMEJune McEnroe 2017-09-03Assert client coords are valid after movementJune McEnroe 2017-09-03Relicense AGPLJune McEnroe I know it's already published under a permissive license in what is probably its final form, but I want to license it AGPL anyway on principle following some conversations I had about open source, corporations and copyleft. 2017-09-01Revert "Add client readOnly mode"June McEnroe This reverts commit 34f25ae40a3db9369e9d98b3814f2b93bbc21451. 2017-09-01Remove clientRemove call from clientCastJune McEnroe If an error occurs on a client socket during a broadcast, that client will show up in the kqueue loop with EV_EOF and get removed that way. Tested by sending SIGKILL to a client and watching its cursor disappear. 2017-09-01Add client readOnly modeJune McEnroe 2017-08-31Clean up merge toolJune McEnroe Choose the version with the most recent access if the modify times are the same. 2017-08-31Choose B for tiles with equal modify timesJune McEnroe This way newer access counts and times will be preserved. 2017-08-31Add quick data file merge toolJune McEnroe Hopefully I won't have to use it ever again. 2017-08-30Use only foreground color for selecting spawnJune McEnroe 2017-08-29Add four additional spawnsJune McEnroe 2017-08-28Add respawningJune McEnroe 2017-08-26Move license above includesJune McEnroe Why was it down there? 2017-08-26Snapshot metadataJune McEnroe 2017-08-26Add meta.c to READMEJune McEnroe 2017-08-26Use MakefileJune McEnroe