Skip to content

Instantly share code, notes, and snippets.

View dgoguerra's full-sized avatar

Diego Guerra dgoguerra

View GitHub Profile
@dgoguerra
dgoguerra / human-size.c
Last active November 29, 2023 03:08
Format a quantity in bytes into a human readable string (C)
#include <stdio.h>
#include <stdlib.h> // atoll
#include <stdint.h> // uint64_t
#include <inttypes.h> // PRIu64
static const char *humanSize(uint64_t bytes)
{
char *suffix[] = {"B", "KB", "MB", "GB", "TB"};
char length = sizeof(suffix) / sizeof(suffix[0]);
@dgoguerra
dgoguerra / p4merge-git-tool.md
Last active March 24, 2024 14:02
Setup p4merge as difftool and mergetool on Windows

Setting up p4merge as diff and merge tool on Windows. Tried for Git version 1.8.4.msysgit.0.

Two alternatives are explained: using the command line, and directly editing the config file.

Setting up from the command line

Being the installation path "C:Program Files\Perforce\p4merge.exe", just run:

$ git config --global diff.tool p4merge
@dgoguerra
dgoguerra / debugging.md
Created January 28, 2014 09:49
Valgrind debugging notes

Remote debugging with GDB and Valgrind

First, Valgrind starts the program to debug:

$ valgrind --vgdb=yes --vgdb-error=0 [valgrind-options] [program] [program-options]

It will halt until GDB is hooked to the process, from another terminal:

@dgoguerra
dgoguerra / coredump.md
Last active August 29, 2015 13:56
Generating and viewing core dumps

Enable generating core dumps

The configuration file /proc/sys/kernel/core_pattern defines the default behaviour when a crash occurs. If it contains a single word such as core, a core dump will be generated on the current directory with that name. If the first character of its content is |, then the core file will be piped to a process, such as Apport (|/usr/share/apport/apport %p %s %c).

To enable core dumps for your current terminal session ignoring the default action:

ulimit -c unlimited
@dgoguerra
dgoguerra / node-npm-install.sh
Last active January 11, 2016 13:07
Install nodejs and npm latest versions; needs sudo rights
# C++ needed to compile node
# sudo apt-get -y install build-essential
cd /tmp
wget --no-verbose http://nodejs.org/dist/node-latest.tar.gz
tar zxf node-latest.tar.gz
cd node-v*
./configure && make && sudo make install
@dgoguerra
dgoguerra / script-with-options.sh
Last active November 23, 2023 19:17
Manual alternative to getopt in bash scripts, supporting short and long options
#!/usr/bin/env bash
# File name
readonly PROGNAME=$(basename $0)
# File name, without the extension
readonly PROGBASENAME=${PROGNAME%.*}
# File directory
readonly PROGDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Arguments
readonly ARGS="$@"
@dgoguerra
dgoguerra / .bash_profile
Last active April 17, 2019 19:40
My dotfiles (.gitconfig with git aliases, .bash_profile, etc.). Old! Now at: https://github.com/dgoguerra/dotfiles
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export HISTCONTROL=ignorespace
# http://unix.stackexchange.com/a/81699
alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'
# http://stackoverflow.com/a/13322549
alias lanip="ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'"
@dgoguerra
dgoguerra / www-data-permissions.md
Last active August 29, 2015 14:00
/var/www proper permissions

Add your user to www-data group.

sudo adduser USERNAME www-data

Then, change recursively the ownership of /var/www to your username.

sudo chown USERNAME:www-data -R /var/www
<!DOCTYPE html>
<html lang="en">
<head>
<link href="lib/bootstrap.css" rel="stylesheet">
<script type="text/javascript" src="lib/jquery.js"></script>
<script type="text/javascript" src="lib/bootstrap.js"></script>
</head>