Skip to content

Instantly share code, notes, and snippets.

View gsauthof's full-sized avatar

Georg Sauthoff gsauthof

View GitHub Profile
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int pid = vfork();
if (pid == 0) {
_exit(0);
}
@gsauthof
gsauthof / last_lines.py
Created October 31, 2017 13:36
asyncssh compatible file-like object for capturing the most recent lines
class Last_Lines:
def __init__(self, n, binary=True):
self.n = n
self.binary = binary
self.lines = []
# asyncssh calls fstat() on fileno() and checks if it is a regular file
# thus we open any regular file to fake it ...
self.dev_null = open('/etc/resolv.conf')
def write(self, s):
lines = s.splitlines(True)
@gsauthof
gsauthof / feedgen_bench.py
Last active February 12, 2017 16:27
Costs of ordered attributes in generated feeds
# in reply to: https://groups.google.com/d/msg/django-developers/6mm42rrQXww/Yee5KJjVBQAJ
# (pull request: write feeds with ordered attributes)
# cf. https://github.com/django/django/pull/8044
#
# Results:
# - generating one feed: ~ 0.01 s (current Laptop, i7, SSD)
# - sorting + OrderedDict() in SimpleXMLGenerator::startElement(): + ~ 27 %
# - sorting + OrderedDict() iff there are attributes: + ~ 18 %
# - replacing {} with [] for attributes in feedgenerator: + ~ 7 %
@gsauthof
gsauthof / orphan.c
Created November 27, 2016 09:44
Illustrating how orphaned children aren't necessarily adopted 2 by PID 1
/* For illustrating how orphaned children aren't necessarily adopted
by PID 1.
See also: http://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits/36945270#36945270
2016, Georg Sauthoff <mail@georg.so>
*/
#include <unistd.h>
@gsauthof
gsauthof / boost_hex.cc
Last active October 3, 2016 20:00
comparison of boost hex algorithm and printf conversion specifier
// re: http://stackoverflow.com/questions/28489153/how-to-portably-compute-a-sha1-hash-in-c/39833022#39833022
#include <boost/uuid/sha1.hpp>
#include <boost/detail/endian.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <boost/endian/conversion.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
% curl -L -o signature.png \
% 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Beethoven_Signature.svg/150px-Beethoven_Signature.svg.png'
% compile twice: pdflatex sig_overlay.tex
\documentclass{article}
\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\node [xshift=150mm,yshift=-80mm]
@gsauthof
gsauthof / fib.c
Created July 25, 2016 06:57
Syntactic variation of a Fibonacci example
// in reply to http://verse.systems/blog/post/2016-07-18-Software-Engineering-Teaching/
unsigned fibonacci(unsigned n) {
unsigned a = 0, b = 1;
unsigned sum = 0;
for ( ; n > 0; --n) {
unsigned t = b;
b += a;
a = t;
sum += a;
@gsauthof
gsauthof / addressoffn.cc
Last active July 2, 2016 10:59
Address of function example
/*
Illustrate how the & operator can be omitted when getting a function pointer.
In reply to: https://www.youtube.com/watch?v=z-kUhwANrIw
2016, Georg Sauthoff
*/
@gsauthof
gsauthof / xclipshow.cpp
Created January 27, 2016 21:59
Simplified, shortened version of http://unix.stackexchange.com/a/163115/1131, a clipboard dump tool
// source: http://unix.stackexchange.com/a/163115/1131
// GS, 2016-01-27, simplify the code a little bit
#include <QApplication>
#include <QTimer>
#include <QClipboard>
#include <QMimeData>
#include <QDebug>
#include <string>
#include <iostream>