Skip to content

akdovlet/libft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libft

A hand-written C standard library, built from scratch as part of the curriculum at 42. Every function in this project is a faithful reimplementation of its libc counterpart — no calls to the real thing, no shortcuts.

No AI was used during the development or research of this project. AI was used solely to write this README.


What is it?

libft is a static C library (libft.a) that reimplements a broad subset of the standard C library (libc), plus a handful of extra utilities and a full singly-linked list module. It serves as the personal toolbox that carries forward into every subsequent 42 project — instead of relying on libc, you rely on code you understand completely because you wrote it.


Why?

The point of the exercise is not to reinvent the wheel for its own sake. It is to force a deep understanding of how foundational tools actually work:

  • How does memcpy handle overlapping memory regions (it doesn't — that's what memmove is for)?
  • What does strlcpy return when the destination is too small?
  • How do you split a string without knowing its length in advance?

Writing these functions by hand makes the answers obvious. It also means you can read, audit, and trust every line of code in your toolbox.


Contents

Character classification & conversion

Function Description
ft_isalpha Is the character alphabetic?
ft_isdigit Is it a decimal digit?
ft_isalnum Is it alphanumeric?
ft_isascii Is it a valid ASCII character (0–127)?
ft_isprint Is it a printable character?
ft_toupper Convert to uppercase
ft_tolower Convert to lowercase

Memory

Function Description
ft_memset Fill a block of memory with a byte value
ft_bzero Zero out a block of memory
ft_memcpy Copy memory (no overlap handling)
ft_memmove Copy memory safely, even with overlapping regions
ft_memchr Find the first occurrence of a byte in memory
ft_memcmp Compare two blocks of memory
ft_calloc Allocate zero-initialised memory

Strings

Function Description
ft_strlen Length of a null-terminated string
ft_strlcpy Safe string copy (always null-terminates)
ft_strlcat Safe string concatenation
ft_strchr First occurrence of a character in a string
ft_strrchr Last occurrence of a character in a string
ft_strncmp Compare up to n characters of two strings
ft_strnstr Find a substring within a length-bounded string
ft_strdup Duplicate a string (heap-allocated)
ft_substr Extract a substring
ft_strjoin Concatenate two strings into a new allocation
ft_strtrim Trim a set of characters from both ends of a string
ft_split Split a string by a delimiter into a null-terminated array
ft_strmapi Apply a function to each character, returning a new string
ft_striteri Apply a function to each character in place

Conversion

Function Description
ft_atoi Parse an integer from a string
ft_itoa Convert an integer to a heap-allocated string

Output (file-descriptor based)

Function Description
ft_putchar_fd Write a character to a file descriptor
ft_putstr_fd Write a string to a file descriptor
ft_putendl_fd Write a string followed by a newline to a file descriptor
ft_putnbr_fd Write an integer to a file descriptor

Bonus — Linked lists

A generic singly-linked list built around t_list:

typedef struct s_list
{
    void          *content;
    struct s_list *next;
}   t_list;
Function Description
ft_lstnew Allocate and initialise a new node
ft_lstadd_front Prepend a node to a list
ft_lstadd_back Append a node to a list
ft_lstlast Return the last node
ft_lstsize Count the nodes in a list
ft_lstdelone Delete a single node using a provided del function
ft_lstclear Delete all nodes and free the list
ft_lstiter Apply a function to the content of every node
ft_lstmap Build a new list by applying a function to every node

Building

# Compile the library (mandatory functions only)
make

# Compile with the bonus linked-list functions included
make bonus

# Remove object files
make clean

# Remove object files and the archive
make fclean

# Full rebuild
make re

The Makefile produces libft.a in the project root. It compiles with cc -Wall -Wextra -Werror.


Using it in your own project

Copy libft.a and libft.h into your project, then link against the archive:

cc -o my_program my_program.c -L. -lft

Or, if you keep libft as a subdirectory, adjust the paths:

cc -o my_program my_program.c -Ilibft -Llibft -lft

Include the header wherever you need it:

#include "libft.h"

Project structure

libft/
├── libft.h                  # All function prototypes and the t_list type
├── Makefile
├── ft_*.c                   # One file per function (mandatory)
└── ft_*_bonus.c             # Linked-list functions (bonus)

Author

Akhmed Dovletov[email protected]
42 — September 2023

About

My own budget libc. 42Paris' first project

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors