From 9ec79d7712b82c9642eff752cbb47dd2f40b66f1 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Fri, 23 Feb 2018 16:15:54 -0500 Subject: Clean up fbclock You can't use the return value of gzerror to check if an error occurred or not. Its implementation actually checks if the internal error is NULL and returns the empty string if it is! This is stupid and unhelpful, so check gzeof first since its return value actually means something. --- bin/fbclock.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'bin') diff --git a/bin/fbclock.c b/bin/fbclock.c index 97d931f7..605fa4e0 100644 --- a/bin/fbclock.c +++ b/bin/fbclock.c @@ -44,30 +44,40 @@ static const uint32_t BG = 0x1D2021; static const uint32_t FG = 0xA99A84; int main() { - size_t count; + size_t len; const char *fontPath = getenv("FONT"); - if (!fontPath) fontPath = "/usr/share/kbd/consolefonts/Lat2-Terminus16.psfu.gz"; + if (!fontPath) { + fontPath = "/usr/share/kbd/consolefonts/Lat2-Terminus16.psfu.gz"; + } gzFile font = gzopen(fontPath, "r"); if (!font) err(EX_NOINPUT, "%s", fontPath); struct Psf2Header header; - count = gzfread(&header, sizeof(header), 1, font); - if (!count) errx(EX_IOERR, "%s: %s", fontPath, gzerror(font, NULL)); + len = gzfread(&header, sizeof(header), 1, font); + if (!len && gzeof(font)) errx(EX_DATAERR, "%s: missing header", fontPath); + if (!len) errx(EX_IOERR, "%s", gzerror(font, NULL)); if (header.magic != PSF2_MAGIC) { - errx(EX_DATAERR, "%s: invalid header magic %#x", fontPath, header.magic); + errx( + EX_DATAERR, "%s: invalid header magic %08x", + fontPath, header.magic + ); } if (header.headerSize != sizeof(struct Psf2Header)) { - errx(EX_DATAERR, "%s: weird header size %d", fontPath, header.headerSize); + errx( + EX_DATAERR, "%s: weird header size %d", + fontPath, header.headerSize + ); } uint8_t glyphs[128][header.glyphSize]; - count = gzfread(glyphs, header.glyphSize, 128, font); - if (!count) errx(EX_IOERR, "%s: %s", fontPath, gzerror(font, NULL)); + len = gzfread(glyphs, header.glyphSize, 128, font); + if (!len && gzeof(font)) errx(EX_DATAERR, "%s: missing glyphs", fontPath); + if (!len) errx(EX_IOERR, "%s", gzerror(font, NULL)); - assert(Z_OK == gzclose(font)); + gzclose(font); const char *fbPath = getenv("FRAMEBUFFER"); if (!fbPath) fbPath = "/dev/fb0"; @@ -79,8 +89,8 @@ int main() { int error = ioctl(fb, FBIOGET_VSCREENINFO, &info); if (error) err(EX_IOERR, "%s", fbPath); - size_t len = 4 * info.xres * info.yres; - uint32_t *buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); + size_t size = 4 * info.xres * info.yres; + uint32_t *buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); if (buf == MAP_FAILED) err(EX_IOERR, "%s", fbPath); for (;;) { @@ -90,10 +100,10 @@ int main() { if (!local) err(EX_OSERR, "localtime"); char str[64]; - size_t len = strftime(str, sizeof(str), "%H:%M", local); + len = strftime(str, sizeof(str), "%H:%M", local); assert(len); - for (int i = 0; i < (60 - local->tm_sec); ++i) { + for (int i = 0; i < (60 - local->tm_sec); ++i, sleep(1)) { uint32_t left = info.xres - header.glyphWidth * len; uint32_t bottom = header.glyphHeight; @@ -105,7 +115,7 @@ int main() { } for (const char *s = str; *s; ++s) { - uint8_t *glyph = glyphs[(uint8_t)*s]; + const uint8_t *glyph = glyphs[(unsigned)*s]; uint32_t stride = header.glyphSize / header.glyphHeight; for (uint32_t y = 0; y < header.glyphHeight; ++y) { for (uint32_t x = 0; x < header.glyphWidth; ++x) { @@ -116,8 +126,6 @@ int main() { } left += header.glyphWidth; } - - sleep(1); } } } -- cgit 1.4.1 over-highlight'> Why even? 2020-06-07Cast z_stream fields to size_tJune McEnroe In the version of zlib in OpenBSD, these fields are of type off_t, which is signed (why?), rather than uLong. 2020-06-07Call static_assert by _Static_assertJune McEnroe OpenBSD doesn't #define static_assert in assert.h and _Static_assert is its real name I guess so why not? 2020-06-07Add OpenBSD to install.shJune McEnroe Straightforward. 2020-06-03Add The Song of AchillesJune McEnroe 2020-06-01Allow redirecting input in everJune McEnroe 2020-05-31Add %c conversion to c scriptJune McEnroe 2020-05-31Add c script to READMEJune McEnroe 2020-05-31Add c scriptJune McEnroe 2020-05-31Update mdoc source URLsJune McEnroe 2020-05-26Remove unfinished PSF fontsJune McEnroe