Skip to content

rfs-hybrid-42-common-core/libft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

23 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

This project has been created as part of the 42 curriculum by maaugust.

Libft Cover

๐Ÿ› ๏ธ Libft: Your Very First Own Library


๐Ÿ’ก Description

Libft is the first project of the 42 Common Core curriculum. It involves coding a custom C library (libft.a) from scratch to simulate the behavior of standard C library functions (libc).

The goal is to understand the low-level implementation of memory manipulation, string handling, and linked listsโ€”fundamental skills for all future C projects at 42.


๐Ÿ“‘ Feature

This library is structured into three distinct parts complying with the project subject.

๐Ÿ”น Part 1: Libc Re-implementation

Standard C functions recoded with strict adherence to the original man descriptions.

Memory String Manipulation Character Checks Conversion/Output
ft_memset ft_strlen ft_isalpha ft_atoi
ft_bzero ft_strlcpy ft_isdigit ft_toupper
ft_memcpy ft_strlcat ft_isalnum ft_tolower
ft_memmove ft_strchr ft_isascii ft_calloc
ft_memchr ft_strrchr ft_isprint ft_strdup
ft_memcmp ft_strncmp
ft_strnstr

๐Ÿ”ธ Part 2: Additional Functions

Utility functions that are either missing from libc or exist in a different form.

Function Description
ft_substr Allocates and returns a substring from the string s.
ft_strjoin Allocates and returns a new string, result of the concatenation of s1 and s2.
ft_strtrim Allocates and returns a copy of s1 with characters in set removed from start/end.
ft_split Allocates and returns an array of strings obtained by splitting s using c as delimiter.
ft_itoa Allocates and returns a string representing the integer received as an argument.
ft_strmapi Applies function f to each char of string s to create a new string.
ft_striteri Applies function f to each char of string s by address (modifying in place).
ft_putchar_fd Outputs the character c to the given file descriptor.
ft_putstr_fd Outputs the string s to the given file descriptor.
ft_putendl_fd Outputs the string s to the given file descriptor followed by a newline.
ft_putnbr_fd Outputs the integer n to the given file descriptor.

๐Ÿš€ Bonus Part: Linked Lists

Functions to manipulate memory using the t_list structure.

typedef struct      s_list
{
    void            *content;
    struct s_list   *next;
}                   t_list;
Function Description
ft_lstnew Creates a new list node.
ft_lstadd_front Adds a new node at the beginning of the list.
ft_lstsize Counts the number of nodes in a list.
ft_lstlast Returns the last node of the list.
ft_lstadd_back Adds a new node at the end of the list.
ft_lstdelone Deletes and frees a single node.
ft_lstclear Deletes and frees a node and all its successors.
ft_lstiter Iterates the list and applies a function to the content of each node.
ft_lstmap Iterates the list and applies a function to create a new list.

๐Ÿง  Algorithm & Data Structure

To truly understand C, one must understand how data is manipulated at the byte level. Here is the core logic behind the different function categories:

1. Memory Manipulation (void * & unsigned char)

Functions like ft_memset, ft_memcpy, and ft_memcmp receive void * pointers so they can handle any data type (strings, structs, integer arrays). However, you cannot dereference or do math on a void *.

  • The Logic: We cast these pointers to unsigned char *. An unsigned char is exactly 1 byte (8 bits). This guarantees that our loops iterate and manipulate the memory address exactly one byte at a time, ensuring safe and predictable behavior regardless of the original data type.
  • The ft_memmove Safety Net: Unlike memcpy, memmove checks if the source and destination memory blocks overlap. If the destination is ahead of the source, copying left-to-right would overwrite data before it gets copied. memmove solves this by copying backwards (right-to-left).

2. String Operations & Pointer Arithmetic

In C, strings are simply arrays of characters terminated by a Null-byte (\0).

  • The Logic: Functions like ft_strlen use pointer arithmetic (subtracting the base pointer from the end pointer) to calculate length instantly without needing an incremental integer counter.
  • Buffer Safety (strlcpy / strlcat): Unlike older libc functions (strcpy), the size-bounded versions demand the total size of the destination buffer. They guarantee that the resulting string will always be null-terminated without overflowing into restricted memory segments, preventing Segmentation Faults.

