Skip to content

Instantly share code, notes, and snippets.

View McUsr's full-sized avatar
💭
I may be slow to respond.

Tommy Bollman McUsr

💭
I may be slow to respond.
View GitHub Profile
@McUsr
McUsr / find.c
Last active June 1, 2024 07:30
C-version of Software Tools in Pascal "find" command, with done exercises
// Software tools p.p 146
/* Converted to C by me. All faults are mine. No warranties what so ever.
* Most of the code is written by Kernighan or Plaugher, so the code is really
* copyrighted by them. I only translated it to C.
*/
#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@McUsr
McUsr / real_arena.c
Last active February 26, 2024 16:25
An arena backed memory allocator
/* License:
* Copyright (C) 2023 - McUsr This program is free software; you can
* redistribute it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version. This
* program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details. You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
@McUsr
McUsr / noncanon.c
Last active October 15, 2023 23:38
Press any key to continue function that reprints message when brought back to foreground.
#define _XOPEN_SOURCE 500
/*
* Termios handling stolen from https://www.gnu.org/software/libc/manual/html_node/Noncanon-Example.html_node
* Reworked to make a press any key to continue function, which handles SIGCONT in a shell with
* job control, so that the message reappears, if we hit SUSP (^Z), while we are waiting for a keypress.
*
* To compile:
* cc -std=c99 -g3 -D_POSIX_SOURCE -o noncanon noncanon.c
*
@McUsr
McUsr / sem_mmap_fork.c
Last active September 21, 2023 11:16
Semaphores controlling access to mmap between parent and child
/*
This is a demo of sharing memory between parent and child, and
to use semaphores to controll access between the processes
for the shared memory.
Based on:
The example with semaphores in `man shm_open`, and
the mmap demo shared between parent and child in:
@McUsr
McUsr / nrcp.c
Last active July 12, 2023 11:16
A utility to create a numbered copy of a file into the current directory
/**
* @brief nrcp : makes a numbered copy of a file.
*
* It finds the ascending number that isn't used
* as a suffix for the file, and creates a copy
* of the file with that number appended as a
* suffix -- in the same directory as the original
* file!
*
* @author McUsr
/* Basic hash example an example found on the internet
*
*
* I u/McUsr made it work flawlessly with the good help of
* r/C_programming:
* u/Bitwise_Gamgee, u/No-Adhesiveness-8125 and u/curiously-b2.
* I got insights and suggestions from u/t40, u/ThatsMyFavoriteThing.
* u/flyingiron, u/Daiktana, and last but not least u/skeeto!
* Thanks a bunch guys!
*
@McUsr
McUsr / ckwd.c
Last active May 23, 2023 01:30
/* K&R p.p 136
* Exercise 6-1
* 2023 (c) McUsr -- Vim licence.
*
* "Our version of getword does not properly handle underscores, string
* constants, comments or preprocesor control lines. Write a better version."
*
* My solution is a tad different from what I have seen online, probably
* because my motivation is that I'd like to have my options open for
* including the entities at a later point in time into hash tables, for
/* 1987 (c) Stephen R. Bourne (from the Unix system V Environment).
* I have been true to the style, except for adding typedeclarations in order
* for it to compile without warnings on gcc (-std90?) without warnings.
* McUsr 14.05.2023
*/
#include <stdio.h>
#define MAXF 256
.TH FIELD 1 1987-1-1
.SH NAME
\fBfield\fR \(en select fields or columns from a file.
.SH SYNOPSIS
\fBfield\fR [\fB n \fR] \fI...\fR
.SH DESCRIPTION
The \fBfield\fR command copies specified, tab-separated fields from the
standard input to the standard output. Fields are numbered from 1
and a field may be requested more than once.
.SH BUGS
@McUsr
McUsr / chunk.c
Last active May 12, 2023 10:28
Chunk.c prints chunks from a text file.
/* chunk.c A combination of head and tail for ease of use.
* A derivation of an exercise of writing a tail command in K&R.
*
* Thank you u/xkcd__386 for pointing out flaws in the comments.
*
* The largest chunk can be MAXLINES long, and files can be arbitrarily long
* (in lines). (see show_help())
*
* Arguments:
*