42 Archive
42 is a peer-to-peer coding school: no teachers, no lectures, every project graded by other students. These are the common-core projects that aren't in the Arcade. Open a cartridge to see how it works.
RANK 0 · CLEARED
libftRank 0My own C standard library, rebuilt from scratch. It covers string, memory and character functions plus a linked-list API, and I reused it in every later 42 project.Memory managementPointer arithmeticAPI designShow walkthrough
The first project of the 42 curriculum. 42 bans almost all of the standard library, so everything later is built on top of this one. It includes ft_strlcpy, ft_memmove (which handles overlapping memory), ft_split, ft_itoa, and a singly linked-list API (ft_lstnew, ft_lstadd_back, ft_lstclear…) that frees nodes through a callback.
void ft_lstclear(t_list **lst, void (*del)(void *))
What I learned: to read man pages closely enough to match every edge case, such as NULL inputs, zero sizes and overlapping memory, and to write a Makefile that only recompiles what has changed.
RANK 1 · CLEARED
ft_printfRank 1A reimplementation of printf. It parses the format string and dispatches the c, s, p, d, i, u, x, X and % conversions through variadic arguments, returning the exact number of bytes written.Variadic functionsFormat parsingNumber base conversionShow walkthrough
The format string is read one character at a time. Every % sends the next specifier to a handler that pulls the right type out of the va_list and writes it out. Each handler returns how many characters it wrote, so the total always matches what the real printf would return.
else if (specifier == 'x')
chars_written += write_hex(va_arg(args, unsigned int), LOWER);
else if (specifier == 'p')
chars_written += write_ptr(va_arg(args, void *));
What I learned: how variadic functions really work at the ABI level, and why %p with NULL and the INT_MIN case of %d need special handling.
get_next_lineRank 1A function that returns one line at a time from any file descriptor, with any buffer size. It keeps unread data between calls in a static buffer.Static stateBuffered I/OMemory-leak-free codeShow walkthrough
read gives you a fixed number of bytes, not whole lines. The function keeps a static buffer, reads more until a newline appears, returns the line, and keeps whatever comes after it for the next call:
buffer = read_to_buffer(fd, buffer);
if (!buffer)
return (NULL);
line = extract_line(buffer);
buffer = update_buffer(buffer);
return (line);
It works the same with BUFFER_SIZE=1 and BUFFER_SIZE=10000000, and it leaks nothing at end-of-file.
What I learned: what static storage really means, and how to keep memory ownership clear when a function returns heap memory that the caller has to free.
RANK 2 · CLEARED
pipexRank 2Recreates the shell pipeline `< infile cmd1 | cmd2 > outfile` in C, using pipe, fork, dup2 and execve, with proper PATH lookup and error handling.Unix processesFile descriptorsError handlingShow walkthrough
The parent opens a pipe and forks two children. The first child replaces its stdin with the input file and its stdout with the pipe’s write end, then runs cmd1. The second child reads from the pipe and writes to the output file. Each command is found by searching PATH from the environment.
dup2(infile, STDIN_FILENO);
dup2(pipe_fds[WRITE_END], STDOUT_FILENO);
close(pipe_fds[READ_END]);
execve(path, args, envp);
What I learned: the groundwork for minishell. Above all, a child process has to close every file descriptor it doesn’t need, or the pipeline never finishes.
RANK 4 · CLEARED
NetPracticeRank 4Ten networking puzzles about TCP/IP addressing, solved by hand. Each one means fixing IP addresses, subnet masks, gateways and routing tables until every host can reach its target.IPv4 subnettingRoutingNetwork debuggingShow walkthrough
Each level shows a small network diagram with broken settings. You get it working by choosing addresses and masks so hosts sit in the right subnets, making sure subnets don’t overlap, adding default routes on hosts and routers, and avoiding reserved ranges.
The core skill is quick binary arithmetic on masks. For example, a /26 splits a /24 into four blocks of 64 addresses, and 62 of those can be hosts in each block.
What I learned: to read a routing problem as “which hop doesn’t know where to send this packet?” That helps every time I debug Docker networks or VPS firewalls.
C++ Modules 00–04Rank 4An introduction to C++98 over five modules covering classes, references, operator overloading, the Orthodox Canonical Form, inheritance, polymorphism and abstract classes.Object-oriented designMemory ownershipOperator overloadingShow walkthrough
Five short modules, each with several small exercises: a phonebook (classes and encapsulation), memory and references, a fixed-point number class with full operator overloading, a ClapTrap inheritance chain, and finally polymorphic Animals with deep copies and abstract base classes.
I wrote a small script, generate_canonical_class.sh, that generates the boilerplate for a class in Orthodox Canonical Form (constructor, copy constructor, assignment operator, destructor), so each exercise could focus on the new idea.
What I learned: the rule of three, when a base class needs a virtual destructor, and how shallow copies cause double frees.
RANK 5 · CLEARED
C++ Modules 05–09Rank 5Advanced C++98 covering exceptions, type casts, templates, STL containers and algorithms. It ends with a Bitcoin price lookup, a reverse-Polish-notation calculator and a Ford-Johnson merge-insertion sort.Generic programmingSTLAlgorithmsException safetyShow walkthrough
- 05: a bureaucracy built on exception classes (forms that can’t be signed throw).
- 06: the four C++ casts and type identification without
typeid. - 07: function and class templates, including a templated
Array<T>. - 08: STL containers with iterators, such as
easyfindand aMutantStackthat can be iterated. - 09: three real programs. A Bitcoin price lookup using
std::map::lower_bound, an RPN calculator built on a stack, and PmergeMe, a Ford-Johnson merge-insertion sort implemented on bothstd::vectorandstd::dequeto compare their performance.
What I learned: to choose containers based on their access patterns, and that the textbook complexity of an algorithm doesn’t always predict how fast it runs on real hardware.
ft_ircRank 5An IRC server in C++98 that real IRC clients can connect to. It handles many clients in a single thread with non-blocking sockets and poll(), and supports channels, operators, private messages and modes.Network programmingProtocol implementationEvent-driven I/OTeam collaborationShow walkthrough
Architecture
IRC client ──TCP──┐
IRC client ──TCP──┼──▶ Server (one thread, one poll() loop)
IRC client ──TCP──┘ │
├─ listening socket: accept() new Client
├─ readable client: recv() into its buffer,
│ split on "\r\n", run command
└─ writable client: flush its write buffer
│
Client ◀────┴────▶ Channel (members, operators, modes)
Every socket is set to O_NONBLOCK and watched by a single poll() call:
if (poll(&_pfds[0], _pfds.size(), -1) < 0)
{
if (!_running)
break;
throw PollFailedError();
}
A client’s data can arrive split across several reads, or several commands can arrive in one read, so every client has its own input buffer and only complete \r\n lines are processed. Replies go into a per-client write buffer that is sent when the socket is writable. That way a slow client can never block the whole server.
The server handles registration (PASS, NICK, USER), JOIN, PART, LIST, NAMES, PRIVMSG, NOTICE, KICK, INVITE, TOPIC, MODE (i t k o l), PING and QUIT, with the numeric replies defined in RFC 2812. We tested it with the real IRC clients irssi and WeeChat, and with raw nc sessions.
What I learned: how TCP really delivers data (as a stream of bytes, not messages), how to read an RFC, and how to split a codebase between two people with clear interfaces.
- Crew
- dgomez-a
- My role
- I built the server core (socket setup, the poll() event loop, per-client read and write buffers, disconnects) and the PING, QUIT, PRIVMSG and NOTICE commands. My teammate built most of the channel commands.
InceptionRank 5A self-built web infrastructure in Docker Compose. NGINX with TLS-only access, WordPress with PHP-FPM, and MariaDB, each in its own Debian container built from my own Dockerfiles, with Docker secrets and persistent volumes.ContainerisationTLSSecrets managementInfrastructure designShow walkthrough
Architecture
HTTPS :443 (TLS 1.2/1.3 only)
browser ───────────────▶ ┌─────────┐
│ NGINX │ reverse proxy, TLS termination
└────┬────┘
FastCGI │ :9000 private bridge network
┌────▼──────────┐
│ WordPress + │ ◀── volume: wp files
│ PHP-FPM │
└────┬──────────┘
│ :3306
┌────▼────┐
│ MariaDB │ ◀── volume: database
└─────────┘
secrets/ ──(mounted as files)──▶ WordPress, MariaDB
Design choices
- One process per container. No
supervisord: each container’s main process is its daemon, so Docker restarts and signals work as intended. - Images built from my own Dockerfiles on
debian:bookworm-slim. No pre-built images from Docker Hub. - Secrets are mounted as files, never passed as environment variables, which would show up in
docker inspectand in logs. A setup script generates the credentials on first build, so no keys live in the repository. - Only port 443 is exposed. MariaDB and PHP-FPM can only be reached on the private network.
- First-run scripts initialise the database and install WordPress only if they haven’t been set up yet, so restarts are idempotent.
What I learned: the difference between virtual machines and containers in practice, how to debug containers that fail at startup, and the security habits I now use for every deployment.