Skip to content

Instantly share code, notes, and snippets.

View ichramm's full-sized avatar

Juan Ramirez ichramm

View GitHub Profile
@ichramm
ichramm / enable-tmp-on-ram.sh
Created September 8, 2019 18:27
Configure /tmp on ram (systemd-based approach)
#!/bin/bash
# Note: Default are good enough for most uses
sudo cp /usr/share/systemd/tmp.mount /etc/systemd/system/tmp.mount
sudo systemctl enable tmp.mount
sudo systemctl start tmp.mount
@ichramm
ichramm / utf8_check_is_valid.cpp
Created January 25, 2019 13:21
C++ UTF-8 validation function
// Based on this one: http://www.zedwood.com/article/cpp-is-valid-utf8-string-function
bool utf8_check_is_valid(const char *str, int len) {
int n;
for (int i = 0; i < len; ++i) {
unsigned char c = (unsigned char) str[i];
//if (c==0x09 || c==0x0a || c==0x0d || (0x20 <= c && c <= 0x7e) ) n = 0; // is_printable_ascii
if (0x00 <= c && c <= 0x7f) {
n=0; // 0bbbbbbb
} else if ((c & 0xE0) == 0xC0) {
n=1; // 110bbbbb
@ichramm
ichramm / guid.cpp
Last active September 3, 2020 13:57
Poor man's GUID generator (not compliant with RFC 4122)
static std::string make_guid() {
int integers[4] = { rand(), rand(), rand(), rand() };
std::string result(36, '\0');
sprintf(&result[0], "%08x-%04x-%04x-%04x-%04x%08x",
integers[0],
integers[1] >> 16,
integers[1] & 0xffff,
integers[2] >> 16,
integers[2] & 0xffff,
integers[3]);
@ichramm
ichramm / recursion_samples.cpp
Created December 21, 2017 23:12
Recursion samples
#include <stdio.h>
#define DO_ITER 1
#define DO_REC 2
#define DO_DP 3
#define DO 3
int fibonacci(int n);
@ichramm
ichramm / tail_call_optimization.asm
Created December 21, 2017 23:10
Some assembly code showing tail-call optimization
; NOT OPTIMIZED
; n * factorial(n - 1);
mov eax,dword ptr [n] ; eax = n
sub eax,1 ; eax = eax - 1
push eax ; push eax onto the stack
call factorial ; call factorial with the argument pushed above
add esp,4 ; restore stack pointer
imul eax,dword ptr [n] ; final multiplication
@ichramm
ichramm / mongomon.sh
Last active November 23, 2016 15:34
Mongo monitor
#!/bin/bash
DATE=$(date '+%s')
INTERVAL=10
mongostat --json $INTERVAL > mongostat_$DATE.log &
MONGOSTAT=$!
mongotop --json $INTERVAL > mongotop_$DATE.log &
MONGOTOP=$!
@ichramm
ichramm / index.js
Created November 3, 2016 01:56
MongoDB (Simple) Performance Test: Multiple Collections vs Indexes
/*jshint esversion: 6 */
var mongodb = require('mongodb');
var uri = 'mongodb://localhost:27017/test';
var Status = {
GREEN : 'green',
YELLOW: 'yellow',
RED : 'red'
@ichramm
ichramm / crypto.log
Last active November 4, 2016 19:55
Setup Encrypted LVM
root@neon:~# cryptsetup luksFormat -c aes-xts-plain64:sha512 -h sha512 /dev/sda3
WARNING!
========
This will overwrite data on /dev/sda3 irrevocably.
Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
root@neon:~# cryptsetup luksOpen /dev/sda3 sda3-crypt
@ichramm
ichramm / core-values-greeting.sh
Created June 30, 2016 19:07
Prints an Intraway's core value in ASCCI art
#!/bin/bash
# File: core-values-greeting.sh
# Author: ichramm
# Description: Prints a core value in ASCCI art
# Usage: core-values-greeting.sh [core-value|login]
# Params:
# core-value: The core value to print , Accepted values: become,enjoy,more,strive,team,think,wow
# Prints a random core value if not set,
# Orints a greeting plus a random core value if it is set to 'login'
@ichramm
ichramm / check-aur-updates.sh
Last active March 4, 2021 00:36
(Archlinux) Update check for packages installed from the AUR
#!/bin/bash
pacman -Qmq | while read package; do
cur_version=$(pacman -Qi $package | egrep 'Version *:' | sed 's/Version *: *//'); # fast
aur_version=$(yaourt -Si $package | egrep 'Version *:' | sed 's/Version *: *//'); # slow
if [ "$cur_version" = "$aur_version" ]; then
echo -e "\e[0;32mUp to date: \e[0m$package";
else
echo -e "\e[1;33mNeeds update: \e[0m$package \e[0;33m(installed: $cur_version, aur: $aur_version)\e[0m";
fi