Skip to content

Instantly share code, notes, and snippets.

View scriptum's full-sized avatar

Pavel Roschin scriptum

View GitHub Profile
@scriptum
scriptum / getpid.c
Last active December 14, 2022 06:10
Getting PID of process by its command name in Linux-like OS.
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
/*
* This is example of getting PID of process by name. Returns first matched PID or -1.
* */
int getPidByName(const char *name)
{
@scriptum
scriptum / Makefile
Created September 9, 2013 13:24
Simplest universal Makefile for *.c single file apps
CFLAGS += -O2 -pipe -Wall -Wextra
LDFLAGS += -lm
HEADERS = $(wildcard *.h)
SOURCES = $(wildcard *.c)
TARGETS := $(SOURCES:.c=)
all: $(TARGETS)
@scriptum
scriptum / fork.c
Last active December 22, 2015 15:58
Simplest possible fork example for Linux
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define __USE_GNU
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
pid_t child;
@scriptum
scriptum / hashtable.c
Created September 9, 2013 13:34
Simple and fast hash table for C language
/*******************************************************************************
Copyright (c) 2013 Pavel Roschin (aka RPG) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above
@scriptum
scriptum / utf8.c
Last active December 22, 2015 15:58
This macros allows easily convert Utf-8 encoded string to array of UNICODE characters (unsigned int)
// a - your string (char *), i - iterator (uint), c - output character (uint)
#define UTF8_TO_UINT(a, i) \
if((a[i] & 0b10000000) == 0) { \
c = (unsigned)a[i++]; \
} \
else if (((unsigned)a[i] & 0b11100000) == 0b11000000) { \
c = ((unsigned)a[i++] & 0b00011111) << 6; \
c |= (unsigned)a[i++] & 0b00111111; \
} \
else if (((unsigned)a[i] & 0b11110000) == 0b11100000) { \
@scriptum
scriptum / cairo_hash_string.c
Last active December 22, 2015 21:29
string hash from cairo
#define _CAIRO_HASH_INIT_VALUE 5381
unsigned long _cairo_hash_string (const char *c)
{
/* This is the djb2 hash. */
unsigned long hash = _CAIRO_HASH_INIT_VALUE;
while (c && *c)
hash = ((hash << 5) + hash) + *c++;
return hash;
}
@scriptum
scriptum / hash speed
Created September 24, 2013 14:57
Hash tables speed in different languages
$ make
/usr/bin/time -f "Time:\t%e\nMemory:\t%M" php test.php
Time: 0.59
Memory: 655984
/usr/bin/time -f "Time:\t%e\nMemory:\t%M" perl test.pl
Time: 0.64
Memory: 353456
/usr/bin/time -f "Time:\t%e\nMemory:\t%M" python test.py
Time: 0.92
Memory: 544240
@scriptum
scriptum / itoa.c
Last active December 23, 2015 20:18
itoa - convert int to char *
char *itoa(int i)
{
static char buf[12];
int j = 11;
int sign = 0;
buf[j] = '\0';
if(i < 0)
{
i = -i;
sign = 1;
@scriptum
scriptum / command-line
Last active December 23, 2015 20:49
Geany leaks catched from valgrind
G_SLICE=always-malloc G_DEBUG=gc-friendly:resident-modules valgrind --tool=memcheck --leak-check=full --log-file=vgdump --suppressions=gtk.suppression --show-possibly-lost=no src/geany
@scriptum
scriptum / demo.py
Last active December 26, 2015 04:19
Extremely packed 1k demo
from OpenGL.GL import*;from OpenGL.GLUT import*;from OpenGL.GL.shaders import*;from time import*;t=time();glutInit();glutInitDisplayMode(2);glutInitWindowSize(640,480);glutCreateWindow('');glOrtho(0,1,0,1,0,1);glMatrixMode(5888);p=compileProgram(compileShader('<SHADER>',35632));
def d():glUseProgram(p);glUniform1f(0,time()-t);glBegin(7);glVertex2i(0,1);glVertex2i(1,1);glVertex2i(1,0);glVertex2i(0,0);glEnd();glutSwapBuffers()
glutIdleFunc(d);glutMainLoop()