Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
nixpulvis / ping.rs
Created November 18, 2015 21:38
A simple Ping implementation in Rust.
#![feature(ip_addr, raw)]
extern crate pnet;
use std::net::{IpAddr, Ipv4Addr};
use pnet::transport::TransportChannelType::Layer4;
use pnet::transport::TransportProtocol::Ipv4;
use pnet::transport::transport_channel;
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::packet::Packet;
# Commands to be executed directly by this shell.
BUILTINS = {}
# The builtin `cd` for changing the shell's working directory.
BUILTINS['cd'] = lambda do |args|
# Change to the home directory by default.
args << ENV['HOME'] if args.empty?
# Read the destination path, doing very basic path expansion.
dest = args.pop.gsub(/~/, ENV['HOME'])
@nixpulvis
nixpulvis / temperature-alarm.sh
Created November 24, 2021 16:10
Watching the weather is serious stuff, better use bash.
#!/bin/sh
alarm() {
while :;
do
play ~/Loud_Alarm_Clock_Buzzer-Muk1984-493547174.wav
done
}
while :;
$ events --today
┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┑
│ Thursday 2020-10-08 │
├─────────────────────────────────────────────────────────────────────────────┤
│ ▾ Groceries │
│ ☒ Coffee │
│ ▸ Readings (due: 10-20) │
└─────────────────────────────────────────────────────────────────────────────┘
@nixpulvis
nixpulvis / gem-reset
Last active October 5, 2020 15:21
Remove all non default gems. For ruby 2.0.0
#!/usr/bin/env ruby
# Remove all gems EXCEPT defaults :)
`gem list -d`.split(/\n\n^(?=\w)/).each do |data|
match = data.match(/(?<name>([^\s]+)) \((?<versions>.*)\)/)
name = match[:name]
versions = match[:versions].split(', ')
if match = data.match(/^.*\(([\d\.]*),? ?default\): .*$/)
next if match[1].empty? # it's the only version if this match is empty
union U {
u32: u32,
u64: u64,
f32: f32,
f64: f64,
}
fn main() {
let u32 = U { u32: 1 };
let u64 = U { u64: 1 };
let highlights = execute('highlight')
let highlights = substitute(highlights, '\n\s\+', ' ', 'g')
let highlights = split(highlights, '\n')
call map(highlights, "split(v:val, '\\s\\+xxx\\s\\+')[0]")
for group in highlights
execute 'highlight' group 'NONE'
endfor
@nixpulvis
nixpulvis / median.rb
Created April 28, 2020 22:55
Median of Medians Algorithm
# Common median algorithm, which averages the two middle
# values when needed.
def median(list)
sorted = list.sort
half = (list.size - 1) / 2
if list.size.even?
(sorted[half] + sorted[half+1]) / 2.0
else
sorted[half]
end
@nixpulvis
nixpulvis / segv.c
Last active March 19, 2020 21:18
Intel pthreads Issue
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static pthread_t thread;
static pthread_mutex_t mutex;
void* work(void* ptr)
{
pthread_detach(pthread_self());
@nixpulvis
nixpulvis / dhm.rs
Created March 16, 2020 03:29
Diffe-Hellman-Merkle Key Exchange Demonstration (using curves crate)
//! ```cargo
//! [dependencies]
//! rand = "0.3.16"
//! curves = { git = "https://gitlab.com/neucrypt/curves", branch = "release" }
//! ```
extern crate rand;
extern crate curves;
use curves::{Secp, SecpOrd, ECGroup, Ford};
/// This demonstrates the math beneath a Diffe-Hellman-Merkle key exchange, and