Skip to content

Instantly share code, notes, and snippets.

@skeeto
skeeto / find.c
Created Aug 1, 2022
Finder over a file descriptor
View find.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
struct finder {
long long lineno;
unsigned char *needle;
size_t nlen, noff;
size_t hlen, hoff;
int fd;
View khashv-benchmark.c
#include <stdio.h>
#include <stdint.h>
#include "khashv.h"
static uint64_t dumbhash(uint64_t s[2])
{
uint64_t x = s[0];
x += 0x3243f6a8885a308d;
x *= 1111111111111111111;
x += s[1];
@skeeto
skeeto / example.c
Last active Aug 4, 2022
Hash set demonstration
View example.c
// cc -g -maes -o example example.c
// https://old.reddit.com/r/C_Programming/comments/w71gmw
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#if __AES__ || (_MSC_VER && _M_AMD64)
# include <smmintrin.h>
# include <wmmintrin.h>
#endif
View extract.py
import sys
import pprint
d = {}
seen = set()
keys = []
icon = None
for line in sys.stdin:
off = line.find(' icon =')
@skeeto
skeeto / splice_example.c
Created Jul 17, 2022
Demonstrates using splice(2) as a pipeline probe
View splice_example.c
// Demonstrates using splice(2) as a pipeline probe
// Ref: https://old.reddit.com/r/C_Programming/comments/w0xg5l
// This is free and unencumbered software released into the public domain.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
@skeeto
skeeto / race.c
Created Jul 7, 2022
Horse racing board game (Monte Carlo method analysis)
View race.c
// Horse racing board game (Monte Carlo method analysis)
// $ cc -O3 -fopenmp -o race race.c
// $ ./race
// Ref: https://possiblywrong.wordpress.com/2022/07/07/analysis-of-horse-racing-board-game/
// This is free and unencumbered software released into the public domain.
#include <stdio.h>
#include <stdint.h>
#define BAR \
"########################################################################"
@skeeto
skeeto / generate.sh
Created Jun 30, 2022
Newsraft Makefile generator
View generate.sh
#!/bin/sh
set -e
cat <<EOF
.POSIX:
VERSION = 0.2
CC = cc
CFLAGS = -std=gnu18 -Wall -Wextra -O3
LDFLAGS = -s
LDLIBS = -lncursesw -lsqlite3 -lcurl -lexpat -lgumbo -lyajl
@skeeto
skeeto / dungeon.c
Last active Jun 21, 2022
Dungeon generator
View dungeon.c
// Dungeon generator
// Ref: https://old.reddit.com/r/C_Programming/comments/vfzqld
#include <stdio.h>
#include <time.h>
#define N 128
static void put(int c)
{
if (c < 0x80) {
@skeeto
skeeto / example.c
Last active Jun 6, 2022
OpenGL mini framebuffer display example
View example.c
// cc example.c -lX11 -lGL
// This is free and unencumbered software released into the public domain.
#define GLX_GLXEXT_PROTOTYPES
#include <stdio.h>
#include <GL/glx.h>
#include <X11/Xlib.h>
#define WIDTH 256
#define HEIGHT 128
#define SCALE 4
View idstore.c
// ID store
// Ref: https://old.reddit.com/r/C_Programming/comments/upv7lx
// This is free and unencumbered software released into the public domain.
#include <stdlib.h>
#define IDSTORE_INIT {0, 0, 0, 0}
struct idstore {
size_t len, cap;
long *heap;
long counter;