Skip to content

Instantly share code, notes, and snippets.

@1995eaton
1995eaton / readin.c
Created November 8, 2014 23:29
C stdin reader example with dynamic memory allocation
#include <stdio.h>
#include <stdlib.h>
static char *
read_stdin (void)
{
size_t cap = 4096, /* Initial capacity for the char buffer */
len = 0; /* Current offset of the buffer */
char *buffer = malloc(cap * sizeof (char));
int c;
@1995eaton
1995eaton / marshal.js
Created November 2, 2014 23:45
A Marshal-like Node.js script for data serialization
var fs = require('fs');
function BufferWriter(initialSize) {
initialSize = initialSize || 1024;
this.data = new Buffer(initialSize);
this.clear(this.data);
this.capacity = initialSize;
this.offset = 0;
}
@1995eaton
1995eaton / client.py
Created October 13, 2014 17:47
Simple Python Socket Messenger
#!/usr/bin/python2
from threading import Thread
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 9999,))
class ReplyHandler(Thread):
@1995eaton
1995eaton / shellshock.c
Last active August 29, 2015 14:07
Shellshock
/* Tested on Linux running apache 2.4.10-1 CGI module
and bash-4.3.018-1 */
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#define HOST "127.0.0.1"
@1995eaton
1995eaton / cpuperc.c
Created October 7, 2014 13:10
CPU usage calculator
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
typedef struct {
int64_t user, nice, system,
idle, iowait, irq,
softirq, steal, guest,
@1995eaton
1995eaton / twister.cc
Created October 5, 2014 23:29
Mersenne Twister in C++
#include <iostream>
#include <fstream>
class Twister {
private:
uint32_t data[624],
index = 0;
uint32_t read_device() {
std::ifstream ifs("/dev/urandom");
uint32_t r(0);
@1995eaton
1995eaton / sfinae.cc
Created September 23, 2014 23:47
SFINAE typedef test
// SFINAE structure that tests if a given
// type contains the iterator typedef
template<typename T>
struct has_iterator {
template<class C>
static char test(typename C::iterator*);
static long test(...);
enum {
value = 1 == sizeof test<T>(0)
@1995eaton
1995eaton / snake.html
Created September 16, 2014 18:00
Classic snake game in JavaScript + HTML5 Canvas
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="author" content="">
<style>
#canvas {
border: 1px solid;
@1995eaton
1995eaton / range.cc
Created September 12, 2014 10:59
Range Iterator C++
#include <iostream>
#include <stdexcept>
struct RangeIterator {
int64_t i, s = 1;
const RangeIterator &operator++() {
i += s;
return *this;
}
const bool operator!=(const RangeIterator &it) {
@1995eaton
1995eaton / isprime.cc
Created September 12, 2014 03:39
Is prime
#include <iostream>
#include <cmath>
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
bool is_prime(T n) {
if (n < 2) {
return false;
}
for (T i = 2, sq = sqrt(n) + 1; i != sq; i++) {
if (n % i == 0) {