A comprehensive C library that reimplements standard C library functions and provides additional utilities for linked list operations.
Libft is a custom C library that provides implementations of commonly used standard library functions. This project serves as a foundation for understanding low-level programming concepts, memory management, and data structures in C.
The library includes:
- 42 mandatory functions that replicate standard C library behavior
- Bonus functions for linked list operations
- Memory-safe implementations with proper error handling
- Optimized code following 42 coding standards
- β Memory Management: Safe allocation and deallocation functions
- β String Manipulation: Comprehensive string handling utilities
- β Character Classification: ASCII character testing functions
- β Linked Lists: Complete linked list implementation
- β File I/O: File descriptor-based output functions
- β Type Conversion: String-to-number and number-to-string conversions
- Clone the repository:
git clone <repository-url>
cd Libft_42- Compile the library:
make# Compile the main library
make
# Compile with bonus functions (linked lists)
make bonus
# Clean object files
make clean
# Clean everything (objects + library)
make fclean
# Recompile everything
make re# Compile your program with libft
gcc -Wall -Wextra -Werror your_program.c -L. -lft -o your_program
# Or include the library directly
gcc -Wall -Wextra -Werror your_program.c libft.a -o your_program| Function | Description | Prototype |
|---|---|---|
ft_isalpha |
Check if character is alphabetic | int ft_isalpha(int c) |
ft_isdigit |
Check if character is a digit | int ft_isdigit(int c) |
ft_isalnum |
Check if character is alphanumeric | int ft_isalnum(int c) |
ft_isascii |
Check if character is ASCII | int ft_isascii(int c) |
ft_isprint |
Check if character is printable | int ft_isprint(int c) |
ft_isspace |
Check if character is whitespace | int ft_isspace(int c) |
ft_toupper |
Convert character to uppercase | int ft_toupper(int c) |
ft_tolower |
Convert character to lowercase | int ft_tolower(int c) |
| Function | Description | Prototype |
|---|---|---|
ft_strlen |
Get string length | size_t ft_strlen(const char *s) |
ft_strlcpy |
Copy string with size limit | size_t ft_strlcpy(char *dst, const char *src, size_t size) |
ft_strlcat |
Concatenate string with size limit | size_t ft_strlcat(char *dst, const char *src, size_t size) |
ft_strchr |
Find first occurrence of character | char *ft_strchr(const char *s, int c) |
ft_strrchr |
Find last occurrence of character | char *ft_strrchr(const char *s, int c) |
ft_strncmp |
Compare strings up to n characters | int ft_strncmp(const char *s1, const char *s2, size_t n) |
ft_strnstr |
Find substring in string | char *ft_strnstr(const char *big, const char *little, size_t len) |
ft_strdup |
Duplicate string | char *ft_strdup(const char *s) |
ft_substr |
Extract substring | char *ft_substr(char const *s, unsigned int start, size_t len) |
ft_strjoin |
Join two strings | char *ft_strjoin(char const *s1, char const *s2) |
ft_strtrim |
Remove characters from string ends | char *ft_strtrim(char const *s1, char const *set) |
ft_split |
Split string by delimiter | char **ft_split(char const *s, char c) |
ft_strmapi |
Apply function to string characters | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) |
ft_striteri |
Apply function to string with index | void ft_striteri(char *s, void (*f)(unsigned int, char *)) |
| Function | Description | Prototype |
|---|---|---|
ft_memset |
Set memory to value | void *ft_memset(void *s, int c, size_t n) |
ft_bzero |
Zero out memory | void ft_bzero(void *s, size_t n) |
ft_memcpy |
Copy memory | void *ft_memcpy(void *dest, const void *src, size_t n) |
ft_memmove |
Move memory (handles overlap) | void *ft_memmove(void *dest, const void *src, size_t n) |
ft_memchr |
Find character in memory | void *ft_memchr(const void *s, int c, size_t n) |
ft_memcmp |
Compare memory | int ft_memcmp(const void *s1, const void *s2, size_t n) |
ft_calloc |
Allocate and zero memory | void *ft_calloc(size_t nmemb, size_t size) |
| Function | Description | Prototype |
|---|---|---|
ft_atoi |
Convert string to integer | int ft_atoi(const char *nptr) |
ft_atol |
Convert string to long | long ft_atol(char *str) |
ft_itoa |
Convert integer to string | char *ft_itoa(int n) |
| Function | Description | Prototype |
|---|---|---|
ft_putchar_fd |
Write character to file descriptor | void ft_putchar_fd(char c, int fd) |
ft_putstr_fd |
Write string to file descriptor | void ft_putstr_fd(char *s, int fd) |
ft_putendl_fd |
Write string with newline to fd | void ft_putendl_fd(char *s, int fd) |
ft_putnbr_fd |
Write number to file descriptor | void ft_putnbr_fd(int n, int fd) |
| Function | Description | Prototype |
|---|---|---|
ft_lstnew |
Create new list node | t_list *ft_lstnew(void *content) |
ft_lstadd_front |
Add node to front of list | void ft_lstadd_front(t_list **lst, t_list *new) |
ft_lstadd_back |
Add node to back of list | void ft_lstadd_back(t_list **lst, t_list *new) |
ft_lstsize |
Count list nodes | int ft_lstsize(t_list *lst) |
ft_lstlast |
Get last node in list | t_list *ft_lstlast(t_list *lst) |
ft_lstdelone |
Delete single node | void ft_lstdelone(t_list *lst, void (*del)(void *)) |
ft_lstclear |
Clear entire list | void ft_lstclear(t_list **lst, void (*del)(void *)) |
ft_lstiter |
Apply function to each node | void ft_lstiter(t_list *lst, void (*f)(void *)) |
#include "libft.h"
int main(void)
{
char *str = "Hello, World!";
// Get string length
size_t len = ft_strlen(str);
printf("Length: %zu\n", len);
// Convert to uppercase
char *upper = ft_strdup(str);
for (size_t i = 0; i < len; i++)
upper[i] = ft_toupper(upper[i]);
printf("Uppercase: %s\n", upper);
// Split string
char **words = ft_split(str, ' ');
for (int i = 0; words[i]; i++)
printf("Word %d: %s\n", i, words[i]);
free(upper);
// Free split array...
return (0);
}#include "libft.h"
int main(void)
{
t_list *list = NULL;
// Create and add nodes
ft_lstadd_back(&list, ft_lstnew("First"));
ft_lstadd_back(&list, ft_lstnew("Second"));
ft_lstadd_back(&list, ft_lstnew("Third"));
// Print list size
printf("List size: %d\n", ft_lstsize(list));
// Iterate through list
t_list *current = list;
while (current)
{
printf("Node: %s\n", (char *)current->content);
current = current->next;
}
// Clean up
ft_lstclear(&list, free);
return (0);
}#include "libft.h"
int main(void)
{
// Allocate and zero memory
int *numbers = ft_calloc(10, sizeof(int));
// Set memory to specific value
ft_memset(numbers, 42, 10 * sizeof(int));
// Copy memory
int *copy = ft_calloc(10, sizeof(int));
ft_memcpy(copy, numbers, 10 * sizeof(int));
// Clean up
free(numbers);
free(copy);
return (0);
}This is a 42 school project, but suggestions and improvements are welcome. Please ensure any contributions follow the existing coding style and include proper error handling.
This project is part of the 42 school curriculum. The code is provided for educational purposes.
Author: vharkush
School: 42
Project: Libft
Status: β
Complete