Libft is a custom library written in C as part of the 42 London curriculum. It replicates a selection of standard C library functions and introduces additional functionality for handling linked lists. The project is designed to reinforce understanding of memory allocation, string manipulation, and data structures.
The Libft project involves recreating functions from the standard C library (libc
) as well as implementing additional utility functions. It is divided into two parts:
- Mandatory Part: Reimplement common standard C functions.
- Bonus Part: Create a set of functions to manipulate linked lists.
This library can be reused in future 42 projects.
The following functions are implemented in the mandatory part of the project:
Function Name | Description |
---|---|
isalpha |
Checks if a character is alphabetic. |
isdigit |
Checks if a character is a digit. |
isalnum |
Checks if a character is alphanumeric. |
isascii |
Checks if a character is an ASCII character. |
isprint |
Checks if a character is printable. |
strlen |
Returns the length of a string. |
memset |
Fills memory with a constant byte. |
bzero |
Zeros out a block of memory. |
memcpy |
Copies memory from one location to another. |
memmove |
Copies memory, handling overlapping regions safely. |
strlcpy |
Copies a string with size limiting. |
strlcat |
Concatenates two strings with size limiting. |
toupper |
Converts a character to uppercase. |
tolower |
Converts a character to lowercase. |
strchr |
Locates the first occurrence of a character in a string. |
strrchr |
Locates the last occurrence of a character in a string. |
strncmp |
Compares two strings up to a specified number of characters. |
memchr |
Searches memory for a specific byte. |
memcmp |
Compares two blocks of memory. |
strnstr |
Locates a substring within a string, up to a given length. |
atoi |
Converts a string to an integer. |
These functions use malloc
for dynamic memory allocation:
Function Name | Description |
---|---|
calloc |
Allocates and zeroes out memory. |
strdup |
Duplicates a string by allocating memory for the copy. |
In the bonus part of the project, the following linked list functions were implemented:
Function Name | Description |
---|---|
ft_lstnew |
Creates a new node with a given content. |
ft_lstadd_front |
Adds a new node at the beginning of a list. |
ft_lstsize |
Counts the number of nodes in a list. |
ft_lstlast |
Returns the last node in a list. |
ft_lstadd_back |
Adds a new node at the end of a list. |
ft_lstdelone |
Deletes a node and frees its content. |
ft_lstclear |
Deletes and frees a list and its contents. |
ft_lstiter |
Iterates through a list, applying a function to each node's content. |
ft_lstmap |
Creates a new list by applying a function to each node's content. |
To use Libft, include its header file in your C program:
#include "libft.h"
To demonstrate how to use some of the functions implemented in Libft, you can write a simple program like the following:
#include "libft.h"
#include <stdio.h>
int main(void)
{
// Example: Using ft_strlen to find the length of a string
char *str = "Hello, Libft!";
size_t length = ft_strlen(str);
printf("Length of string: %zu\n", length);
// Example: Creating a linked list node using ft_lstnew
t_list *node = ft_lstnew("Node content");
if (node)
printf("Node content: %s\n", (char *)node->content);
return 0;
}
Compile and run this program with:
gcc -o test_program test_program.c -L. -lft
./test_program
To build the Libft library, follow these steps:
-
Run the
make
command in the project directory:make
-
This will create a static library file named
libft.a
. -
To clean up the object files, use:
make clean
-
To remove all compiled files, including
libft.a
, use:make fclean
-
To recompile everything from scratch, use:
make re
To integrate Libft into your project:
-
Include the
libft.h
header file in your source files:#include "libft.h"
-
Link the
libft.a
static library during compilation:gcc -o your_program your_program.c -L. -lft
-
Ensure the
libft.a
file is in the same directory or adjust the-L
path accordingly.
You can write your own test files to verify the behavior of the library functions. For example:
#include "libft.h"
#include <stdio.h>
int main(void)
{
// Testing ft_atoi
char *num_str = "42";
int num = ft_atoi(num_str);
printf("String '%s' converted to integer: %d\n", num_str, num);
// Testing ft_memset
char buffer[10];
ft_memset(buffer, 'A', sizeof(buffer) - 1);
buffer[9] = '\0';
printf("Buffer after memset: %s\n", buffer);
return 0;
}
Compile and run this file as described in the Compilation section.
This project is part of the 42 curriculum and is intended for educational purposes. You are free to use, modify, and share the code for learning and personal projects. Attribution to the author is appreciated but not required.
Happy coding! 🚀