Skip to content

Instantly share code, notes, and snippets.

static void __debug(const char* fname, const char* format, ...)
{
fprintf(stderr, "DEBUG: %s", fname);
va_list args;
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
}
#define debug(format, ...) __debug(__func__, format, __VA_ARGS__)
/*
* ascii whitespace:
* U+0009
* U+000A
* U+000C
* U+000D
* U+0020
*/
/*
* code point, four-to-six hex chars
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
/*
* memory allocation sub routine that DOES NOT fail
*/
void* tryAllocate()
{
void* buffer;
@rmccullagh
rmccullagh / http.c
Created June 5, 2015 06:26
Hello HTTP server, that only implements GET|HEAD requests
/*
* Copyright (c) 2015 Ryan McCullagh <me@ryanmccullagh.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
<?php
$str = "Sending: 6011 1234 4321 1234:12/2016:543.21:John Doe
Received: Mon Apr 27 18:07:22 CDT 2015: Discover: 6011 1234 4321 1234 is valid: authorization code: 472080 for $543.21";
$a = preg_split("/\n/", $str);
array_shift($a);
$c = preg_match_all('/authorization\scode:\s[0-9]+/', $a[0], $matches);
$d = preg_split('/\n/', $matches[0][0]);
$e = explode(" ", $d[0]);
var_dump($e[2]);
function isWebScaleCapable() {
return true;
}
if(isWebScaleCapable()) {
INIT_WEB_SCALE();
}
static void read_file(void)
{
FILE* fp = fopen("a.dat", "rb");
size_t bread = 0;
int data;
if(fp == NULL) {
fprintf(stderr, "Read error\n");
exit(1);
}
#include <stdlib.h>
#include <stdio.h>
int main()
{
int r[10];
for(size_t i = 0; i < 10; i++) {
r[i] = i + 1;
}
@rmccullagh
rmccullagh / popcount.c
Created April 4, 2015 02:12
is_power_of_two
#include <stdbool.h>
#include <stdio.h>
bool is_power_of_two(size_t);
int my_pop_count(size_t);
int main()
{
size_t n = 8;
@rmccullagh
rmccullagh / my_pop_count.c
Created April 3, 2015 23:46
Hamming Weight
/*
* return the Hamming Weight
*/
int my_pop_count(size_t x)
{
unsigned setBits = 0;
do {
int rem = x % 2;
if(rem == 1) {
setBits++;