Skip to content

Instantly share code, notes, and snippets.

View nilput's full-sized avatar

Turki Alsaleem nilput

View GitHub Profile
@nilput
nilput / main.c
Created November 7, 2021 18:33
An example showing usage of alloca() function, additionally: the stack pointer is printed after each call.
#include <stdio.h>
#include <alloca.h>
#include <string.h>
__attribute__((always_inline))
static inline void print_rsp() {
#if defined(__x86_64__)
void *rsp = NULL;
asm ("mov %%rsp, %0;"
:"=r"(rsp) /* output */
@nilput
nilput / pwgen.py
Last active October 24, 2021 20:57
Passphrase generator: an english dictionary password generator all in a single file. to use it just run: "python pgen.py".
#!/usr/bin/python3
import secrets
NUMBER_OF_WORDS = 4
NUMBER_OF_DIGITS = 3
SYMBOLS = ['#', '@', '!', '$', '%']
WORDS = None
def main():
pw = ''
@nilput
nilput / arabic-bidi.html
Last active July 27, 2021 10:40
An example page to show how arabic is supposed to be displayed in weechat, which might be useful to developers to check their implementation. using HTML/CSS.
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Arabic Bidirectional Text Example</title>
<base href="https://nilput.com/">
<link rel="alternate" type="text/html" title="Arabic Bidirectional Text Example" href="https://nilput.com/pages/arabic">
<link rel="author" type="text/html" title="Turki's personal website" href="https://nilput.com">
<link rel="icon" href="/favicon.ico">
<link rel="stylesheet" href="/css/fonts/custom_fa.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
@nilput
nilput / def.c
Last active July 17, 2021 06:46
C Constants Definitions, Preprocessor Macro Function Redefinition Example
/*
In many cases, we have situations where we need to define a lot of constants / data definitions.
examples: enums, definitions such as cpu instructions encodings, http message response types, lookup tables, etc..
this method is seen in many projects and it's a good solution to the problem, it avoid redundancy and code duplication,
and it help maintain a single source of truth.
*/
#include <stdio.h>
enum item_type { FRUIT, VEGETABLE };
@nilput
nilput / cexec
Last active May 31, 2020 09:59
a script to compile and execute C snippets
#!/bin/bash
function die() {
echo "$*" >&2
[ -n "$tmp" -a -d "$tmp" ] && rm -rf "$tmp"
exit 1;
}
tmp="$(mktemp -d -t cexec-XXXXXX)" && [ -n "$tmp" -a -d "$tmp" ] || die "couldn't create tmp dir";
[ "$1" = '-h' ] && die "$(printf "Usage: $0 <<<'C Source code")"
cat <(printf "#include <%s.h>\n" stdio stdlib string) \
<(printf 'int main(int argc,char *argv[]){') \
@nilput
nilput / javaheader
Created May 11, 2020 05:48
Javaheader - a script to generate a JNI header from a java source file
#!/bin/bash
function die() {
rm -r "$tmp_dir"
exit 1;
}
if [ "$#" -lt 1 ]; then
echo "Usage: $0 [java source file]"
exit 1;
@nilput
nilput / object.c
Created July 12, 2019 09:37
Simple example about objects and polymorphism in C
#include <stdio.h>
struct base {
const struct vt *vt;
};
struct vt {
void (*who)(struct base *b);
void (*talk)(struct base *b);
};
struct cat {
struct base base;
@nilput
nilput / sockets.c
Last active November 14, 2024 13:48
C socket server/client example, handles multiple clients on a single thread, single process.
//C tcp server/client example
// some of this is based on https://gist.github.com/oleksiiBobko/43d33b3c25c03bcc9b2b
// which has a couple of problems in the time of writing this.
//
// this handles multiple clients without blocking, without threads, without multiprocessing, using poll()
//
// Author: github.com/nilput <[email protected]>
// license: BSD-3
@nilput
nilput / serialize_structs.c
Last active May 23, 2019 09:11
an example showing serialization an deserialization routines for structs, ending up with a representation that can be used for network or files in a cross platform way.
///author: github.com/nilput <[email protected]>
//license: BSD-3
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#if CHAR_BIT != 8
@nilput
nilput / genp2.c
Created April 22, 2019 08:49
find prime near power of two using openssl
//find prime near power of two using openssl
//author: github.com/nilput
//compile using: cc genp2.c -o genp2 $(pkg-config --cflags libssl) $(pkg-config --libs libssl)
#include <openssl/bn.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>