Skip to content

Instantly share code, notes, and snippets.

@p120ph37
p120ph37 / epsilon.pl
Created January 21, 2016 19:14
Demonstrates machine epsilon in Perl
#!/usr/bin/perl
use warnings;
use strict;
my $val1;
if(($ARGV[0] // '') eq 'fixed') {
require Math::FixedPoint;
$val1 = Math::FixedPoint->new(2.1, 2); # fixed-point number
} else {
$val1 = 2.1; # floating point number
@p120ph37
p120ph37 / rpms.rb
Created October 15, 2015 18:13
Facter custom fact to query installed RPMs
Facter.add(:rpms) do
setcode do
gs = "\x1D"
rs = "\x1E"
us = "\x1F"
# see:
# "rpm --querytags"
# http://rpm5.org/docs/api/queryformat.html
# http://www.rpm.org/max-rpm/ch-queryformat-tags.html
rpmtags = {
@p120ph37
p120ph37 / threadmutex.c
Last active September 22, 2015 07:56
Curl example with a background thread, a mutex, and a shared buffer
/*
* Perform a fetch in a background worker thread, and use a mutex to safely grab chunks from the buffer.
*
* Usage:
* cc threadmutex.c -lcurl -lpthread -o threadmutex
* ./threadmutex
*
* Or, if curl will be using OpenSSL define "USE_OPENSSL":
* cc threadmutex.c -lcurl -lpthread -lcrypto -DUSE_OPENSSL -o threadmutex
* ./threadmutex
@p120ph37
p120ph37 / nph-comet.pl
Last active September 22, 2015 06:21
Perl NPH CGI streaming Comet events
#!/usr/bin/perl
use warnings;
use strict;
sub chunk { sprintf "%x\x0D\x0A%s\x0D\x0A", length($_[0]), $_[0]; }
$|=1;
print "HTTP/1.1 200 Ok\x0D\x0A";
print "Content-Type: text/html\x0D\x0A";
@p120ph37
p120ph37 / chunkedpost.c
Created August 30, 2015 18:46
Curl example with chunked post
/*
* HTTP POST with chunked encoding.
*
* Usage:
* cc chunkedpost.c -lcurl -o chunkedpost
* ./chunkedpost
*
*/
#include <stdio.h>
#include <stdlib.h>
@p120ph37
p120ph37 / win32-ping.pl
Created July 31, 2015 22:10
Example using Win32::PingICMP
use warnings;
use strict;
use Win32::PingICMP;
use Socket qw/inet_ntoa/;
# The exact error strings as used by ping.exe
my %errors = (
'IP_BUF_TOO_SMALL' => 'General failure.',
'IP_DEST_NET_UNREACHABLE' => 'Destination net unreachable.',
'IP_DEST_HOST_UNREACHABLE' => 'Destination host unreachable.',
@p120ph37
p120ph37 / basicauthsoap.c
Last active August 29, 2015 14:25
libcurl basic-auth soap example
/*
* HTTP POST with authentiction using "basic" method.
* Hybrid of anyauthput.c and postinmemory.c
* Specifically adapted for SOAP use.
*
* Usage:
* cc basicauthsoap.c -lcurl -o basicauthsoap
* ./basicauthsoap
*
*/
@p120ph37
p120ph37 / info.pl
Created July 14, 2015 21:21
Simple CGI that displays information about the request.
#!/usr/bin/perl
use warnings;
use strict;
use CGI qw/param escapeHTML/;
use MIME::Base64;
### Apache2's mod_cgi does not pass the HTTP__AUTHORIZATION header by default.
### Add this to .htaccess to work around the issue...
# RewriteEngine on
# RewriteCond %{HTTP:Authorization} ^(.*)
@p120ph37
p120ph37 / basicauthpost.c
Created July 14, 2015 21:14
libcurl basic-auth post example
/*
* HTTP POST with authentiction using "basic" method.
* Hybrid of anyauthput.c and postinmemory.c
*
* Usage:
* cc basicauthpost.c -lcurl -o basicauthpost
* ./basicauthpost
*
*/
#include <stdio.h>
@p120ph37
p120ph37 / d64
Created July 7, 2015 21:20
Base64 decode purely via Bash builtins.
#!/bin/sh
((V=N=0))
while :; do
((V<<=6,++N))
IFS= read -n1 C && {
printf -vC '%d' "'$C"
((C=C>64&&C<91?C-65:C>96&&C<123?C-71:C>47&&C<58?C+4:C==43?62:C==47?63:(V>>=6,--N,0),V|=C))
}
((N==4)) && {