Skip to content

Instantly share code, notes, and snippets.

@RaphGL
RaphGL / detect_original_user.c
Last active November 28, 2024 13:55
Detect what is the actual user running the comamnds. This can be used to detect if user is running sudo or running commands as any other user in the system.
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
// getuid retrieves the UID for the current user, the user can pretend to be another user,
// thus the UID is not necessarily attached to the current user running.
uid_t user_id = getuid();
// getpwuid retrieves the `struct passwd` which stores many some information about the user,
@RaphGL
RaphGL / linked_list.cpp
Created May 16, 2024 23:43
A linked list in C++ cause reasons???
#include <cstddef>
#include <initializer_list>
#include <iostream>
#include <stdexcept>
template <typename T> struct LLNode {
T val{};
LLNode<T> *next{nullptr};
};
@RaphGL
RaphGL / install_odin.sh
Last active October 2, 2024 17:21
Install Odin compiler and LSP
#!/bin/env sh
USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6)
# where to put the odin repos
REPO_DIR=${USER_HOME}/Documents
if [ "$EUID" -ne 0 ];
then echo "Please run as root"
exit
fi
@RaphGL
RaphGL / textbuffer.odin
Last active December 29, 2023 15:26
Bug printing textbuffer window with ncurses
package main
import "core:c"
import "core:fmt"
import "core:os"
import "core:slice"
import "core:strings"
import nc "ncurses/src"
TextBuffer :: struct {
@RaphGL
RaphGL / static_array_indices.md
Last active November 13, 2023 15:57
Static array indices in Modern C

Static Indices

C99 adds static indices letting you preserve size information for array parameters. This means that there's a guarantee that an array will never be null and has to have at least the specified number of items instead of just decaying into pointer.

So say we have this function signature:

void print_msg(char msg[10]);

You could call it this way:

@RaphGL
RaphGL / rle.c
Created September 20, 2023 15:14
Run-length encoding written in C11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
size_t times;
char chr;
} RLEChar;
// Run length Encoding
@RaphGL
RaphGL / sorting.js
Last active September 20, 2023 19:43
Sorting Algorithms in JavaScript
function bubbleSort(myArr) {
for (let j = myArr.length - 1; j > 0; j--) {
let biggest = myArr[j];
for (let i = 0; i < j; i++) {
const item1 = myArr[i];
const item2 = myArr[i + 1];
if (item1 > item2) {
myArr[i] = item2;
myArr[i + 1] = item1;
@RaphGL
RaphGL / FlappyBird.ch8
Created June 22, 2023 12:26
Flappy Bird for the Chip8. It uses Wernsey's Chip8 assembler: https://github.com/wernsey/chip8
define x v0
define y v1
define tube_x v2
define tube_y v3
define px v4
define py v5
define k_flap v6
define score v7
define x2 v8
define arg1 va
@RaphGL
RaphGL / portscanner.c
Created April 18, 2023 15:51
Very basic port scanner
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#define MAXPORTNUM 65535
int main(int argc, char **argv) {
if (argc < 2) {
@RaphGL
RaphGL / eventloop.go
Last active September 19, 2023 17:51
Event handling from scratch
package main
import "fmt"
type EventHandler struct {
// all events available on the system
validEvents []string
// a were an event string contains all of it's associated functions to be run
subscribers map[string][]func(s string)
// a queue of all raised events to be processed