3. Allocation & Dynamic Memory (malloc & free)

Part 2 introduces functions that create entirely new strings.

  • The Logic (ft_split): This function requires allocating an array of pointers (a 2D array). The core logic relies on heavily protecting the memory: if any single word fails to malloc midway through, a helper function must iterate backward, free() every previously allocated word, and finally free the parent array to prevent memory leaks.

4. Linked Lists (Non-Contiguous Memory)

Unlike arrays, which demand a single block of continuous memory, linked lists allocate memory dynamically wherever there is space on the heap.

  • The Logic: Each node (t_list) contains two things: the actual data (content), and a pointer (next) holding the memory address of the subsequent node. Functions like ft_lstlast or ft_lstadd_back require traversing this chain using a while (node->next != NULL) loop to manually find the end before making adjustments.

๐Ÿ› ๏ธ Instructions

๐Ÿ“ฆ Installation

To compile the library, run the following command in the root of the repository:

make

To compile the library including the bonus functions:

make bonus

This will generate the libft.a archive file.

๐Ÿงน Cleaning

  • make clean: Removes object files (.o).
  • make fclean: Removes object files and the libft.a library.
  • make re: Performs a clean re-build.

๐Ÿ’ป Usage

To use this library in your code, include the header and link the archive during compilation:

1. Include header:

#include "libft.h"

int main()
{
    ft_putstr_fd("Hello, 42!", 1);
    return (0);
}

2. Compile:

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

๐Ÿงช Testing

The 42 subject highly encourages creating test programs to verify your work before peer evaluations.

1. Using a Custom Tester If you are using a custom libft_tester.c file, compile it alongside your library. (Using preprocessor macros like -D allows you to target specific tests if your tester is configured for it).

cc -Wall -Wextra -Werror -D TEST_PART_1 -D TEST_BONUS libft_tester.c libft.a -o tester
./tester

โš ๏ธ WARNING for 42 Students: Do not push libft_tester.c or any executable files to your final Moulinette repository! They are strictly for local testing purposes. Submitting unauthorized files will result in a 0.

2. Third-Party Testers (Francinette) Francinette is a widely used testing framework within the 42 community that runs strict tests (including memory leak checks and edge cases) against your library.

paco

(Note: To include bonus functions, use paco -b. To enforce strict norm and timeout rules, use paco -s)

๐Ÿšจ The Norm

Moulinette relies on a program called norminette to check if your files comply with the 42 Norm. Every single .c and .h file, including bonus files, must pass this check. If there is a norm error, you will receive a 0.

The 42 Header: Before writing any code, every file must start with the standard 42 header. norminette will automatically fail any file missing this specific signature.

/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_isalpha.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: maaugust <maaugust@student.42porto.com>    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/04/08 13:24:04 by maaugust          #+#    #+#             */
/*   Updated: 2025/04/09 16:21:38 by maaugust         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

Run the following command in the root of your repository before pushing to check all your files at once:

norminette -R CheckForbiddenSourceHeader

๐Ÿ“š Resources & References

System Manuals:

  • man 3 isalpha / man 3 isdigit - Essential manuals for character classification functions.
  • man 3 string - Overview of C string manipulation functions (or query individually like man 3 strlen, man 3 strlcpy).
  • man 3 bzero / man 3 memset / man 3 memcpy - Manuals detailing raw byte-level memory manipulation.
  • man 3 malloc / man 3 free - Manuals detailing dynamic memory allocation.

Articles & Guides:

Video Tutorials:

42 Standards:

๐Ÿค– AI Usage & Transparency

In the spirit of transparency and the learning objectives of the 42 curriculum, here is how AI tools were utilized during this project:

  • Conceptual Validation: Used strictly to explain complex algorithm logic, such as linked list reversal techniques.
  • Documentation & Formatting: Assisted in generating portfolio-ready documentation templates.
  • Zero Code Generation: No core logic was generated by AI. All functions were 100% manually implemented to ensure a foundational understanding of pointers and memory management.

About

My custom implementation of the C standard library. Focuses on advanced memory handling, string manipulation, and full linked list data structures (Bonus).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors