Skip to content

Instantly share code, notes, and snippets.

View mspreij's full-sized avatar
🧗
OK

Maarten Spreij mspreij

🧗
OK
View GitHub Profile
Example script that uses pcntl_fork() to fork itself a few dozen times, and System V IPC functions to create a message queue,
let the child processes send data to it and the parent process read and display it.
#!/usr/bin/env php
<?php
// Create or attach to a message queue
$queue = msg_get_queue(ftok(__FILE__, 'a'), 0600); # set permissions so that only the owner can read/write to this queue
# ftok() creates a message queue key based on the file's inode and a single character project identifier. The latter lets
# one create several queues all based on/in the same file.
@mspreij
mspreij / ef.sh
Last active August 4, 2023 12:28
########
# Edit Functions live. often I wanted to tweak a function but didn't want to go to the bother of finding out where in my
# home dir it was defined (yeah I should clean my room)
########
ef ()
{
if [[ -z $1 || "$1" == "-?" ]]; then
echo " ef will let you edit already defined functions, or executable bash/php scripts /somewhere/ in \$PATH.";
echo " Function changes will only remain during the session, scripts are overwritten.";
lpath ()
{
if [[ -z $1 ]]; then
echo " E: expected path";
echo " lpath shows permissions, user, group for each part of a given path to file or directory (with ls -lad)";
return 1;
fi;
local path=${1#/};
local oldIFS="$IFS";
IFS='/';
// based on: https://github.com/scottaohara/select-to-datalist/blob/master/assets/js/progressive-datalist.js
// changes select to input with datalist, that thing where you type and it suggests matching entries
function selectToDatalist(element) {
// todo: accept actual element instead of selector string
var select = document.querySelector(element);
if (select.tagName != 'SELECT') {
console.error('not a select: '+ element);
return false;
}
// ==UserScript==
// @name Google results
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.google.com/search*
// @grant none
// @require http://code.jquery.com/jquery-2.2.4.min.js
// sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=
@mspreij
mspreij / google.php
Last active December 24, 2018 14:10
<?php
$q = $_GET['q'];
$googStump = "https://www.google.com/search?q=";
$gooogle = $googStump.rawurlencode($q);
// custom keys
$keys = [
'hn' => 'https://news.ycombinator.com/',
];
$input = explode("\n", file_get_contents('3.txt'));
$count = count($input);
echo $count. " inputs\n";
foreach ($input as $i => $line) {
preg_match('/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/', $line, $m);
$rects[$m[1]] = ['x'=>$m[2], 'y'=>$m[3], 'x2'=>$m[2]+$m[4], 'y2'=>$m[3]+$m[5]];
}
$filled = [];
// having turned input into an array..
// Levenshtein 0.04 seconds
for ($i=0; $i < 250; $i++) {
for ($j=$i+1; $j < 250; $j++) {
if (levenshtein($input[$i], $input[$j]) == 1) {
echo $input[$i].':'.$input[$j]."\n";
}
}
}
/*
We have a table with read/write permissions for every User Role / Object Type / Object Property.
When adding a new Property to an object we often copy the rights from an existing, similar property, with this query:
*/
INSERT INTO rights (roleId, strType, strProperty, `read`, `write`)
SELECT roleId, strType, newProperty, `read`, `write`
JOIN (SELECT 'new property one' AS newProperty UNION SELECT 'new property two' UNION SELECT 'new property three')
FROM rights
WHERE strType = 'phdRegistrations'
AND strProperty = 'existingProperty';
var yadda = (function() {
var myObject = {};
var pubVar = 123;
var privVar = 456;
var someDefaultOptions = {};
function public1() {
// ...
return 'allKindsOfInterestingStuff';
}