Skip to content

Instantly share code, notes, and snippets.

View seansawyer's full-sized avatar

Sean Sawyer seansawyer

View GitHub Profile
@seansawyer
seansawyer / hex_to_bit_varying.sql
Last active February 2, 2017 09:40
PL/pgSQL function to convert a hexadecimal string to a bit varying
DROP FUNCTION IF EXISTS hex_to_bit_varying(hex_string text);
CREATE FUNCTION hex_to_bit_varying(hex_string text) RETURNS bit varying AS $$
DECLARE
bytes bytea = decode(hex_string, 'hex');
n int = length(bytes) * 8 - 1;
bits bit varying := B''::bit varying;
BEGIN
-- RAISE NOTICE 'bytes=%, n=%', bytes, n;
-- RAISE NOTICE 'bits=%', bits;
FOR i IN 0 .. n BY 8 LOOP
@seansawyer
seansawyer / c-tools.md
Last active February 26, 2016 03:27
Various Helpful C Tools

Print all libs loadable at runtime

ldconfig -p
# 208 libs found in cache `/etc/ld.so.cache'
#     libz.so.1 (libc6,x86-64) => /lib64/libz.so.1
#     libz.so (libc6,x86-64) => /usr/lib64/libz.so
# ...

Print all libs loadable at compile time

@seansawyer
seansawyer / comm.txt
Last active February 8, 2016 21:48
Find numbers in two files full of 10 million random positive integers
$ time shuf -i 1-4294967296 -n 10000000 | sort | uniq - sorted1.txt
real 0m21.387s
user 0m22.700s
sys 0m0.820s
$ time shuf -i 1-4294967296 -n 10000000 | sort | uniq - sorted2.txt
real 0m21.230s
user 0m22.417s
sys 0m0.703s
@seansawyer
seansawyer / coolness.sh
Created January 15, 2016 18:50
Cool Shell Things
# Meta Man
man man
# When your last commit has some garbage author info - maybe you committed in a VM or something
git commit --amend --reset-author
# Sudo do the last thing I said
sudo !!
# Remove an item from your history - maybe you typed your password by accident
@seansawyer
seansawyer / .tmux.conf
Created October 5, 2011 20:27
my tmux configuration
# Prefix
set -g prefix C-j
# Copy mode
unbind [
bind Escape copy-mode
# Use Vi mode
setw -g mode-keys vi
# Make mouse useful in copy mode
setw -g mode-mouse on
@seansawyer
seansawyer / gist:983715
Created May 20, 2011 20:19
Extract an OTP application version number from an Erlang .app file
awk "/\\{vsn, \"([0-9]+)\"\\}/" src/synchole.app.src | sed -En "s/[^[:digit:]]*([[:digit:]]+)[^[:digit:]]*/\1/p"
git clone git@github.com:vitrue/synchole.git
cd synchole
rebar clean compile generate
%% extract version number from reltool.config
mv rel/synchole rel/synchole_VSN
scp rel/synchole_VSN deploy@dest:/data/synchole/rel
@seansawyer
seansawyer / gist:88a16373a3d2c2a939b1
Last active September 21, 2015 22:11
Vagrant + Ansible crash course

Install Virtualbox, Vagrant and Ansible.

Create a new directory for your first Vagrant VM. In that directory, create a Vagrantfile using vagrant init, and enable the Ansible provisioner by adding the following.

config.vm.provision "ansible" do |ansible|
  ansible.playbook = "playbook.yml"
end

Now you can create an Ansible playbook named playbook.yml file in the same directory as your Vagrantfile, and vagrant provision will run it. Here's an easy one to start with that will update NSS and add the EPEL and IUS repositories.

@seansawyer
seansawyer / socket_lock.php
Last active August 29, 2015 14:20
Interprocess locking in PHP with Unix domain sockets
<?php
/**
* Try to acquire an arbitrarily-named lock by binding a Unix
* (family AF_UNIX) datagram (type SOCK_DGRAM) socket to an
* abstract address corresponding to the lock name. On success,
* the bound socket resource is returned (or null, on
* failure). You may either call unlock() with that resource to
* release the lock explicitly, or rely on the operating system
* to clean up the socket when the process exits.