From de6746127b335345e0a1c89cdd330e8968182218 Mon Sep 17 00:00:00 2001 From: June 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 480a4b6d..b1039d80 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