logo

utils

~/.local/bin tools and git-hooks
commit: b117e65252d79f88e68087d2d5e4480b3d86c432
parent: cefaecfb8d425a65c4527f365719e6073f267919
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed,  6 May 2020 06:48:34 +0200

src → bin: will allow to have sbin for system ones

Diffstat:

Rsrc/args.c -> bin/args.c0
Abin/date.144++++++++++++++++++++++++++++++++++++++++++++
Abin/date.c58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/echo.c -> bin/echo.c0
Rsrc/lolcat.1 -> bin/lolcat.10
Rsrc/lolcat.c -> bin/lolcat.c0
Rsrc/pwd.c -> bin/pwd.c0
Rsrc/range.c -> bin/range.c0
Rsrc/sizeof.c -> bin/sizeof.c0
Abin/xcd.c73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmkfile23+++++++++++++----------
Dsrc/date.145---------------------------------------------
Dsrc/date.c58----------------------------------------------------------
Dsrc/xcd.c69---------------------------------------------------------------------
14 files changed, 188 insertions(+), 182 deletions(-)

diff --git a/src/args.c b/bin/args.c diff --git a/bin/date.1 b/bin/date.1 @@ -0,0 +1,44 @@ +.Dd 2018-10-15 +.Dt DATE 1 +.Os +.Sh NAME +.Nm date +.Nd display date and time +.Sh SYNOPSIS +.Nm +.Op Fl u +.Op Cm + Ns Ar format +.Sh DESCRIPTION +When +.Nm +is invoked without arguments it displays the current datetime +Otherwise, depending on the options specified, will print the datetime in a user-defined way. +.Bl -tag -width Ds +.It Fl u +Use UTC (coordinated universal time) instead of the local time. +.El +.Pp +The plus operand +.Pq Sq + +specifies in which format the datetime should be displayed, the format string is specified in the +.Xr strftime 3 +manual page. +.Sh ENVIRONMENT +Look at the manual page of +.Xr strftime 3 +for the environment variables, typical ones are +.Ev TZ , +.Ev LC_TIME and +.Ev LC_ALL but that depends on your system. +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr strftime 3 +.Sh STANDARDS +.Nm +is compliant wit the +.St -p1003.1-2008 +specification. +.Pp +.Sh AUTHORS +.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me diff --git a/bin/date.c b/bin/date.c @@ -0,0 +1,58 @@ +/* Copyright CC-BY-SA-4.0 2017-2020 Haelwenn (lanodan) Monnier <contact@hacktivis.me> */ +#include <locale.h> /* setlocale() */ +#include <stdio.h> /* BUFSIZ, perror(), puts() */ +#include <stdlib.h> /* exit() */ +#include <time.h> /* time, localtime, tm, strftime */ +#include <unistd.h> /* getopt(), optarg, optind */ + +int main(int argc, char *argv[]) +{ + char outstr[BUFSIZ]; + struct tm *tm; + time_t now = time(NULL); + char *format = "%c"; + int uflag = 0, dflag = 0; + int c; + + setlocale(LC_ALL, ""); + + while ((c = getopt(argc, argv, ":u")) != -1) { + switch(c) { + case 'u': /* UTC timezone */ + uflag++; + break; + } + } + + if(uflag) { + tm = gmtime(&now); + + if(tm == NULL) + { + perror("gmtime"); + exit(EXIT_FAILURE); + } + } else { + tm = localtime(&now); + + if(tm == NULL) + { + perror("localtime"); + exit(EXIT_FAILURE); + } + } + + argc -= optind; + argv += optind; + + if (*argv && **argv == '+') + format = *argv + 1; + + if(strftime(outstr, sizeof(outstr), format, tm) == 0) + { + perror("strftime"); + exit(EXIT_FAILURE); + } + + return puts(outstr); +} diff --git a/src/echo.c b/bin/echo.c diff --git a/src/lolcat.1 b/bin/lolcat.1 diff --git a/src/lolcat.c b/bin/lolcat.c diff --git a/src/pwd.c b/bin/pwd.c diff --git a/src/range.c b/bin/range.c diff --git a/src/sizeof.c b/bin/sizeof.c diff --git a/bin/xcd.c b/bin/xcd.c @@ -0,0 +1,73 @@ +// Copyright 2018-2020 Haelwenn (lanodan) Monnier <contact@hacktivis.me> +// Distributed under the terms of the CC-BY-SA-4.0 license + +#include <stdio.h> /* printf(), fread() */ +#include <math.h> /* sin() */ +#include <stdint.h> /* uint8_t */ +#include <string.h> /* memset() */ +#include <ctype.h> /* isprint() */ + +#define LANODAN_XCD_RESET printf(""); +#define LANODAN_XCD_PRINT printf(" >%s<", line); + +void rainbow(double freq, char i) { + if(i == 0) { + printf(""); + } else { + uint8_t red, green, blue; + double pi = 3.14159; + + red = sin(freq*i + 0) * 127 + 128; + green = sin(freq*i + 2*pi/3) * 127 + 128; + blue = sin(freq*i + 4*pi/3) * 127 + 128; + + printf("[38;2;%02d;%02d;%02dm", red, green, blue); + } +} + +int main(void) { + int cols = 0, bytes = 0; + int line_width = 16; + double freq = 0.2; + char c, line[line_width]; + + memset(&line, 0, line_width); + + LANODAN_XCD_RESET + + rainbow(freq, bytes); + printf("%06x ", bytes); + while(fread(&c, 1, 1, stdin) > 0) + { + if(cols >= line_width) { + cols = 0; + + LANODAN_XCD_RESET + LANODAN_XCD_PRINT + memset(&line, 0, line_width); + + rainbow(freq, bytes); + printf("\n%06x ", bytes); + } + + rainbow(freq, c); + printf("%02hhx ", c); + line[cols] = isprint(c) ? c : '.'; + + cols++; + bytes++; + } + + + // Fill the rest of the hex space with spaces + for(cols; cols < line_width; cols++) printf(" "); + + LANODAN_XCD_RESET + LANODAN_XCD_PRINT + + rainbow(freq, bytes); + printf("\n%06x\n", bytes); + LANODAN_XCD_RESET + + return 0; +} diff --git a/mkfile b/mkfile @@ -7,23 +7,26 @@ BINDIR=$PREFIX"/bin" DOCDIR=$PREFIX"/share/doc/lanodan-utils" MANDIR=$PREFIX"/share/man" -bins=`ls -d src/*.c | sed -e 's;src;bin;' -e 's;\.c$;;'` +bins=`ls -d *bin/*.c | sed 's;\.c$;;'` all:V: $bins install:V: all - for bin in bin/*; do cp $bin $BINDIR/; done - for doc in doc/*; do cp $doc $DOCDIR/; done - for man in src/*.[1-9]; do cp $man $MANDIR/man${man: -1}/; done + mkdir -p $DESTDIR$BINDIR/ + for bin in $bins; do cp $bin $DESTDIR$BINDIR/; done + mkdir -p $DESTDIR$DOCDIR/ + for doc in doc/*; do cp $doc $DESTDIR$DOCDIR/; done + mkdir -p $DESTDIR$MANDIR/ + for man in *bin/*.[1-9]; do cp $man $DESTDIR$MANDIR/man${man: -1}/; done clean:V: rm -f $bins -bin/&: src/&.c - $CC $CDEFS $CFLAGS src/$stem.c -o bin/$stem +bin/&: bin/&.c + $CC $CDEFS $CFLAGS bin/$stem.c -o bin/$stem -bin/lolcat: src/lolcat.c - $CC $CDEFS $CFLAGS -lm src/lolcat.c -o bin/lolcat +bin/lolcat: bin/lolcat.c + $CC $CDEFS $CFLAGS -lm bin/lolcat.c -o bin/lolcat -bin/xcd: src/xcd.c - $CC $CDEFS $CFLAGS -lm src/xcd.c -o bin/xcd +bin/xcd: bin/xcd.c + $CC $CDEFS $CFLAGS -lm bin/xcd.c -o bin/xcd diff --git a/src/date.1 b/src/date.1 @@ -1,45 +0,0 @@ -.Dd 2018-10-15 -.Dt DATE 1 -.Os -.Sh NAME -.Nm date -.Nd display date and time -.Sh SYNOPSIS -.Nm -.Op Fl du -.Op Cm + Ns Ar format -.Sh DESCRIPTION -When -.Nm -is invoked without arguments it displays the current datetime -Otherwise, depending on the options specified, will print the datetime in a user-defined way. -.Bl -tag -width Ds -.It Fl u -Use UTC (Coordinated Universal) time instead of the local time. -.It Fl d -Use the time provided after this option instead of the system one. -.Pp -The plus operand -.Pq Sq + -specifies in which format the datetime should be displayed, the format string is specified in the -.Xr strftime 3 -manual page. -.Sh ENVIRONMENT -Look at the manual page of -.Xr strftime 3 -for the environment variables, typical ones are TZ and LC_TIME/LC_ALL, but that depends on your system. -.Sh EXIT STATUS -.Ex -std -.Sh SEE ALSO -.Xr strftime 3 -.Sh STANDARDS -.Nm -is compliant wit the -.St -p1003.1-2008 -specification. -.Pp -The -.Op Fl d -flag is an extension to this specification. -.Sh AUTHORS -.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me diff --git a/src/date.c b/src/date.c @@ -1,58 +0,0 @@ -/* Copyright CC-BY-SA-4.0 2017-2018 Haelwenn (lanodan) Monnier <contact@hacktivis.me> */ -#include <locale.h> /* setlocale() */ -#include <stdio.h> /* BUFSIZ, perror(), puts() */ -#include <stdlib.h> /* exit() */ -#include <time.h> /* time, localtime, tm, strftime */ -#include <unistd.h> /* getopt(), optarg, optind */ - -int main(int argc, char *argv[]) -{ - char outstr[BUFSIZ]; - struct tm *tm; - time_t now; - char *format = "%c"; - int uflag = 0, dflag = 0; - int c; - - setlocale(LC_ALL, ""); - - while ((c = getopt(argc, argv, ":ud:")) != -1) { - switch(c) { - case 'd': /* user-provided datetime */ - now = time(optarg); - dflag++; - break; - case 'u': /* Timezone is UTC */ - uflag++; - break; - } - } - - if(!dflag) - now = time(NULL); - - if(uflag) - tm = gmtime(&now); - else - tm = localtime(&now); - - argc -= optind; - argv += optind; - - if (*argv && **argv == '+') - format = *argv + 1; - - if(tm == NULL) - { - perror("localtime"); - exit(EXIT_FAILURE); - } - - if(strftime(outstr, sizeof(outstr), format, tm) == 0) - { - perror("strftime returned 0"); - exit(EXIT_FAILURE); - } - - return puts(outstr); -} diff --git a/src/xcd.c b/src/xcd.c @@ -1,69 +0,0 @@ -// Copyright 2018-2020 Haelwenn (lanodan) Monnier <contact@hacktivis.me> -// Distributed under the terms of the CC-BY-SA-4.0 license - -#include <stdio.h> /* printf(), fread() */ -#include <math.h> /* sin() */ -#include <stdint.h> /* uint8_t */ -#include <string.h> /* memset() */ -#include <ctype.h> /* isprint() */ - -#define LANODAN_XCD_RESET printf(""); -#define LANODAN_XCD_PRINT printf(" >%s<", line); - -void rainbow(double freq, char i) { - uint8_t red, green, blue; - double pi = 3.14159; - - red = sin(freq*i + 0) * 127 + 128; - green = sin(freq*i + 2*pi/3) * 127 + 128; - blue = sin(freq*i + 4*pi/3) * 127 + 128; - - printf("[38;2;%02d;%02d;%02dm", red, green, blue); -} - -int main(void) { - int cols = 0, bytes = 0; - int line_width = 16; - double freq = 0.2; - char c, line[line_width]; - - memset(&line, 0, line_width); - - LANODAN_XCD_RESET - - rainbow(freq, bytes); - printf("%06x ", bytes); - while(fread(&c, 1, 1, stdin) > 0) - { - if(cols >= line_width) { - cols = 0; - - LANODAN_XCD_RESET - LANODAN_XCD_PRINT - memset(&line, 0, line_width); - - rainbow(freq, bytes); - printf("\n%06x ", bytes); - } - - rainbow(freq, c); - printf("%02hhx ", c); - line[cols] = isprint(c) ? c : '.'; - - cols++; - bytes++; - } - - - // Fill the rest of the hex space with spaces - for(cols; cols < line_width; cols++) printf(" "); - - LANODAN_XCD_RESET - LANODAN_XCD_PRINT - - rainbow(freq, bytes); - printf("\n%06x\n", bytes); - LANODAN_XCD_RESET - - return 0; -}