summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-03-05 12:55:46 -0500
committerJune McEnroe <june@causal.agency>2018-03-05 12:55:46 -0500
commit3e7377a479f49d2a8f2b0b893b6f8ea946b56fc9 (patch)
treed68aa786c7bd98dd44fa6b691a16bd6005fc1b18
parentClean up spawn constants (diff)
downloadtorus-3e7377a479f49d2a8f2b0b893b6f8ea946b56fc9.tar.gz
torus-3e7377a479f49d2a8f2b0b893b6f8ea946b56fc9.zip
Use stdio in merge and meta
Diffstat (limited to '')
-rw-r--r--merge.c42
-rw-r--r--meta.c7
2 files changed, 19 insertions, 30 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();
diff --git a/meta.c b/meta.c
index 8e3b6a3..ede1041 100644
--- a/meta.c
+++ b/meta.c
@@ -17,7 +17,6 @@
 #include <err.h>
 #include <stdio.h>
 #include <sysexits.h>
-#include <unistd.h>
 
 #include "torus.h"
 
@@ -25,9 +24,9 @@ int main() {
     printf("tileX,tileY,createTime,modifyCount,modifyTime,accessCount,accessTime\n");
     for (int i = 0;; ++i) {
         struct Tile tile;
-        ssize_t len = read(STDIN_FILENO, &tile, sizeof(tile));
-        if (len < 0) err(EX_IOERR, "read");
-        if (!len) return EX_OK;
+        size_t count = fread(&tile, sizeof(tile), 1, stdin);
+        if (ferror(stdin)) err(EX_IOERR, "(stdin)");
+        if (!count) return EX_OK;
 
         printf(
             "%d,%d,%ld,%u,%ld,%u,%ld\n",