Skip to content

Instantly share code, notes, and snippets.

# find directories which contain only one entry called "index.html" and delete them
# this doesn't enforce that the found "index.html" is a file rather than a directory, symlink, socket, device node, etc
find . -depth -print0 | \
awk 'BEGIN{RS="\0";ORS="\0";} match($0, /^(.*)[/]([^/]+)$/, A) {prevDir=dir; dir=A[1]; if ((prevDir == $0) && couldDelete) { print($0); } if (prevDir != dir) { couldDelete = 1; } if (A[2] != "index.html") { couldDelete = 0; } }' | \
xargs -0 rm -r
@RichardBarrell
RichardBarrell / size_t_minus_one.c
Last active February 4, 2017 04:14
Demonstrating that converting an int containing -1 to size_t does the right thing even where sizeof(int) != sizeof(size_t)
#include <stdint.h>
#include <stdio.h>
#include <limits.h>
typedef struct rtunion {
char whatever[8];
} rtunion;
int main (int argc, char **argv) {
(void)argc;
import csv
import sys
try:
column_indexes = list(map(int, sys.argv[1:]))
except ValueError:
sys.stderr.write("Usage: python pick_csv.py 0 1 2...\n")
sys.exit(1)
if len(column_indexes) == 0:
from StringIO import StringIO
from hypothesis import given
from hypothesis.strategies import lists, binary
def to_lines(f, _block_size=32<<10):
"""file.__iter__ but in python and ought to be slower
>>> list(to_lines(StringIO("foo\\nbar\\nbaz\\n"), _block_size=2))
['foo\\n', 'bar\\n', 'baz\\n']
@RichardBarrell
RichardBarrell / Vagrantfile
Created January 9, 2014 15:00
bog standard Vagrantfile for working with Lucid
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "lucid64"
config.vm.box_url = "http://files.vagrantup.com/lucid64.box"
config.vm.hostname = "puppet"
config.vm.network :private_network, :ip => "192.168.50.100"
config.vm.provision :shell, :path => bootstrap.sh
end
import inspect
def marmoset_patch(old, new, extra_globals={}):
g = old.func_globals
g.update(extra_globals)
c = inspect.getsource(new)
exec c in g
old.func_code = g[new.__name__].func_code
# Incoming, allow TCP 22, 80, 443 and all ICMP except redirect.
# Outgoing, allow everything.
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
iptables -F
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
{-# LANGUAGE BangPatterns #-}
module Collatz where
-- thing I want to know: what's the smallest (n) such that a naive C
-- implementation of "iterate the Collatz function until I get to 1" with
-- fixed-size integers will encounter an integer overflow
-- the below is terrible and contains multiple implementations because I kept
-- trying to make it go faster.
import Data.Word
@RichardBarrell
RichardBarrell / perf_annotate_output.txt
Created April 4, 2013 22:07
Interesting output from "perf annotate" - it seems that whenever I use a synchronising instruction, "perf record" accidentally attributes the sync instruction's ticks to the instruction immediately *after* the lock or mfence.
Percent | Source code & Disassembly of rev26_2
------------------------------------------------
:
:
:
: Disassembly of section .text:
:
: 0000000000400760 <paws>:
: void paws() {
: #ifdef USE_A_POSIX_BARRIER
@RichardBarrell
RichardBarrell / client-connect.c
Created December 18, 2011 05:05
My (mildly-embarrassing) openvpn config
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
int run(char **argv, char **envp) {
pid_t chipr = fork();
if (chipr == 0) {