Skip to content

Instantly share code, notes, and snippets.

@jehiah
jehiah / strnchr.c
Created March 4, 2011 17:20
strnchr because it doesn't exist for some reason
#include <stdlib.h>
/*
Returns a pointer to the first occurrence of character in the C string str.
The terminating null-character is considered part of the C string. Therefore,
it can also be located to retrieve a pointer to the end of a string.
@param str the string to be searched
@param len the number of characters to search
@param character the character to search for
@jehiah
jehiah / lru_cache.c
Created April 3, 2011 21:35
a LRU cache in C using uthash
#include <string.h>
#include <uthash.h>
// this is an example of how to do a LRU cache in C using uthash
// http://uthash.sourceforge.net/
// by Jehiah Czebotar 2011 - jehiah@gmail.com
// this code is in the public domain http://unlicense.org/
#define MAX_CACHE_SIZE 100000
@jehiah
jehiah / wait_run_in_parallel.sh
Created July 22, 2011 16:05
run bash commands in parallel
# This is a simple way to run multiple things in parallel in a bash
# script without making things too complicated
# it's modeled after (and uses) the bash `wait` command
# TIP: to do more complicated steps in parallel put them in a function
function wait_run_in_parallel()
{
local number_to_run_concurrently=$1
if [ `jobs -np | wc -l` -gt $number_to_run_concurrently ]; then
wait `jobs -np | head -1` # wait for the oldest one to finish
@jehiah
jehiah / bash_read_loop_stdin.sh
Created July 30, 2011 23:23
Bash trick #48 while loop reading input from a file
#!/bin/sh
# this loop uses "read" to parse stdin into variables
# stdin is specified at the ending "done" with < input_file, or
# in this case, an inline block of text.
while read host port; do
echo "host $host port $port";
done << EOF
google.com 80
@jehiah
jehiah / simulate_memcached.py
Created August 20, 2011 01:43
test script to simulate a partially reachable memcached instance
#!/bin/env python
"""
This is a test script to simulate a memcached instance on a server
that has gone south and is accepting connections, but generally not
responding.
The goal of this script is to help test/develop correct client
side settings for timeouts/failure scenarios
@jehiah
jehiah / git-branch-status
Last active March 21, 2024 12:39
show git ahead/behind info for branches
moved to github --> https://github.com/bill-auger/git-branch-status/
@jehiah
jehiah / AppDelegate.h
Created December 5, 2011 04:16
Example for building an OSX app with Chameleon UIKit
#import <Cocoa/Cocoa.h>
#import <UIKit/UIKit.h>
@class ChameleonAppDelegate;
@interface AppDelegate : NSObject <NSApplicationDelegate> {
@private
NSWindow *window;
IBOutlet UIKitView *chameleonNSView;
ChameleonAppDelegate *chameleonApp;
}
@jehiah
jehiah / increment_build_number.sh
Created December 6, 2011 03:37
Auto-increment build number script for Xcode
TARGET="$PROJECT_DIR/project/Info.plist"
echo $TARGET
if [ ! -f "$TARGET" ]; then
echo "missing file $TARGET"
exit 1;
fi
# the perl regex splits out the last part of a build number (ie: 1.1.1) and increments it by one
# if you have a build number that is more than 3 components, add a '\.\d+' into the first part of the regex.
@jehiah
jehiah / strange_bash_feature.sh
Created May 22, 2012 21:17
Weird (annoying) feature of bash
#!/bin/bash
COUNT=1
FILE=$0
# run this script with bash
# i bet you can't predict the outcome
function printsleep {
echo COUNT=$(( COUNT++ ))
echo printsleep >> $FILE
@jehiah
jehiah / test_shell_scripts.sh
Created September 15, 2012 02:14
bash shell script syntax checking
#!/bin/sh
#
# This is a script to run syntax check (via `sh -n $filename`) but it
# supports recursive checking and --quiet
QUIET=0
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in