Skip to content

Instantly share code, notes, and snippets.

View savanovich's full-sized avatar

Alexey Savanovich savanovich

View GitHub Profile
/*
Original here https://www.technovelty.org/linux/poking-around-in-auxv-part-2.html
Changed for x64
gcc read-auxv.c -o read-auxv
./read-auxv <PID>
*/
#define _GNU_SOURCE
#include <stdio.h>
@savanovich
savanovich / flac.sh
Last active May 11, 2022 12:48
Splits flac file to separate tracks.
#!/bin/bash
# !!! PREREQUISITES !!!
# chmod u+x ~/bin/flac.sh
#
# sudo apt install cuetools shntool enca
#
# sudo add-apt-repository -y ppa:flacon
# sudo apt-get update
# sudo apt-get install -y flacon
@savanovich
savanovich / likely.c
Last active August 29, 2015 14:12
C: likely / unlikely userspace macros
#if __GNUC__ >= 3
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
#else
# define likely(x) (x)
# define unlikely(x) (x)
#endif
@savanovich
savanovich / packed_struct.c
Created March 5, 2015 10:53
C: Do not allow structure padding
struct test_t {
int a;
char b;
int c;
} __attribute__((__packed__));
@savanovich
savanovich / string_literal.c
Created March 6, 2015 03:45
C Principals: Local string literal vs local value on stack
#include <stdio.h>
int* foo(void);
int* foo2(void);
char* bar(void);
char* bar2(void);
int main(int argc, char* argv[])
{
@savanovich
savanovich / datetime_locale.py
Created March 26, 2015 08:00
PY: Format datetile in locale
import locale
from datetime import datetime, timedelta
try:
locale.setlocale(locale.LC_TIME, '%s.UTF-8' % lang)
except Exception as e:
logger.error("Error setting exception", lang=lang, exception=e)
# English locale
locale.setlocale(locale.LC_TIME, 'en_US.UTF-8')
@savanovich
savanovich / measure_time.c
Created April 5, 2015 02:37
Measure working time
### gcc -std=c11 -O0 -Wall -o measure_time measure_time.c
#include <stdio.h>
#include <time.h>
int main()
{
clock_t start = clock();
for (int i = 0; i < 10000; i++)
i += 1;
@savanovich
savanovich / inline_opcode.c
Last active August 29, 2015 14:19
Inline asm opcode
/* http://habrahabr.ru/company/intel/blog/200658/
gcc -O0 -Wall mulsd.c
./a.out
4.000000 2.000000
*/
#include <stdio.h>
int main() {
double a[2] = {2, 2}, b[2] = {0, 0};
@savanovich
savanovich / safe_spawnp.c
Last active August 29, 2015 14:21
Safe posix_spawn alternative
/*
http://blog.kazuhooku.com/2015/05/how-to-properly-spawn-external-command.html
https://github.com/h2o/h2o/blob/08e6f20b0e85b830f6334253b72735bb15980dab/lib/common/serverutil.c
*/
pid_t safe_spawnp(const char *cmd, char **argv)
{
int pipefds[2] = {-1, -1}, errnum;
pid_t pid;
ssize_t rret;
@savanovich
savanovich / stop_gdb.py
Last active August 29, 2015 14:26
Sends SIGTRAP signal and stops gdb
"""
$ gdb --args ./python /home/user/src/python/stop_gdb.py
(gdb) run
"""
import os
import signal
signal.signal(signal.SIGTRAP, lambda *args, **kwargs: None)
stop = lambda: os.kill(os.getpid(), signal.SIGTRAP)