- Introduction
- Objective
- Instructions
- Dependencies
- Installation
- Usage
- Testing
- Future
- Mechanics
- Credits
- Disclaimer
- Keep In Touch
This project is our first official project at my school 42. The project is to create our own implementation of the standard C library. This serves two purposes, to help us learn the inner workings of the language and to use and implement in all future C projects while attending.
I'm Mike Brave and I'm a programming student at 42. I'm specializing in Procedural Generation and Creative Machine Learning. I also do a lot with mobile apps, UX Design and low level C programming. I wrote the curriculum on swift for the school.
42 is a free, non profit, project-based, peer-to-peer learning coding school. It originated in France and now has over 20 campuses all over the world. More information can be found here
Reverse engineer the following functions. Most are from the standard C library and a select others that would be considered useful in both learning and in future building.
The Assignment is broken up into a part 1 and part 2, the further divisions are my own for maintenance purposes.
"Is" Group
Function | Description |
---|---|
ft_isalpha() | The ft_isalpha() function tests for any character for which isupper(3) or islower(3) is true. The value of the argument must be representable as an unsigned char or the value of EOF. |
ft_isdigit() | The ft_isdigit() function tests for a decimal digit character. Regardless of locale, this includes the following characters only: '0' - '9'. |
ft_isalnum() | The ft_isalnum() function tests for any character for which ft_isalpha(3) or ft_isdigit(3) is true. The value of the argument must be representable as an unsigned char or the value of EOF. |
ft_isascii() | The ft_isascii() function tests for an ASCII character, which is any character between 0 and decimal 127 inclusive. |
ft_isprint() | The ft_isprint() function tests for any printing character, including space (' '). The value of the argument must be representable as an unsigned char or the value of EOF. |
"Str 1" Group
Function | Description |
---|---|
ft_strlen() | The ft_strlen() function computes and returns the length of the string s. |
ft_strdup() | The ft_strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). If insufficient memory is available, NULL is returned. |
ft_strcpy() | The ft_strcpy() function copies the string src to dst (including the terminating '\0' character.) |
ft_strncpy() | The ft_strncpy() function copies at most len characters from src into dst. If src is less than len characters long, the remainder of dst is filled with '\0' characters. |
ft_strcat() | The ft_strcat() function appends a copy of the null-terminated string s2 to the end of the null-terminated string s1, then add a terminating '\0'. The string s1 must have sufficient space to hold the result. |
ft_strncat() | The ft_strncat() function appends a copy of the null-terminated string s2 to the end of the null-terminated string s1. The ft_strncat() function appends not more than n characters from s2, and then adds a terminating '\0'. |
ft_strlcat() | The ft_strlcat() appends string src to the end of dst. It will append at most maxlen - strlen(dst) - 1 characters. It will then NUL-terminate, unless maxlen is 0 or the original dst string was longer than maxlen. maxlen should be the size of the destination string buffer dst plus the space for the nul-terminator. Returns the total length of the string it tried to create. |
ft_strchr() | The ft_strchr() function locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be part of the string; therefore if c is '\0', the functions locate the terminating '\0'. Returns a pointer to the located character, or NULL if the character does not appear in the string. |
ft_strrchr() | The ft_strrchr() function locates the last occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be part of the string; therefore if c is '\0', the functions locate the terminating '\0'. Returns a pointer to the located character, or NULL if the character does not appear in the string. |
ft_strstr() | The ft_strstr() function locates the first occurrence of the null-terminated string needle in the null-terminated string haystack. |
ft_strnstr() | The ft_strnstr() function locates the first occurrence of the null-terminated string needle in the null-terminated string haystack, where not more than len characters are searched. Characters after the '\0' are not searched. If needle is an empty string, haystack is returned; if needle occurs nowhere in haystack, NULL is returned; otherwise a pointer to the first character of the first occurrence of needle is returned. |
ft_strcmp() | The ft_strcmp() function lexicographically compares the null-terminated strings s1 and s2. Returns an integer greater than, equal to, or less than 0, according as the string s1 is greater than,equal to, or less than the string s2. The comparison is done using unsigned characters, so that '\200' is greater than '\0'. |
ft_strncmp() | The ft_strncmp() function lexicographically compares the null-terminated strings s1 and s2. Returns an integer greater than, equal to, or less than 0, according as the string s1 is greater than, equal to, or less than the string s2. Compares not more than n characters. The comparison is done using unsigned characters, so that '\200' is greater than '\0'. |
"mem 1" Group
Function | Description |
---|---|
ft_memset() | The ft_memset() function writes len bytes of value c (converted to an unsigned char) to the string b. Returns it's first argument. |
ft_bzero() | The ft_bzero() function writes n zeroed bytes to the string s. If n is zero, ft_bzero() does nothing. |
ft_memcpy() | The ft_memcpy() function copies n bytes from memory area src to memory area dst. If dst and src overlap, behavior is undefined. Returns the original value of dst. |
ft_memccpy() | The ft_memccpy() function copies bytes from string src to string dst. If the character c (as converted to an unsigned char) occurs in the string src, the copy stops and a pointer to the byte after the copy of c in the string dst is returned. Otherwise, n bytes are copied and a null pointer is returned. |
ft_memmove() | The ft_memmove() function copies len bytes from strin src to dst. The two strings may overlap; the copy is always done in a non-destructive manner. Returns the original value of dst. |
ft_memchr() | The ft_memchr() function locates the first occurence of c (converted to an unsigned char) in string s. Returns a pointer to the byte located, or NULL if no such byte exists within n bytes.) |
ft_memcmp() | The ft_memcmp() function compares byte string s1 against byte string s2. Both strings are assumed to be n bytes long. Returns 0 if the first two strings are identical, otherwise returns the difference between the first two bytes (treated as unsigned char values). Zero-length strings are always identical. |
"misc 1" Group
Function | Description |
---|---|
ft_atoi() | ft_atoi() converts the initial portion of the string pointed to by str to int representation and returns the int. |
ft_toupper() | The ft_toupper() function converts a lower-case letter to the corresponding upper-case letter. The argument must be representable as an unsigned char or the value of EOF. If the argument is a lower-case letter, the ft_toupper() function returns the corresponding upper-case letter if there is one; otherwise, the argument is returned unchanged. |
ft_tolower() | The ft_tolower() function converts an upper-case letter to the correspond-ing lower-case letter. The argument must be representable as an unsigned char or the value of EOF. |
"Puts" Group
Function | Description |
---|---|
ft_putchar() | The ft_putchar() function outputs the character c to the standard output. |
ft_putstr() | The ft_putstr() function outputs a string to the standard output. |
ft_putendl() | The ft_putendl() function outputs a string to the standard output, followed by a newline. |
ft_putnbr() | The ft_putnbr() function outputs the integer n to the standard output |
ft_putchar_fd() | The ft_putchar_fd() function outputs the character c to the specified file descriptor. A file descriptor of 0, 1, or 2, refers to the standard input, standard output, or standard error, respectively. |
ft_putstr_fd() | The ft_putstr_fd() function outputs a string to the output specified by the file descriptor. A file descriptor of 0, 1, or 2, refers to the standard input, standard output, or standard error, respectively. |
ft_putendl_fd() | The ft_putendl_fd() function outputs a string to the output specified by the file descriptor, followed by a newline. The file descriptor can be 0, 1, or 2, to refer to standard input, standard output, or standard error, respectively. |
ft_putnbr_fd() | The ft_putnbr_fd() function outputs a number to the output specified by the file descriptor. A file descriptor of 0, 1, or 2, refers to the standard input, standard output, or standard error, respectively. |
"Str 2" Group
Function | Description |
---|---|
ft_strnew() | The function ft_strnew() allocates with malloc(3) and returns a fresh string ending with '\0'. Each character of the string is initialized at '\0'. If the allocation fails, the function returns NULL. |
ft_strdel() | The ft_strdel() function takes as a parameter the address of a string that needs to be freed with free(3), then sets its pointer to NULL. |
ft_strclr() | The ft_strclr() function sets every character of the string s to '\0'. |
ft_striter() | The ft_striter() function applies the function f to each character of the string passed as argument. Each character is passed by address to f to be modified if necessary. |
ft_striteri() | The ft_striteri() function applies the function f to each character of the string passed as argument, and passing its index as first argument. Each character is passed by address to f to be modified if necessary. |
ft_strmap() | The function ft_strmap() applies the function f to each character of the string given as an argument to create a "fresh" new string (with malloc(3)) resulting from the successive applications of f. |
ft_strequ() | The ft_strequ() function does a lexicographical comparison between s1 and s2. If the 2 strings are identical the function returns 1, or 0 otherwise. |
ft_strnequ() | The ft_strnequ() function does a lexicographical comparison between s1 and s2. If the 2 strings are identical the function returns 1, or 0 otherwise. |
ft_strsub() | The ft_strsub() function allocates with malloc(3) and returns a "fresh" substring from string given as an argument. The substring begins at index start and is of size len. If start and len aren't referring to a valid substring, the behavior is undefined. If the allocation fails, the function returns NULL. |
ft_strjoin() | The ft_strjoin() function allocates (with malloc(3)) and returns a "fresh" string that is the concatenation of s1 and s2, ending with '\0'. If the allocation fails, the function returns NULL. |
ft_strtrim() | The ft_strtrim() function allocates with malloc(3) and returns a copy of the string given as argument without whitespaces at the beginning or at the end of the string. ' ', '\n' and '\t' are considered whitespace characters. If s has no whitespaces at the beginning or the end, the function returns a copy of s. If the allocation fails the function returns NULL. |
ft_strsplit() | The ft_strsplit() function allocates with malloc(3) and returns an array of fresh strings (all ending with '\0' including the array itself) obtained by splitting s using the character c as a delimiter. If the allocation fails, the function returns NULL. |
"mem 2" Group
Function | Description |
---|---|
ft_memalloc() | Allocates with malloc(3) and returns a fresh memory area. The memory allocated is initialized to 0. If the allocation fails, the function returns NULL. |
ft_memdel() | The ft_memdel() function takes the address of a memory area that needs to be freed with free(3), then puts the pointer to NULL. |
"misc 2" Group
Function | Description |
---|---|
ft_itoa() | The ft_itoa() fuction allocates (with malloc(3)) and returns a "fresh" string ending with '\0' representing the integer n given as argument. Negative numbers must be supported. If the allocation fails, the function returns NULL. |
Function | Description |
---|---|
ft_lstnew() | The function ft_lstnew() allocates (with malloc(3)) and returns a “fresh” link. The variables content and content_size of the new link are initialized by copy of the parameters of the function. If the parameter content is nul, the variable content is initialized to NULL and the variable content_size is initialized to 0 even if the parameter content_size isn’t. The variable next is initialized to NULL. If the allocation fails, the function returns NULL. |
ft_lstdelone() | The function ft_lstdelone() takes as a parameter a link's pointer address and frees the memory of the link's content using the function del given as a parameter, then frees the link's memory using free(3). The memory of next must not be freed under any circumstance. Finally, the pointer to the link that was just freed must be set to NULL (quite similar to the function ft_memdel) |
ft_lstdel() | The function ft_lstdel() takes as a parameter the address of a pointer to a link and frees the memory of that link, and every successor of that link using the functions del and free. The last pointer is set to NULL. |
ft_lstadd() | The ft_lstadd() function adds the element new at the beginning of the list alst. |
ft_lstiter() | The ft_lstiter() function iterates through a list and applies the function f to each link in the list. |
ft_lstmap() | The ft_map() function iterates a list lst and applies the function f to each link to create a “fresh” list (using malloc(3)) resulting from the successive applications of f. If the allocation fails, the function returns NULL. |
Function | Description |
---|---|
ft_isspace() | The ft_isspace() function tests for the white-space characters. For any locale, this includes the following standard characters: \t'' \n'' \v'' \f'' \r'' ''. In the "C" locale, ft_isspace() successful test is limited to these characters only. The value of the argument must be representable as an unsigned char or the value of EOF. The ft_isspace() function returns zero if the character tests false and returns non-zero if the character tests true. |
ft_iscntrl() | Checks whether c is a control character. A control character is a character that does not occupy a printing position on a display (this is the opposite of a printable character, checked with ft_isprint). For the standard ASCII character set (used by the "C" locale), control characters are those between ASCII codes 0x00 (NUL) and 0x1f (US), plus 0x7f (DEL). |
ft_isgraph() | Checks whether c is a character with graphical representation. The characters with graphical representation are all those characters than can be printed (as determined by isprint) except the space character (' '). |
ft_ispunct() | Checks whether c is a punctuation character. The standard "C" locale considers punctuation characters all graphic characters (as in isgraph) that are not alphanumeric (as in isalnum). Other locales may consider a different selection of characters as punctuation characters, but in any case they are isgraph but not isalnum. |
"printf dependencies" Group
Function | Description |
---|---|
ft_itoa_base() | The ft_itoa_base() fuction allocates (with malloc(3)) and returns a "fresh" string ending with '\0' representing the integer n given as argument according to the n base given as a second argument. Negative numbers must be supported. If the allocation fails, the function returns NULL. |
ft_itoa_unsigned() | The ft_itoa_unsigned() fuction allocates (with malloc(3)) and returns a "fresh" string ending with '\0' representing the unsigned integer n given as argument according to the n base given as a second argument. If the allocation fails, the function returns NULL. |
Make sure you have the needed dependencies before proceeding. For instructions and/or greater detail refer to the project pdf
- A C language compiler most common would be GCC or Clang.
- These were written to run on Unix based systems, it would likely work on a windows machine too but is currently untested.
First copy this repo using git.
Git clone https://github.com/michaelbrave/C-Standard-Library-Clone---42-libft.git
cd C-Standard-Library-Clone---42-libft\
This library has a makefile which has the following commands:
Command | Usage |
---|---|
make |
creates .o files for each function as well as the main library file, libft.a |
make clean |
removes the .o files used to create the library |
make fclean |
removes the .o & .a files used to create the library |
make re |
removes all .o & .a files then remakes them |
It's easiest to use the provided makefile. So type
make && make clean
There should now be a libft.a file.
To use it we include the header in your .c file, by putting this line at the top of your c file
include "libft.h"
when compiling use the -L flag and the location of the library file, an example:
gcc myFile.c -L <path_to_library> -lft
Most testing was done on a function by function basis with more thorough testing done using 42 File Checker
This library will continue to be added to and improved during my time at 42. After that time it will exist as only a reference as it was only created with the intent of learning.
For this particular project see Objective as the function descriptions also describe how they should work.
Some frameworks and libraries that were helpful with testing.
This project was done for learning purposes only and is not meant to be used as a replacement for the GNU C Library.
This is not kept up to date and I cannot guarantee that it will work on your future machines. You are welcome to use and learn from this as you want, but do not implement code that you do not understand. Copying is not the way.
You can find me on
Enjoy!