Skip to content

Instantly share code, notes, and snippets.

@ilansmith
ilansmith / iface.c
Created December 29, 2022 14:20
List network devices
#include <net/if.h>
#include <stdio.h>
int main(void)
{
struct if_nameindex *if_nidxs, *intf;
if (!(if_nidxs = if_nameindex()))
goto exit;
#include <stdio.h>
#if defined(DEBUG)
#define PRINT(_fmt_, ...) printf(_fmt_, __VA_ARGS__);
#else
#define PRINT(_fmt_, ...)
#endif
/* works in the range: [0x1, 0x7FFF FFFF FFFF FFFF] */
static size_t upper_power_of_two(size_t v)
@ilansmith
ilansmith / count_bits.c
Last active January 6, 2019 13:49
Efficient count the number of bits in a 32bit bitmask
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define DO_PRINT 0
#define TEST_BITS(_n_, _do_print_) do { \
unsigned calculated = number_of_set_bits(_n_); \
unsigned actual = number_of_set_bits_naive(_n_); \
if (_do_print_) \
@ilansmith
ilansmith / my_getopt.c
Last active October 18, 2018 23:21
getopt() for optional argument
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#define ALIGN_OPTIONAL_ARGUMENT(_argc, _argv, _optstring, _optarg, _optind) \
do { \
if (!(_optarg) && (_optind) < (_argc) && \
(((_argv)[(_optind)][0] != '-') || \
!strchr(_optstring, (_argv)[(_optind)][1]))) { \
(_optarg) = (_argv)[(_optind)++]; \
@ilansmith
ilansmith / time_limit.bat
Last active June 15, 2019 04:35
Set Windows 10 local user time restrictions
@echo off
set MORNING=6:00am-8:00am
set SCHOOL=8:00am-1:00pm
set AFTERNOON=1:00pm-6:00pm
set EVENING=6:00pm-12:00am
set NIGHT=12:00am-6:00am
set LIMIT=SU-SA,%AFTERNOON%;F,%EVENING%;SA,%MORNING%,%SCHOOL%
rem echo MORNING:%MORNING%
@ilansmith
ilansmith / dirent.c
Created September 26, 2017 06:50
An example of how to manipulate a directory entry
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
static int path_scan(char *path)
{
DIR *d;
struct dirent *ent;
@ilansmith
ilansmith / print_in_place.c
Created August 19, 2017 22:13
Print "moving" objects (advancing timeline, rotating star, etc...)
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdarg.h>
#define CURSOR_DISSABLE "\E[?25l"
#define CURSOR_ENABLE "\E[?25h"
#define ARRAY_SZ(ARR) (sizeof(ARR) / sizeof(ARR[0]))
@ilansmith
ilansmith / colours.c
Created August 19, 2017 11:24
Shell Colours
#include <stdio.h>
#define CLEAR "\033[00;00;00m"
static void p_colour(int c1, int c2)
{
char col[10];
sprintf(col, "\033[%d;%dm", c1, c2);
printf("\"\\033[%d;%dm\" = %scolour%s\n", c1, c2, col, CLEAR);
@ilansmith
ilansmith / glibc_version.c
Last active October 30, 2016 12:56
Get glibc version
#include <stdio.h>
#include <gnu/libc-version.h>
int main (void)
{
puts(gnu_get_libc_version());
return 0;
}
@ilansmith
ilansmith / commit-msg
Created October 9, 2016 21:45
A git hook that automatically adds a change-id to the commit message.
#!/bin/sh
# From Gerrit Code Review 2.1.2-rc2-33-g7e30c72
#
# Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
#
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at