Skip to content

Instantly share code, notes, and snippets.

View willeccles's full-sized avatar
🖤
probably out driving

Will Eccles willeccles

🖤
probably out driving
View GitHub Profile
@willeccles
willeccles / hex2float.c
Last active February 22, 2021 18:35
convert hex to float, optionally swapping byte order
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <ctype.h>
#include <stdbool.h>
#define SWAP(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
@willeccles
willeccles / buf.hpp
Last active November 18, 2020 15:05
C-style buffer wrapper class for C++
#ifndef CXX_BUFFER_HPP_
#define CXX_BUFFER_HPP_
#include <functional>
#include <cstdlib>
#include <cstring>
#include <system_error>
#include <stdexcept>
#include <initializer_list>
@willeccles
willeccles / shellandc.c
Last active February 24, 2023 01:52
A C program which is also a valid bash script, and vice versa.
#include <stdio.h>
#define $x ,x
#define $i ,i
#define eval int
#define read scanf
#define $scanfpat "%d", &
#define $(x) ,(x)
#define $open (
#define $close )
#define do {
@willeccles
willeccles / gpioutil.c
Last active June 19, 2020 14:31
A simple command-line utility for managing GPIO devices
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
static enum MODE {
INFO,
EXPORT,
@willeccles
willeccles / sockets.h
Last active February 6, 2024 22:52
Cross-platform socket header for both POSIX sockets and Winsock
/*
* File: sockets.h
* Author: Will Eccles
* Date: 2020-03-12
*
* 1. Description
*
* This header attempts to make it easy to use sockets across platforms,
* for both Windows and POSIX systems. While Winsock is often somewhat
* compatible with Berkeley sockets, it is not strictly compatible,
@willeccles
willeccles / cp.c
Last active February 18, 2020 23:52
recursive file copying function in C
// note there is no check for excessive recursion so if something has already been copied it will be tried again
int cpfile(const char* src, const char* dst) {
char* b = basename(src);
if (b != NULL && (0 == strcmp(b, ".") || 0 == strcmp(b, ".."))) {
return 0;
}
struct stat srcstat, dststat;
bool dstexist = false;
int s = 0;
@willeccles
willeccles / gpiodetails.sh
Created January 3, 2020 13:19
A script to get the details of an exported GPIO device.
#! /bin/bash -e
if [[ $# < 2 ]]; then
echo "Example usage: $0 1 27"
echo "This would get the details of GPIO1_27 if it's exported."
exit 1
fi
DEVNUM=$(( 32 * $1 + $2 ))
@willeccles
willeccles / listfonts.sh
Last active January 2, 2020 22:50
Take screenshots of all fonts in a linux console.
#! /bin/bash -e
if ! command -v fbgrab &>/dev/null; then
echo "Please install fbgrab before running this."
exit 1
fi
fontpath="${FONTPATH:-/usr/share/consolefonts}"
cd "$fontpath"
@willeccles
willeccles / build-linaro-macos.sh
Last active March 4, 2024 14:14
Build the Linaro arm-linux-gnueaibhf-gcc toolchain on macOS.
#! /bin/bash
# source for most of this information: https://stackoverflow.com/a/58852167/2712525
set -e
error() {
echo "failed!"
exit 1
}
@willeccles
willeccles / conway.c
Last active December 4, 2019 17:36
Conway's Game of Life in C
/*
* Input is from stdin. The first line should be the width of the grid.
* The second line should be the height.
* The third line should be the length of a generation in microseconds.
* The rest of the file is assumed to be the grid itself
* Anything out of bounds will be cut off.
* A space is a "dead" cell; anything else is assumed to be a "living" cell.
*
* This implementation doesn't allow cells to wrap around. Edges are solid
* and block cells.