logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 6d37c7d61114da9b668fdad400c48dc7c737732c
parent 5e3962357625458ab6fef8f4f2eed510369a8411
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue,  8 Jun 2021 04:45:51 +0200

xcd: Add ability to read files

Diffstat:

Mbin/xcd.c58+++++++++++++++++++++++++++++++++++++++++++++++++++-------
Mtest-bin/xcd6++++++
Mtest-bin/xcd_all_bytes.fixture4++--
3 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/bin/xcd.c b/bin/xcd.c @@ -4,10 +4,11 @@ #define _POSIX_C_SOURCE 200809L #include <ctype.h> /* isprint() */ +#include <errno.h> /* errno */ #include <math.h> /* sin() */ #include <stdint.h> /* uint8_t */ -#include <stdio.h> /* printf(), fread() */ -#include <string.h> /* memset() */ +#include <stdio.h> /* printf(), fread(), fopen(), fclose() */ +#include <string.h> /* memset(), strerror() */ #define LANODAN_XCD_RESET printf(""); @@ -55,7 +56,7 @@ print_plain_rgb(unsigned char *line, size_t len) for(size_t i = 0; i < len; i++) { - c = line[i]; + c = line[i]; struct rgb color = rgb_char(c); printf("[38;2;%d;%d;%dm%c", color.red, color.green, color.blue, isprint(c) ? c : '.'); } @@ -67,13 +68,14 @@ print_plain_rgb(unsigned char *line, size_t len) #define WIDTH 16 int -main(void) +concat(FILE *stream) { - int cols = 0; + int cols = 0; char line[WIDTH]; unsigned char c; unsigned int bytes = 0; struct rgb pos_rgb; + errno = 0; memset(&line, 0, WIDTH); @@ -81,7 +83,7 @@ main(void) pos_rgb = rgb_char(bytes); printf("[38;2;%d;%d;%dm%06x ", pos_rgb.red, pos_rgb.green, pos_rgb.blue, bytes); - while(fread(&c, 1, 1, stdin) > 0) + while(fread(&c, 1, 1, stream) > 0) { if(cols >= WIDTH) { @@ -102,7 +104,7 @@ main(void) } // Fill the rest of the hex space with spaces - for(;cols < WIDTH; cols++) + for(; cols < WIDTH; cols++) printf(" "); LANODAN_XCD_RESET @@ -114,3 +116,45 @@ main(void) return 0; } + +int +main(int argc, char *argv[]) +{ + int err = 0; + + if(argc <= 1) + { + err += concat(stdin); + } + else + { + for(int argi = 1; err == 0, argi < argc; argi++) + { + if(strncmp(argv[argi], "-", 2) == 0) + { + err += concat(stdin); + } + else + { + FILE *file = fopen(argv[argi], "r"); + if(!file) + { + printf("\nError opening ā€˜%sā€™: %s\n", argv[argi], strerror(errno)); + } + else + { + err += concat(file); + + if((err += fclose(file)) != 0) + { + printf("\nError closing ā€˜%sā€™: %s\n", argv[argi], strerror(errno)); + } + } + } + } + } + + printf(""); + + return err; +} diff --git a/test-bin/xcd b/test-bin/xcd @@ -1,4 +1,9 @@ #!/usr/bin/env atf-sh +atf_test_case openfile +openfile_body() { + atf_check -o file:xcd_all_bytes.fixture ../bin/xcd all_bytes +} + atf_test_case stdinput stdinput_body() { atf_check -o file:xcd_all_bytes.fixture ../bin/xcd <all_bytes @@ -6,5 +11,6 @@ stdinput_body() { atf_init_test_cases() { cd "$(atf_get_srcdir)" + atf_add_test_case openfile atf_add_test_case stdinput } diff --git a/test-bin/xcd_all_bytes.fixture b/test-bin/xcd_all_bytes.fixture @@ -15,4 +15,4 @@ 0000e0 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef  >................< 0000f0 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff  >................< 000100 -- \ No newline at end of file ++ \ No newline at end of file