Skip to content

Instantly share code, notes, and snippets.

View izabera's full-sized avatar

Isabella Bosia izabera

  • DDN
  • Flitwick, UK
View GitHub Profile
@izabera
izabera / rpn.bash
Created February 28, 2025 08:08
basic rpn calculator with assignments
set -f
IFS=
rpn () {
for token do
case $token in
+|-|\*|/|%|\*\*) (( stack[-2] = stack[-2] $token stack[-1] )); unset stack[-1] ;;
=) (( ${stack[-2]} = stack[-1] )); unset stack[-1] stack[-1] ;;
*) stack+=($token) ;;
esac
@izabera
izabera / ipctest.c
Last active February 4, 2025 23:49
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <pty.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/wait.h>
@izabera
izabera / sincos
Last active January 19, 2025 12:53
#!/bin/bash
pi=205667
scale=65536
maskf0=-$scale
mask0f=$((scale-1))
shift=16
pisq=42389628127
pi2=411775
pi_2=102944
pi_4=51472
@izabera
izabera / recursive_exp.md
Last active January 7, 2025 11:20
recursive expansions

for the latest chapter of what's becoming a blog on the most cursed bash you can imagine, let's do some maths together

euclid's algorithm for gcd could be written like this in python:

>>> def gcd(a, b):
...     if b:
...         return gcd(b, a%b)
... return a
{n=$1;r=n":";for(p=2;p*p<=n;p++)while(n%p==0){r=r" "p;n/=p}$1=n>1?r" "n:r}1
@izabera
izabera / query
Last active January 2, 2025 12:06
query certain useful terminal attributes
#!/bin/bash
printf %b%.b \
'\e7' 'save cursor position' \
'\e[9999;9999H' 'move to bottom right' \
'\e[6n' 'get cursor position' \
'\e8' 'restore cursor' \
'\e[?u' 'kitty kbd proto' \
'\e[?2026$p' 'synchronised output' \
'\e[m' 'reset colours' \
@izabera
izabera / array
Last active December 15, 2024 08:18
bash arrays
#!/bin/bash
a=({1..1000000})
TIMEFORMAT=%R
for mult in {1..100}; do
printf "%s " "$mult"
RANDOM=0
time for i in {1..100000}; do
: "${a[RANDOM*mult/10]}"
done
@izabera
izabera / maths
Last active January 19, 2025 12:36
#!/bin/bash
# max multiple of pi so our maths always fits in a signed 64 bit int
pi=314159
scale=100000
# bhaskara's formula
# https://en.wikipedia.org/wiki/Bh%C4%81skara_I's_sine_approximation_formula
@izabera
izabera / build.dumb
Created December 4, 2024 08:29
dumb "build system" in 10 lines of posix sh
#!/usr/bin/env dumb
input fentry.cpp count.hpp
output fentry.o
cmd clang++ -std=c++17 -ggdb3 -fPIC -mgeneral-regs-only -c fentry.cpp
input count.cpp count.hpp
output count.o
cmd clang++ -std=c++17 -ggdb3 -fPIC -c count.cpp
#!/usr/bin/env dumb
in: fentry.cpp
out: fentry.o
cmd: clang++ -std=c++23 -ggdb3 -fPIC -mgeneral-regs-only -c fentry.cpp
in: count.cpp
out: count.o
cmd: clang++ -std=c++23 -ggdb3 -fPIC -c count.cpp