Libft
ProjectM0
Your own C library of libc functions and linked lists, the base of every C project that follows.
Requires
Nothing, start here
Unlocks
Guide
Libft is the first project of the core (milestone M0) and the one every later C project leans on. Working solo in C, you rewrite a selection of libc functions, add a set of string and memory helpers that libc does not have, and (bonus) a small singly linked list API. The result is a static library, libft.a, built by your own Makefile, that you will copy into ft_printf, get_next_line, push_swap and Codexion. Treat it as a tool you will keep, not as a box to tick.
What you will learn
- How the classic libc functions really behave:
strlcpy,strlcat,strnstr,atoi,memmoveand friends, including their odd corners - Pointer arithmetic,
constcorrectness and the difference betweenchar *,unsigned char *andvoid * - Heap allocation with
mallocandfree, and returningNULLcleanly when an allocation fails - Writing a Makefile with
all,clean,fclean,reandbonusrules that does not relink - Building and linking a static library with
ar rcs - Writing to file descriptors with
write(theft_put*_fdfamily) - Linked lists with function pointers (
ft_lstiter,ft_lstmap)
Before you start
- Previous nodes: none. You should be comfortable with the C Piscine level: loops, pointers,
write, compiling withcc. - Tools:
cc(gcc or clang),make,ar,valgrind(Linux) orleaks(macOS), andnorminette(python3 -m pip install norminette). - Read the man pages of every libc function you have to copy before you write a line. The man page is the specification.
- Set up a test folder outside the repository so test files never end up in your submission.
Plan of attack
- Day 1: write the Makefile first, with
-Wall -Wextra -Werror, an object list and alibft.atarget. Check thatmaketwice in a row prints "Nothing to be done". - Day 1: the character tests (
ft_isalpha,ft_isdigit,ft_isalnum,ft_isascii,ft_isprint,ft_toupper,ft_tolower). Compare each against the real function for every value from -1 to 255. - Day 2: the memory functions (
ft_memset,ft_bzero,ft_memcpy,ft_memmove,ft_memchr,ft_memcmp). Draw the overlap cases formemmoveon paper before coding them. - Day 2: the string functions (
ft_strlen,ft_strlcpy,ft_strlcat,ft_strchr,ft_strrchr,ft_strncmp,ft_strnstr,ft_atoi), thenft_callocandft_strdup. - Day 3: the part 2 functions that allocate:
ft_substr,ft_strjoin,ft_strtrim,ft_split,ft_itoa,ft_strmapi,ft_striteri. - Day 4:
ft_splitdeserves its own day. Free every word already allocated if one allocation fails, and test with separators at the start, at the end, repeated and absent. - Day 4: the
ft_put*_fdfunctions, then the bonus list functions with abonusrule. - Day 5: run norminette on every file, run two testers, run valgrind on your own
main, and read your code once more as if you were the evaluator.
A tiny test harness is enough to start:
#include "libft.h"
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[10] = "abcdef";
char b[10] = "abcdef";
memmove(a + 2, a, 4);
ft_memmove(b + 2, b, 4);
printf("%s | %s\n", a, b);
return (0);
}
Common pitfalls
- Norm errors: more than 25 lines per function, more than 5 functions per file, declarations mixed with code, forbidden
forloops. One norm error is a 0. - Relinking: if
makerebuildslibft.awhen nothing changed, the evaluation fails. ft_strlcatreturn value whensizeis smaller than the destination length.ft_atoiwith leading whitespace, a single sign, and values nearINT_MIN.ft_callocwithnmemb * sizeoverflowingSIZE_MAX.ft_substrwhenstartis past the end of the string: return an empty allocated string, notNULL.ft_splitleaking the array or the words when amallocfails halfway.ft_memmovecopying forward when the destination overlaps the end of the source.- Forgetting to protect against
NULLwhere your own later projects will need it (the subject does not require it, so be consistent and able to justify your choice).
How it is evaluated
- Peer evaluation, then Moulinette. The evaluator first checks the repository: only the allowed files, a Makefile with the required rules, no relink, and norminette clean.
- Every function is tested, usually with a tester and with hand-written edge cases. A crash, a segfault, a wrong return value or a leak on one function is enough to fail.
- The bonus (linked list functions) is only graded if the mandatory part is perfect.
- Expect questions on any line: why
unsigned charinft_memcmp, howft_memmovehandles overlap, whatardoes.