Skip to content

Instantly share code, notes, and snippets.

View bynect's full-sized avatar
🙄
Procrastinating

nect bynect

🙄
Procrastinating
  • Laniakea Supercluster
  • 04:19 (UTC +02:00)
View GitHub Profile
@bynect
bynect / memswap.h
Last active August 23, 2021 00:57
Memswap implementation in ISO C.
#ifndef MEMSWAP_H
#define MEMSWAP_H
static inline void
memswap(void *restrict aptr, void *restrict bptr, size_t len)
{
register unsigned char *a = aptr;
register unsigned char *b = bptr;
register unsigned char t;
@bynect
bynect / vector.h
Last active March 16, 2021 21:28
Generic vector implementation in C using preprocessor macros.
#ifndef VECTOR_H
#define VECTOR_H
#include <stdint.h>
#include <stddef.h>
#include <malloc.h>
#define VECTOR(_type) VECTOR_##_type##_t
#define VECTOR_INIT(_type, _vec) VECTOR_##_type##init(_vec)
#define VECTOR_FREE(_type, _vec) VECTOR_##_type##free(_vec)
@bynect
bynect / lambda.h
Last active June 7, 2021 10:04
Anonymous functions in C using GCC extensions.
#ifndef _LAMBDA_H
#define _LAMBDA_H
#define LAMBDA(stmt, ret, ... /*args*/) \
({ ret __fun__(__VA_ARGS__) { stmt; }; __fun__; })
#endif
@bynect
bynect / strtoi.c
Created January 2, 2021 11:49
A simple implementation of string (char *) to int conversion in ISO C.
/* Naive implementation of string (char *) to int conversion in ISO C. */
int
strtoi(const char *str, unsigned int len)
{
int i, neg, num;
unsigned int val, pow;
if (str == 0 || len == 0)
@bynect
bynect / ascii.c
Created December 30, 2020 21:08
A shell utility that prints the integer value of the given ascii characters.
/* ascii.c -- print the integer value of the given ascii chars */
/*
compilation:
cc ascii.c -o ascii
usage:
ascii your characters here [...]
*/
@bynect
bynect / ls.rs
Created December 18, 2020 16:51
Educational implementation of the ls command in Rust. Doesn't support globbing nor proper error handling.
pub fn main() {
// collect the command line arguments into a vector of string
let mut argv: Vec<String> = std::env::args().collect();
// if no arguments are given assume the current directory
if argv.len() < 2 {
argv.push(String::from("."));
}
// iterate over the vector of args ignoring the first argument
@bynect
bynect / echo.rs
Last active December 18, 2020 16:52
Educational implementation of the echo command in Rust.
pub fn main() {
// collect the command line arguments into a vector of string
let argv: Vec<String> = std::env::args().collect();
// iterate over the vector of args ignoring the first argument
for arg in &argv[1..] {
// ignore args starting with '-'
if !arg.starts_with("-") {
// print argument without newline
print!(
@bynect
bynect / restapi.go
Last active January 24, 2021 19:51
Simple RESTful API made in Go with Mux.
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
@bynect
bynect / doughnut.c
Last active January 25, 2021 09:31
A spinning doughnut made in C.
//A spinning doughnut made in C.
//Found that on a Youtube vid, very cool though.
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int k;

String operator

Operator Description Example
= is equal to [ $a = $b ]
== is equal to [ $a == $b ]
!= is not equal to [ $a != $b ]
> is greater than, in alphabetical order [ $a > $b ]
< is less than, in alphabetical order [ $a < $b ]
-z has zero length [ -z $a ]
-n has non-zero length [ -n $a ]