A reusable static C library developed during the 42 curriculum. It implements common character, string, memory, conversion, and file-descriptor helpers, with additional linked-list routines, formatted output, and buffered line reading.
The library is used as a Git submodule by projects including Minitalk, Push Swap, and Wireframe Model.
Requirements: a C compiler, Make, and the ar archive tool. Some functions use
POSIX file descriptors and read()/write(), so a Unix-like environment is
expected.
git clone https://github.com/civanaban2/libft.git
cd libft
makeThe output is libft.a. The default compiler is cc with
-Wall -Wextra -Werror.
To add linked-list functions to an archive containing the core functions, run these commands in order:
make
make bonusmake bonus archives only the bonus objects; on a clean checkout it does not
first build the core archive. Run both targets as shown if you need both sets.
Create example.c in the library root:
#include "libft.h"
int main(void)
{
char *message;
message = ft_strjoin("Hello, ", "libft!");
if (message == NULL)
return 1;
ft_putendl_fd(message, 1);
free(message);
return 0;
}cc -Wall -Wextra -Werror -I. example.c -L. -lft -o example
./example
# Hello, libft!Place -lft after the application object/source files when linking. Public
function declarations and the t_list structure are in libft.h.
| Group | Functions |
|---|---|
| Character classification | ft_isalpha, ft_isdigit, ft_isalnum, ft_isascii, ft_isprint |
| Case conversion | ft_toupper, ft_tolower |
| Memory | ft_memset, ft_bzero, ft_memcpy, ft_memmove, ft_memchr, ft_memcmp, ft_calloc |
| String length/copy | ft_strlen, ft_strlcpy, ft_strlcat, ft_strdup |
| String search/compare | ft_strchr, ft_strrchr, ft_strncmp, ft_strnstr |
| Allocated string operations | ft_substr, ft_strjoin, ft_strtrim, ft_split, ft_itoa |
| String callbacks | ft_strmapi, ft_striteri |
| Numeric conversion | ft_atoi, ft_atol |
| File-descriptor output | ft_putchar_fd, ft_putstr_fd, ft_putendl_fd, ft_putnbr_fd |
| Extensions | ft_printf, get_next_line |
Bonus linked-list functions:
ft_lstnew ft_lstadd_front ft_lstadd_back
ft_lstsize ft_lstlast ft_lstdelone
ft_lstclear ft_lstiter ft_lstmap
ft_printf supports characters, strings, pointers, signed decimal, unsigned
decimal, lowercase/uppercase hexadecimal, and literal percent conversions:
%c, %s, %p, %d, %i, %u, %x, %X, and %%. It is a limited
implementation, without general field width, precision, or length modifiers.
get_next_line(int fd) returns one allocated line per call, including its
newline when present. BUFFER_SIZE defaults to 1024. This bundled version uses
a single persistent buffer; it is not the separate multi-descriptor bonus
implementation from the Get Next Line repository.
- Free allocated string results when they are no longer needed.
- For
ft_split, free every returned token and then the pointer array. get_next_lineresults belong to the caller;NULLrepresents EOF or failure.- Linked-list content cleanup is controlled by the caller's
delcallback. - In the current implementation,
ft_lstmapclears its input list after mapping. Do not assume a non-destructive mapping contract or reuse pointers into that input list afterward.
These are educational implementations. Do not assume complete ISO C/POSIX conformance or identical error behavior to the standard library. Allocation failure paths, allocation-size overflow, conversion overflow, and callback ownership need review before use with untrusted inputs. Not every function accepts null pointers, even though a few explicitly handle them.
| Target | Action |
|---|---|
make |
Build the core static archive |
make bonus |
Archive the linked-list objects |
make clean |
Remove core and bonus object files |
make fclean |
Also remove libft.a |
make re |
Clean and rebuild core functions |
make rebonus |
Clean and rebuild core and bonus functions |
Run cleanup/rebuild targets without parallel -j execution. The existing
Makefile does not track header dependencies; after changing libft.h, perform
a clean rebuild.
A parent project records an exact libft commit, rather than automatically using
the newest main. Use the parent's submodule initialization instructions and
retain its recorded revision unless intentionally updating the dependency.
After a history rewrite, parent repositories must update their recorded
submodule commit to a revision available on the current libft branch. This
repository uses a single main branch.