Skip to content

Instantly share code, notes, and snippets.

@h4k1m0u
h4k1m0u / Webgl-example.md
Created June 18, 2022 23:06
Draw a noisy triangle with WebGL using Regl and Glslify

Install dependencies

$ npm install

Run on webbrowser

$ npm start
@h4k1m0u
h4k1m0u / process.c
Created May 11, 2022 19:39
Fork children processes & wait for them to finish
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
const int n_processes = 5;
pid_t pid;
@h4k1m0u
h4k1m0u / args.c
Created May 11, 2022 19:10
Parses cli arguments passed to executable using getopt()
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
/* https://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html */
int main(int argc, char* argv[]) {
bool is_a = false;
bool is_b = false;
bool is_c = false;
char* value_b = "";
@h4k1m0u
h4k1m0u / ip.c
Created May 11, 2022 19:07
Get ip address by url
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
/**
* Find ipv4 addresses (tcp) associated with given hostname
@h4k1m0u
h4k1m0u / ls.c
Created May 11, 2022 19:04
Open stream to read output of shell command
#include <stdio.h>
/* Inspired by: https://hackaday.com/2022/03/16/linux-fu-simple-pipes/ */
int main() {
// creates a pipe between calling pgm & executed command (`ls`) and
// returns a stream to read from the pipe
FILE* pipe = popen("ls -l", "r");
// read the output of shell command from stream
char buffer[64];
@h4k1m0u
h4k1m0u / exec_time.c
Created May 11, 2022 19:00
Calculate execution time in C
#include <stdio.h>
#include <time.h>
#include <math.h>
/* https://www.educative.io/edpresso/what-is-clockt-in-c */
int main() {
clock_t start_ticks = clock();
size_t n_iters = pow(10, 9);
for (size_t i = 0; i < n_iters; i++) {
@h4k1m0u
h4k1m0u / Client-Server in C.md
Created May 11, 2022 18:57
Client-Server in C

Server

  • Each time a client connects, a message entered by the user is sent to it
  • Can serve multiple clients simultaneously using processes, while still waiting for other incoming connections

Client

  • Receives the same message 10 times from the server
  • Disconnects automatically once the server goes down
@h4k1m0u
h4k1m0u / List commits on a single line
Last active May 8, 2022 19:51
List commits on a single line (date & commit message)
git log --pretty=format:'%ad - %s' --date=human
@h4k1m0u
h4k1m0u / NanoVG-example.cpp
Last active March 19, 2025 08:03
Rendering basic shapes with OpenGL using NanoVG
// file: src/main.cpp
#include <iostream>
#include "glad/glad.h"
#include "window.hpp"
#include "nanovg.h"
#define NANOVG_GL3_IMPLEMENTATION
#include "nanovg_gl.h"
@h4k1m0u
h4k1m0u / cairo-xcb.md
Last active February 6, 2022 12:57
Draws vector graphics with cairo on a xcb window

XCB

  • Client-side of X11 display server (replacement for xlib), stands for X protocol C-language Binding.
  • Check the [official tutorial][xcb-tutorial].

Terminology

From this [Stackoverflow question][stackoverflow]:

  • Each X server has one display identified by a display number (by default 0).
  • A display is a collection of monitors (screens) sharing the same input devices (keyboard, mouse).
  • Multi-user systems have multiple displays, so that more than one person can do graphic work. For instance, two users connected to the GUI of the same machine will have different values for $DISPLAY variable.