Skip to content

Instantly share code, notes, and snippets.

@rlcamp
rlcamp / pending.sh
Last active November 3, 2023 17:03
list git repos with uncommitted or unpushed changes
#!/bin/sh
set -e
for repo in $(find . -mindepth 2 -type d -name '.git' | sed -e 's/\/.git//'); do
(cd $repo &&
git diff --exit-code HEAD &&
git log --decorate --oneline | head -n1 | grep origin
) 1>/dev/null || printf '"%s" has uncommitted or unpushed changes\n' $(basename $repo) >&2
done
@rlcamp
rlcamp / plot_image_vs_time.py
Last active July 27, 2023 01:57
using matplotlib to plot data vs unix time with yaxis_date is ungoogleable
#!/usr/bin/env python3
import sys
import os
def plot_image_vs_time(data, timestamps, x0, dx, xlabel, title=None):
import datetime
try:
import numpy
#!/bin/bash
# campbell, 2022-2023
# Given a mesh of already-mutually-known peers, initially not fully connected with non-stale
# handshakes, this script will propagate knowledge of current endpoints to peers which need
# them, such that in the steady state, the network is fully-connected and will self-heal if
# an endpoint moves around.
# This conservative implementation requires that a peer already be known to wireguard in
# order to have its endpoint updated. No provision is made for having peers forward traffic
@rlcamp
rlcamp / conflicts.sh
Last active November 4, 2023 04:40
find files in adjacent directories with the same filenames and different contents
#!/bin/sh
find . -type f \( -name '*.c' -o -name '*.h' -o -name '*.py' \) \
-not -name 'main*' \
-not -name '__init__.py' \
-not -name 'version.h' \
-not -name 'ViewController.h' \
-not -name 'AppDelegate.h' \
-not -name 'test.c' \
-not -name 'config.h' -exec cksum {} + |
@rlcamp
rlcamp / popen_generator.c
Last active October 18, 2023 04:57
demo of a three thread generator-style pipeline where the middle thread is a child process, with which communication happens via its stdin and stdout
/* demo of a three thread generator-style pipeline where the middle thread is a child process, with
which communication happens via its stdin and stdout */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
@rlcamp
rlcamp / rpiblink.c
Last active November 7, 2022 02:15
@rlcamp
rlcamp / flock.pl
Last active October 30, 2023 15:54
calls flock(2) for various use cases, behaves like unix flock, python and perl implementations
#!/usr/bin/perl -w
use strict;
use Fcntl qw(:DEFAULT :flock);
if (!@ARGV) { die "Usage: $0 fd | { lockfile prog args }\n"; }
# get a filehandle to either the supplied filename or fd, depending on whether it is an integer
open(my $fh, $ARGV[0] =~ /^\d+$/ ? ">&=" : ">", $ARGV[0]) or die "cannot open: $!\n";
# flock the filehandle
@rlcamp
rlcamp / decrypt_with_privkey.sh
Last active March 11, 2023 20:35
encryption and decryption using an openssh-formatted rsa public-private key pair
#!/bin/bash
# TODO: this leaks the session key to arguments visible to ps while decrypting
set -euo pipefail
# if an argument was provided, use it as the path to the rsa private key, otherwise assume openssh
keypath=${1:-"$HOME/.ssh/id_rsa"}
# deal with converting openssh special file format to something openssl understands
TMPFILE=$(mktemp)
cp -p "$keypath" $TMPFILE
/* standalone test case for https://github.com/kaniini/libucontext/issues/2
# compile libucontext:
git clone --depth 1 https://github.com/kaniini/libucontext.git
cd libucontext
make FREESTANDING=yes libucontext.a
# and then compile this test case:
cc -Wall -Wextra -Wshadow -Os -o libucontext_fp_test libucontext_fp_test.c -I${HOME}/Downloads/libucontext/include/ ${HOME}/Downloads/libucontext/libucontext.a
*/
@rlcamp
rlcamp / inline_c.sh
Last active October 11, 2022 22:54
for when you really just need to run a bit of C logic from a bash script
#!/bin/bash
TMPFILE=$(mktemp)
cc -Wall -Wextra -Wshadow -Os -lm -o ${TMPFILE} -x c <(tail -n+$(awk '/^exit$/ { print NR + 1 }' $0) $0) && chmod +x ${TMPFILE} && ${TMPFILE} "$@"
rm ${TMPFILE}
exit
#include <stdio.h>
#include <unistd.h>
int main(const int argc, const char ** const argv) {