Skip to content

Instantly share code, notes, and snippets.

View clausecker's full-sized avatar

Robert Clausecker clausecker

View GitHub Profile
@clausecker
clausecker / url.l
Last active October 2, 2019 23:10
Lexer that detects links in html documents
/* http://stackoverflow.com/a/1732454/417501 */
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MYEOF EOF
#define TOKEN_URL 257
@clausecker
clausecker / locate.sh
Last active April 8, 2021 22:19
locate(1) as a CGI script
#!/bin/sh
PATH="$PATH:/usr/local/bin"
export LC_CTYPE=C
export LC_COLLATE=C
# decode URL encoded $1 and print result to stdout
urldecode() {
gawk "BEGIN { print \"$(echo "$1" | sed -e 's/"/\\"/' -e 's/+/ /g' -e 's/%/\\x/g')\" }" </dev/null
}
/* remove comments from C source files */
#include <stdio.h>
#include <stdlib.h>
/*
* read ahead buffer
*/
static char rabuf[BUFSIZ], *readahead = rabuf;
section bss
BUFSIZ equ 64
iobuf resb BUFSIZ ; where we put the number
section text
; convert 32 bit number in argument to asciz in iobuf
; return pointer to number in ax
global ltoa@4
# binary gcd algorithm in amd64 assembly
# gcd(u, v);
.text
.globl gcd
.type gcd,@function
gcd:
# u is in edi
# v is in esi
# abs routines are not branch free because we need to test
@clausecker
clausecker / read_puzzle.c
Created January 10, 2018 17:49
Fibonacci growth example
static struct puzzle *
read_puzzles(size_t *n_puzzle, FILE *puzzlefile)
{
struct puzzle *puzzles;
size_t cap = 64, n_linebuf;
char *linebuf = NULL;
*n_puzzle = 0;
puzzles = malloc(cap * sizeof *puzzles);
if (puzzles == NULL) {
/*-
* Copyright (c) 2014, 2016 Robert Clausecker
*/
/*
* util.h - various utility functions
*/
#ifndef UTIL_H
#define UTIL_H
@clausecker
clausecker / exp.s
Created July 6, 2017 19:53
Compute exp() in i386 assembly
.text
.globl exp
.type exp,@function
.align 16
exp:
fldt 4(%esp) # x
fldl2e # log2(e) x
fmulp # x' (= log2(e) * x)
fld %st(0) # x' x'
frndint # round(x') x'
/* https://www.reddit.com/r/abstractgames/comments/6dklih/ */
#include <stdio.h>
#include <stdlib.h>
/*
* The game is played on a board like this, every player has three
* pieces named a, b, and c. The player to play first (blue) has
* uppercase pieces, the other player has lowercase pieces. This is
* the initial setup:
*
/*-
* Copyright (c) 2014, 2016 Robert Clausecker
*/
/*
* parse_mode() - parse an octal or symbolic mode string into a mode_t
*/
#define _XOPEN_SOURCE 700
#include <stdlib.h>