42 Common Core — Libft: Rebuilding the C Standard Library
42 Common Core — Libft: Rebuilding the C Standard Library

42 Common Core — Libft: Rebuilding the C Standard Library

Systems & Low-Level Deep dive CLinux
Created on:
Team Size: 1
Tool Used: C/GCC/Make/ar

Part of the project CodeTester — An Interactive C Tester for 42 Projects

Part 1 of 3 in 42 Common Core
  1. 42 Common Core — Libft: Rebuilding the C Standard Library
  2. 42 Common Core — ft_printf: Variadic Functions and Format Parsing
  3. 42 Common Core — get_next_line: Reading a File One Line at a Time

Libft is the first project of the 42 Common Core: write your own C library, libft.a, that re-implements a selection of libc functions plus a set of utilities the standard library doesn’t provide. Almost every later project forbids most of libc, so this library becomes the foundation for everything that follows.

The rules are strict: only malloc, free and write from the outside world, everything compiled with -Wall -Wextra -Werror, and the code must follow the 42 Norm (at most 25 lines per function, 5 functions per file, declarations at the top…).

What’s in the library

PartContents
Libc functionsctype checks, memset / memcpy / memmove, strlen, strlcpy / strlcat, strnstr, atoi, calloc, strdup…
Additional functionssubstr, strjoin, strtrim, split, itoa, strmapi, striteri, the put*_fd family
Linked listslstnew, lstadd_front/back, lstsize, lstlast, lstdelone, lstclear, lstiter, lstmap

All prototypes live in a single libft.h, and the Makefile packs the objects into a static archive with ar rcs, so any later project only needs #include "libft.h" and libft.a.

Overlapping memory: memcpy vs memmove

memcpy is allowed to assume the two areas don’t overlap; memmove isn’t. If the destination starts inside the source and you copy forwards, you overwrite bytes before you’ve read them. The fix is to pick the copy direction from the pointer order:

if (d < s)
    while (n--)
        *d++ = *s++;     /* destination before source: copy forwards */
else
    while (n--)
        d[n] = s[n];     /* destination after source: copy backwards */

Size-bounded strings: strlcpy and strlcat

These BSD functions are easy to get almost right. The details that count:

  • they always NUL-terminate when size > 0;
  • they return the length of the string they tried to create (strlen(src), or dst_len + src_len), so the caller can detect truncation with ret >= size;
  • strlcat must not read past size bytes of dst when it isn’t terminated within them.

The return value is what most tests catch: returning the number of bytes actually copied looks fine on short strings and fails as soon as truncation happens.

Clean failure in allocating functions

Every function that returns a pointer can fail. ft_split is the classic trap: it makes one allocation for the array, then one per word. If the fourth word fails, the first three must be freed, or they leak.

strs[index] = (char *)malloc(str - from_ptr + 1);
if (!strs[index])
    return (free_all(strs, index));   /* frees words 0..index-1, then the array */

The same rule applies to ft_lstmap: if creating a node fails, the partly built list must be cleared with del, and the content f just returned must be freed too, before returning NULL.

Edge cases worth knowing

  • ft_itoa(INT_MIN): -INT_MIN overflows an int, so the conversion has to work on a wider or unsigned type.
  • ft_calloc(nmemb, size): nmemb * size can overflow size_t; a correct implementation checks for it before allocating.
  • ft_substr with start beyond the string must return an empty string, not NULL.
  • The ctype functions take an int that may be EOF or a value above 127.

How CodeTester checks it

Writing a good tester taught me more about these functions than writing them. For libft, CodeTester runs:

  • one suite per function (all 43), each in a forked process, so a crash in one doesn’t hide the others; missing functions are reported as MISSING thanks to weak symbols;
  • comparisons against the real libc function wherever one exists;
  • a memory suite that wraps malloc and makes the 1st, 2nd, 3rd… allocation fail in turn, checking that ft_split, ft_strjoin, ft_strtrim, ft_itoa and the other allocating functions return NULL and free everything they had already allocated;
  • subject checks: Makefile rules, no relinking, flags, library and header names, forbidden functions.

Result: 100/100, including the linked-list functions.

Libft on GitHub
© 2026 Samuel Styles