Skip to content

Instantly share code, notes, and snippets.

@ingramj
ingramj / test.h
Created Nov 30, 2014
Unit Tests for C, in a single header.
View test.h
#ifndef TEST_H_
#define TEST_H_
/* Unit testing framework.
*
* Copyright (c) 2014, Jim Ingram <ingramj@gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
View gist:2023883
# print-clean.sh
targets=`grep '^.*:' Makefile | cut -d: -f1` # this isn't a general solution
echo rm -f $targets
# Makefile
a:
echo 'contents of a' > $@
clean:
sh print-clean.sh > $@
@ingramj
ingramj / getdelim.c
Created Jul 25, 2011
Implementations of the getdelim() and getline() functions from POSIX 2008, just in case your libc doesn't have them. They should comply with the POSIX standard, but they haven't been thoroughly tested yet.
View getdelim.c
/* Implementations of the getdelim() and getline() functions from POSIX 2008,
just in case your libc doesn't have them.
getdelim() reads from a stream until a specified delimiter is encountered.
getline() reads from a stream until a newline is encountered.
See: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html
NOTE: It is always the caller's responsibility to free the line buffer, even
when an error occurs.
@ingramj
ingramj / error.c
Created Jul 23, 2011
Error reporting functions and macros. Includes goodies like color-coding and time stamps. Originally based on eprintf() from Kernighan and Pike's "The Practice of Programming".
View error.c
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
const char *progname = NULL;
View narepl
#!/bin/sh
# narepl - Not A Read-Eval-Print Loop.
echo "This is not a REPL. Press ctrl-d to compile and run, ctrl-c to exit."
echo -n "> "
while true ; do
echo "#include </dev/tty>" | gcc -x c -ldl - && ./a.out && rm ./a.out
echo -n "> "
done
View stsh.rb
#!/usr/bin/env ruby
require 'readline'
require 'pp'
class Stsh
def initialize
@prompt = "-$ "
@aliases = {}
@running = false
@ingramj
ingramj / brainfuck.rb
Created Feb 25, 2009
A Brainfuck interpreter written in Ruby.
View brainfuck.rb
#!/usr/bin/env ruby
class BrainFuck
def initialize
@ops = create_ops
@tape = Array.new(1024,0)
@tp = 0
@code = []
@cp = 0
end
View tidy.rb
# HTML Tidy filter. This is pretty Unix-specific. It uses the external 'tidy'
# command, which it looks for using 'which', and communicates with using open3.
require 'open3'
module Tidy
# Runs the source through tidy and returns the result. If tidy isn't found,
# just returns the source as is. The source should be string-like.
#
# Takes an optional hash parameter, with the keys :strict and :verbose
View verify-sig.rb
#!/usr/bin/ruby
# Takes a list of files and verifies the gpg signature on each, and makes
# sure that it was signed by an allowed key id
ALLOWED_KEY_IDS = ['C81FD50E']
ARGV.each do |e|
result = `gpg --verify #{e} 2>&1`
# If the the signature isn't verified, print and error and go the next file
unless result.include?('Good signature')
View attachments.rb
#!/usr/bin/ruby
# Save attachments from a saved email.
require 'rubygems'
require 'tmail'
return 1 unless ARGV.size > 0
ARGV.each do |in_file|
mail = TMail::Mail.load(in_file)