This project is about coding a C library. It contain a lot of general purpose functions your programs will rely upon
This project is about understanding the way these functions work, implementing and learning to use them. It will be helpful since you will use it in your next C school assignments
As most of 42's Projects, this one also have a few rules (such as the prohibition of the use of for, VLA, global variables and others).
git clone [email protected]:rodrigo-br/libft_42.git
cd libft_42/
make
touch main.c
echo '#include "printf/ft_printf.h"' > main.c
(write a main)
cc main.c libft.a && ./a.out
Suggestion of main:
#include "printf/ft_printf.h"
int main(void)
{
char *a = "125";
int x;
x = ft_atoi(a);
ft_printf("%d\n", x - 83);
return (0);
}
This part is about recreate a set of functions from the libc. The functions will have the same prototypes and the same behaviors as the originals. They must comply with the way they are defined in their man. The only difference will be their names. They will begin with the ’ft_’ prefix. For instance, strlen becomes ft_strlen.
isalpha
ft_isalpha
checks for an alphabetic character; in the standard "C" locale, it is equiva‐ lent to (isupper(c) || islower(c)). In some locales, there may be addi‐ tional characters for which isalpha() is true—letters which are neither up‐ percase nor lowercase.
isalnum
ft_isalnum
checks for an alphanumeric character; it is equivalent to (isalpha(c) || is‐ digit(c)).
isascii
ft_isascii
checks whether c is a 7-bit unsigned char value that fits into the ASCII character set.
strlen
ft_strlen
calculates the length of the string pointed to by s, excluding the terminating null byte ('\0').
memset
ft_memset
fills the first n bytes of the memory area pointed to by s with the constant byte c.
bzero
ft_bzero
erases the data in the n bytes of the memory starting at the location pointed to by s, by writing zeros (bytes containing '\0') to that area.
memcpy
ft_memcpy
copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.
memmove
ft_memmove
copies n bytes from memory area src to memory area dest. The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the tempo‐ rary array to dest.
strlcpy
ft_strlcpy
copies up to size - 1 characters from the NUL-terminated string src to dst, NUL-terminating the result.
strlcat
ft_strlcat
appends the NUL-terminated string src to the end of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-terminating the result.
strrchr
ft_strrchr
function returns a pointer to the last oc‐ currence of the character c in the string s.
strncmp
ft_strncmp
compares the two strings s1 and s2. The locale is not taken into account. It returns an inte‐ ger less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
memchr
ft_memchr
scans the initial n bytes of the memory area pointed to by s for the first instance of c. Both c and the bytes of the memory area pointed to by s are interpreted as unsigned char.
memcmp
ft_memcmp
compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.
strnstr
ft_strnstr
locates the first occurrence of the null-terminated string little in the string big, where not more than len characters are searched. Characters that appear after a ‘\0’ character are not searched.
calloc
ft_calloc
allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free(). If the multiplication of nmemb and size would result in integer overflow, then calloc() re‐ turns an error.
strdup
ft_strdup
returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).
In this second part, is developed a set of functions that are either not in the libc, or that are part of it but in a different form.
ft_substr
ft_substr
Allocates (with malloc(3)) and returns a substring from the string ’s’. The substring begins at index ’start’ and is of maximum size ’len’.
ft_strjoin
ft_strjoin
Allocates (with malloc(3)) and returns a new string, which is the result of the concatenation of ’s1’ and ’s2’.
ft_strtrim
ft_strtrim
Allocates (with malloc(3)) and returns a copy of ’s1’ with the characters specified in ’set’ removed from the beginning and the end of the string.
ft_split
ft_split
Allocates (with malloc(3)) and returns an array of strings obtained by splitting ’s’ using the character ’c’ as a delimiter. The array must end with a NULL pointer.
ft_itoa
ft_itoa
Allocates (with malloc(3)) and returns a string representing the integer received as an argument. Negative numbers must be handled.
ft_strmapi
ft_strmapi
Applies the function ’f’ to each character of the string ’s’, and passing its index as first argument to create a new string (with malloc(3)) resulting from successive applications of ’f’.
ft_striteri
ft_striteri
Applies the function ’f’ on each character of the string passed as argument, passing its index as first argument. Each character is passed by address to ’f’ to be modified if necessary.
ft_putendl_fd
ft_putendl_fd
Outputs the string ’s’ to the given file descriptor followed by a newline.
This part is about implement a list (t_list) and some functions in order to easily use it.
The members of the t_list struct are:
- content: The data contained in the node. void * allows to store any kind of data.
- next: The address of the next node, or NULL if the next node is the last one.
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
ft_lstnew
ft_lstnew
Allocates (with malloc(3)) and returns a new node. The member variable ’content’ is initialized with the value of the parameter ’content’. The variable ’next’ is initialized to NULL.
ft_lstdelone
ft_lstdelone
Takes as a parameter a node and frees the memory of the node’s content using the function ’del’ given as a parameter and free the node. The memory of ’next’ must not be freed.
ft_lstclear
ft_lstclear
Deletes and frees the given node and every successor of that node, using the function ’del’ and free(3). Finally, the pointer to the list must be set to NULL.
ft_lstiter
ft_lstiter
Iterates the list ’lst’ and applies the function ’f’ on the content of each node.
ft_lstmap
ft_lstmap
Iterates the list ’lst’ and applies the function ’f’ on the content of each node. Creates a new list resulting of the successive applications of the function ’f’. The ’del’ function is used to delete the content of a node if needed.
Checkout the printf function repository for more information about the ft_printf.