Skip to content

Instantly share code, notes, and snippets.

View mlabbe's full-sized avatar

Michael Labbe mlabbe

View GitHub Profile
@mlabbe
mlabbe / log.h
Created December 28, 2021 19:49
Barebones code for a structured logger in C
#pragma once
// config
#define LOG_USE_STDIO 1
#define LOG_USE_FILE 0
#define LOG_USE_STRUCTURED 1
#define LOG_LEVEL_TRACE 0
#define LOG_LEVEL_WARN 1
#define LOG_LEVEL_ERROR 2
@mlabbe
mlabbe / log2json.py
Created December 27, 2021 20:58
Process structured logs to JSON
#!/usr/bin/env python3
# parse log.h's structured format:
#
# - each line ends with sequence 0x20 0x1a 0x0a
# - must parse through possible ascii escape sequences
# - 0x1b (esc) until 'm'
# - structure starts with line containing 3 upcase alpha chars
# - message ends with 0x1a 0x1a 0x0a
# - each line after the first is 0x09
@mlabbe
mlabbe / windowsupdate.conf
Created May 12, 2021 13:05
Block windows update dns records with dnsmasq
# put in /etc/dnsmask.d and restart the daemon
# then run ipconfig /flushdns
#
# run at your own risk
address=/windowsupdate.microsoft.com/0.0.0.0
address=/update.microsoft.com/0.0.0.0
address=/windowsupdate.com/0.0.0.0
address=/test.stats.update.microsoft.com/0.0.0.0
address=/ntservicepack.microsoft.com/0.0.0.0
address=/download.microsoft.com/0.0.0.0
@mlabbe
mlabbe / build.sh
Created December 11, 2020 23:08
MacOS M1 cross compile and build a fat binary with aarch64/arm64 and x86_64/amd64
#!/bin/bash
clang hello.c -target arm64-apple-darwin20.1.0 -o hello.arm64
echo -n "hello.arm64 is architecture " && lipo -archs ./hello.arm64
clang hello.c -target x86_64-apple-darwin-macho -o hello.x86_64
echo -n "hello.x86_64 is architecture " && lipo -archs ./hello.x86_64
lipo hello.arm64 hello.x86_64 -create -output hello
echo -n "final output binary has archs " && lipo -archs ./hello
@mlabbe
mlabbe / ifdef-disable.el
Created December 3, 2020 17:31
yasnippet disable a region with #if 0
# -*- mode: snippet -*-
# name: ifdef-disable
# key: ifdef-disable
# binding: M-#
# expand-env: ((yas-wrap-around-region nil))
# --
#if 0
`yas-selected-text`#else
$0
#endif
@mlabbe
mlabbe / i3_config
Created April 30, 2020 18:12
Run Emacsclient on i3wm scratchpad workspace
# launch scratchpad frame in existing emacs daemon
exec --no-startup-id /usr/local/bin/pad.sh
# when scratchpad frame is launched, move it to the scratchpad workspace
for_window [title="_emacs scratchpad_" class="Emacs"] move scratchpad
@mlabbe
mlabbe / build.sh
Last active March 6, 2020 04:47
Redefine a symbol in a compiled elf object file
#!/bin/sh -x
rm -f *.o redefine
CC=clang-9
LD=$CC
CFLAGS="-fno-inline-functions -O0"
$CC -c redefine.c -o redefine.o $CFLAGS
$CC -c redefine_b.c -o redefine_b.o $CFLAGS
@mlabbe
mlabbe / build.sh
Last active March 6, 2020 04:54
Override a function at link time by weakening it with objcopy
#!/bin/sh -x
rm -f *.o weak
CC=clang-9
LD=$CC
CFLAGS="-fno-inline-functions -O0"
$CC -c weak.c -o weak.o $CFLAGS
$CC -c weak_b.c -o weak_b.o $CFLAGS
@mlabbe
mlabbe / build.sh
Last active May 18, 2024 07:10
Use clang linker --wrap to wrap a function in the same compilation unit
#!/bin/sh -x
CC=clang-9
rm -f *.o wrap nowrap
# build without wrapping
$CC -c wrap.c -o wrap.o
$CC -c wrap_b.c -o wrap_b.o
$CC wrap.o wrap_b.o -o nowrap
@mlabbe
mlabbe / ftg_core.h
Created December 7, 2019 07:38
Parse Terminfo binary files without dependency libraries
/* ftg_core.h - v0.5 - Frogtoss Toolbox. Public domain-like license below.
ftg libraries are copyright (C) 2015 Frogtoss Games, Inc.
http://github.com/mlabbe/ftg_toolbox
ftg header files are single file header files intended to be useful
in C/C++. ftg_core contains generally useful functions
Special thanks to STB for the inspiration.