Skip to content

Instantly share code, notes, and snippets.

@skeeto
skeeto / queue.c
Created April 12, 2025 14:21
Single consumer, single producer circular buffer using Win32 SRW
// Single consumer, single producer circular buffer using Win32 SRW
// Ref: https://github.com/vibhav950/cbuf
// Ref: https://old.reddit.com/r/C_Programming/comments/1jwnlrf
// Ref: https://nullprogram.com/blog/2024/10/03/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
typedef uint8_t u8;
typedef int32_t b32;
@skeeto
skeeto / path_open.txt
Created April 5, 2025 14:57
Question via Illume
!profile claude
!user
Please give me an example of using WASI `path_open` to open a file for writing. In particular, I don't know where to get the `dirfd` parameter.
!assistant
# Example of using WASI `path_open` to open a file for writing
The WASI `path_open` function requires a directory file descriptor (`dirfd`) as its first parameter because WASI uses a capability-based security model. Here's how to use it:
@skeeto
skeeto / example.c
Last active March 17, 2025 22:34
Robust Lock?
// Robust Lock: if a "thread" exits while holding a lock, it is unlocked
// linux $ cc -pthread example.c
// w64dk $ cc example.c -lntdll
// Ref: https://old.reddit.com/r/C_Programming/comments/1jd82ux
// Ref: https://github.com/cozis/timestamp_lock
// This is free and unencumbered software released into the public domain.
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
// https://old.reddit.com/r/C_Programming/comments/1iljkn2
// https://github.com/jamesnolanverran/dmap
#include "dmap.c"
#include <string.h>
#define KEYLEN 8
#define RUNS 10
#define KEYS 1000000
static void encode(char *key, int x)
@skeeto
skeeto / solve.c
Last active January 21, 2025 01:57
// 1. Input:
// A number N, representing the size of the square board grid. N rows of
// N characters each, where each character is either R (red) or A (blue).
//
// 2. Objective:
// Rearrange the rows of the board to maximize the area of the largest
// contiguous rectangular zone for each color (R for The Famous, A for
// The Warriors).
//
// Determine the winning team based on the larger rectangular area of
@skeeto
skeeto / example.c
Last active April 7, 2025 16:13
Code for "Examples of quick hash tables and dynamic arrays in C"
// Examples of quick hash tables and dynamic arrays in C
// https://nullprogram.com/blog/2025/01/19/
// This is free and unencumbered software released into the public domain.
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Question 2: Steganography using 4-bit LSB algorithm
Scope: File Processing, Pointers, Dynamic Memory Allocation (No arrays)
File to use: stego_lsb.c
Preliminary:
This question should be solved using dynamic memory allocation along with
pointers. Any submitted work which includes an array will be given
automatically a zero mark. To view pgm files, you can use the “Irfan View”
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define new(a, n, t) (t *)alloc(a, n, sizeof(t), _Alignof(t))
#define S(s) (Str){(uint8_t *)s, sizeof(s)-1}
@skeeto
skeeto / demo.cpp
Created October 3, 2024 22:27
Slim Reader/Writer Condition Variable Demo / Test
// Slim Reader/Writer Condition Variable Demo / Test
// $ cc -nostartfiles -o main.exe demo.cpp
// $ cl /GS- demo.cpp /link /subsystem:console kernel32.lib
// This is free and unencumbered software released into the public domain.
#define assert(c) while (!(c)) *(volatile i32 *)-4 = 0
using b32 = signed;
using i32 = signed;
using uz = decltype(sizeof(0));
@skeeto
skeeto / testenv.cpp
Created September 30, 2024 23:22
Quick test of environment variable passing
// Quick test of environment variable passing
// $ cc -nostartfiles -o testenv testenv.cpp
// $ cl testenv.cpp /link /subsystem:console kernel32.lib
//
// Spawns a copy of itself with a custom-built environment block, and
// the spawned child examines/probes that block.
//
// This is free and unencumbered software released into the public domain.
typedef int I;