From 02bb28d98b614303f4a0dbcc4ea2f327ac46ed80 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 5 Mar 2018 12:55:46 -0500 Subject: Use stdio in merge and meta --- merge.c | 42 ++++++++++++++++-------------------------- meta.c | 7 +++---- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/merge.c b/merge.c index bf1a1b5..8ca5267 100644 --- a/merge.c +++ b/merge.c @@ -19,22 +19,12 @@ #include #include #include -#include #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 cc6e262..3f4dcc8 100644 --- a/meta.c +++ b/meta.c @@ -17,7 +17,6 @@ #include #include #include -#include #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", -- cgit 1.4.1 t'> 2019-04-13Add The Demolished ManJune McEnroe 2019-04-07Add AmatkaJune McEnroe 2019-04-03Add Space OperaJune McEnroe 2019-03-22Add An Unkindness of GhostsJune McEnroe 2019-03-14Highlight line continuations in line commentsJune McEnroe 2019-03-14Add The Long Way to a Small Angry PlanetJune McEnroe 2019-03-10Add Bleachers Tiny Desk ConcertJune McEnroe 2019-02-28Add Station ElevenJune McEnroe 2019-02-27Use getsubopt(3) to parse -o options in hiJune McEnroe 2019-02-27Add type and ulimit shell keywordsJune McEnroe 2019-02-27Fix make target pattern whitespace in hiJune McEnroe 2019-02-27Delete extraneous <pre> in html.shJune McEnroe 2019-02-24Use setopt in upJune McEnroe 2019-02-24Add setoptJune McEnroe 2019-02-20Match make targets as TagJune McEnroe 2019-02-20Use $PWD in html.sh to pass absolute path to manJune McEnroe 2019-02-20Factor out html.sh and produce html for all sourcesJune McEnroe Except for gfx-cocoa because I don't have an Objective-C highlighter. Also hypothetically the .h rule could match up headers with .3 manual pages if I cared to write them. 2019-02-20Add Dirk Gently's Holistic Detective AgencyJune McEnroe 2019-02-20Move /opt/pkg after /usr in PATHJune McEnroe Installing mandoc from pkgsrc also installs a man executable which is horribly broken. Keep using the system man. 2019-02-20Set man_hard_wrap in nvimJune McEnroe Vendoring man.vim until <https://github.com/neovim/neovim/pull/9633> is merged and appears in a release. 2019-02-18Use hi -o anchor in upJune McEnroe 2019-02-18Don't match nested parentheses in Tag for CJune McEnroe Fixes mistaken highlight of: pngWrite(file, (uint8_t []) { 0, 0, 0 }, 3); 2019-02-18Match whitespace between * [] {}June McEnroe 2019-02-18Fix function-like #define regexJune McEnroe A define like #define FOO (1) is not function-like. 2019-02-18Match Tag in RustJune McEnroe 2019-02-18Match sh functions as TagJune McEnroe 2019-02-18Match Sh and Ss as Tag in mdocJune McEnroe 2019-02-18Match statics and typedefs as TagJune McEnroe 2019-02-18Clean up htmlHeaderJune McEnroe 2019-02-18Remove hi line numberingJune McEnroe Tags are much better for referring to specific parts of a file and line numbering is better done by a post-processing tool such as cat -n or producing a two-column HTML <table>. 2019-02-18Add Tag class to hiJune McEnroe 2019-02-17Generate HTML with hi -n -f html -o anchorJune McEnroe Running hi twice to insert stuff between the head and the content is a bit of a hack but oh well. 2019-02-17Add hi -f html -o anchor for line number linksJune McEnroe 2019-02-17Simplify temp trap in upJune McEnroe 2019-02-17Add line numbers to hiJune McEnroe Renames previous -n option to -m to stay consistent with cat -n. Prefixing lines with line numbers affects where the first tab indent ends up relative to the text above it. Not sure if it's worth fixing somehow. 2019-02-17Always split spans after newlinesJune McEnroe Simplifies ANSI and IRC output code, and prepares for line numbered output. 2019-02-15Color format specifiers light cyan in vimJune McEnroe 2019-02-15Highlight Interp as yellowJune McEnroe 2019-02-15Highlight strings in sh command substitutionsJune McEnroe 2019-02-15Add nmap gpJune McEnroe 2019-02-14Avoid newline when copying URL to pasteboardJune McEnroe 2019-02-13Add forgotten "sixth" book of H2G2June McEnroe