Skip to content

Instantly share code, notes, and snippets.

View kurahaupo's full-sized avatar
🏠
Working from home

Martin Kealey kurahaupo

🏠
Working from home
  • Brisbane, Australia
  • 17:52 (UTC +10:00)
View GitHub Profile
@kurahaupo
kurahaupo / sn_make_path.c
Created June 7, 2018 06:42
demo of string handling
/* This functions takes a path to a directory (C:/blah) and a file name (foobar.txt) then returns the full path of the file (C:/blah/foobar.txt) */
size_t sn_make_path(char *buffer, size_t bsize, const char *dir_path, const char *file_name, char dir_sep) {
size_t dir_len = strlen(dir_path);
size_t file_len = strlen(file_name);
int add_slash = dir_len > 0 && dir_path[dir_len - 1] != dir_sep;
size_t file_path_len = dir_len + file_len + add_slash;
if (bsize > 0) {
@kurahaupo
kurahaupo / shorten-symlinks
Created January 7, 2014 23:24
Convert absolute symbolic links (those whose target starts with /) to be relative (to their containing directory). Written for Linux, but will work anywhere that has `readlink -m` and `ln -T` (the GNU versions do).
#define BUfSIZ*pick a number
, add #includes, and ADD ERROR HANDLING */
int main(){
char b[N]="/tmp/.spongeXXXXXX";
char*t=mktemp(b);
int f=open(t,O_RDWR|O_CREAT|O_EXCL,0);
unlink(t);
ssize_t n;
while((n=read(0,b,N))>0)
#!/bin/bash
shopt -s globstar
sdir=$HOME/Documents/website
tdir=$HOME/tmp/transcoded-website
scode=WINDOWS-1252
tcode=UTF-8
mkdir -vp "$tdir"
@kurahaupo
kurahaupo / xterm-colourtest
Created January 10, 2016 07:29
Xterm Colour test pattern
#!/usr/bin/perl
use 5.008;
use strict;
use warnings;
use Getopt::Long qw(:config bundling);
use POSIX qw( floor );
use utf8;
@kurahaupo
kurahaupo / wideascii
Created January 3, 2014 00:07
Convert normal ascii to equivalent double-width unicode characters
#!/usr/bin/perl
use utf8;
binmode STDOUT, ":utf8";
while (<>) {
s/ / /g;
s/\t/\t\t/g;
s/[!-~]/chr(ord($&)+0xff00-0x20)/xge;
print $_;
@kurahaupo
kurahaupo / siginfo
Last active December 29, 2015 12:19
Show signal names from Sig* lines in Linux's /proc/$PID/status
#!/bin/bash
pid=${1:-$PPID}
signals="$(trap -l)"
declare -A SIG
for s in ${signals//') '/=}
do
i=${s%%=*}
@kurahaupo
kurahaupo / readkey
Last active December 28, 2015 10:59
How to read ANSI keyboard sequences in Bash
readkey() {
local char
IFS= \
read -rs -d '' -N1 key || return $? # use -n1 on older versions
while
case "$key" in
('') key=$'\n' ; break ;; # compensate for bug
( $'\e[M'??? ) break ;; # mouse report
( $'\e' | $'\e[' | $'\e['[?O] | $'\e['*[\;0-9] | $'\e[M'* | $'\eO' ) ;;
( [$'\xc0'-\$'xfe'] \
@kurahaupo
kurahaupo / chresolvconf
Created January 14, 2015 02:00
Manually change /etc/resolv.conf but protect it from meddling by NetworkManager et al
#!/bin/bash
# Update /etc/resolv.conf but keep it unwritable, even by root-owned processes.
# Useful to stop WiFi and/or DHCP from messing with it, and quite essential when
# there are multiple managers for different interface types.
#
# Because symlinks don't work, instead we bind-mount an appropriate file onto
# /etc/resolv.conf, chosen from those in ~/.resolvconf.d/.
#
# Since BIND is running locally, almost all the time that will work, so it's the
@kurahaupo
kurahaupo / twelve-days-of-christmas.bash
Last active August 29, 2015 14:07
twelve days of christmas, in bash
#!/path/to/your/bash
dox=( [1]='a partridge in a pair tree'
'2 turtle doves, and'
'3 french hens'
'4 calling birds'
$'5 gold rings\n ...'
'6 geese a-laying'
'7 swans a-swimming'
'8 maids a-milking'