Skip to content

Instantly share code, notes, and snippets.

View jmatth11's full-sized avatar

Joshua Matthews jmatth11

  • United States
View GitHub Profile
@jmatth11
jmatth11 / name_params.c
Created August 14, 2025 17:39
named parameters in C
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *name;
size_t len;
int value;
} option_t;
void display_cmd_fn(option_t opt);
@jmatth11
jmatth11 / compiler_defer.c
Last active August 14, 2025 17:42
Utilizing Compiler supported cleanup attribute as Defer in C.
#include <stdio.h>
#include <string.h>
#define DEFER(func) __attribute__((__cleanup__(func)))
static void free_char(char **p) {
if (p == NULL) return;
if (*p == NULL) return;
free(*p);
*p = NULL;
@jmatth11
jmatth11 / defer
Last active December 2, 2022 18:16
simple defer in C
#define macro_var(name) name##__LINE__
#define defer(start, end) for ( \
int macro_var(_i_) = (start, 0); \
!macro_var(_i_); \
(macro_var(_i_) += 1), end) \
/**
Example usage:
@jmatth11
jmatth11 / git_show_bash.sh
Last active February 7, 2018 04:04
convenient git show in bash
# allows user to git show by the number the commit appears in git log
# 1 is the first commit, 2 is the second, etc...
git_show() {
re='^[0-9]+$'
if ! [[ $1 =~ $re ]]; then
return
fi
sha="$(git log -$1 --format="%H")"
shaSplit=(${sha//\n/ })
git show ${shaSplit[$(($1 - 1))]}